Not matter of the today will leave tomorrow

Methods several

The whole can be divided into: XML mode, annotation mode

  • xml
    • Manual: Constructor-arg, set property
    • Automatic: byName, byType, constructor

XML way

manual

Let’s take a look at how each method is used!

A constructor

Provide two classes, one OrderService and the other UserService, which contains the OrderService property, a test method, a parameterized constructor, and a parameterless constructor

public class OrderService {}public class UserService {

	OrderService orderService;

	public UserService(a){
		System.out.println("XML no-parameter construct injection" + orderService);
	}
	public UserService(OrderService orderService1){
		System.out.println("XML parameterized construct injection" + orderService1);
		orderService = orderService1;
	}
	
	public void test(a){ System.out.println(orderService); }}Copy the code

We can take a look at configuration in XML, the constructor-arg tag

<bean class="com.gongj.populateBean.OrderService" id="orderService"></bean>

<bean class="com.gongj.populateBean.UserService" id="userService">
   <constructor-arg index="0" ref="orderService"></constructor-arg>
</bean>
Copy the code
validation
public static void main(String[] args) {
		ClassPathXmlApplicationContext context =
				new ClassPathXmlApplicationContext("spring-config2.xml"); UserService bean = context.getBean(UserService.class); bean.test(); } the result: XML has structure into com refs. Gongj. PopulateBean. OrderService @14514713
com.gongj.populateBean.OrderService@14514713
Copy the code

The set method

Add a setOrder method. This setOrder method is used to inject OrderService.


	public void setOrder(OrderService order){
		System.out.println("XML Property method injection" + order);
		orderService = order;
	}

Copy the code

To change the configuration in the XML, take a look at the UserService configuration and change the constructor-arg tag to a property tag. The value configured for name is the name of setOrder in the UserService class (omits set and starts with lower case), and the value configured for ref is the value of the id property of the OrderService above, that is, beanName.

	<bean class="com.gongj.populateBean.OrderService" id="orderService">
	</bean>

	<bean class="com.gongj.populateBean.UserService" id="userService">
		<property name="order" ref="orderService"></property>
	</bean>
Copy the code
Set authentication
XML no-argument construction injectionnullXML property method into com. Gongj. PopulateBean. OrderService @ 255316 f2 com. Gongj. PopulateBean. OrderService @ 255316 f2Copy the code

No constructor is specified. Default is no-argument construction.

automatic

Note that the bean tag has an attribute autowire, which has five different values: No, byName, byType, Constructor, and Autodetect

  • No: The default, which is to configure the property tag or constructor-arg tag in THE XML without automatic injection, which is the above two manual methods

  • ByName: Automatic injection based on the name of the set method in the entity class, without the need to configure the property tag

  • ByType: Automatic injection based on the parameter type of the set method in the entity class.

  • Constructor: Matches against constructors without the need to configure constructor-arg tags.

  • Autodetect: Construct autowage if there is a default constructor, otherwise byType autowage is deprecated.

byName

  • Example:
public void setOrder(OrderService order){
		orderService = order;
}
Copy the code

Find the Bean by the name order (omitting set and lowercase) as a beanName. If the corresponding Bean is found, the Bean is assigned to the method’s parameters and the set method is executed. Otherwise nothing will change. In this case, of course, the bean cannot be found by the name order, because the beanName in the XML configuration is called orderService, so we need to add the set method and name it setOrderService

  • Is case
public void setOrderService(OrderService order){
		System.out.println("ByName method injection"+ order); orderService = order; } result: XML no-argument construct injectionnullByName method into com. Gongj. PopulateBean. OrderService @ 255316 f2 com. Gongj. PopulateBean. OrderService @25531
Copy the code

byType

Look for the OrderService type. If more than one of the same types is found, an exception is thrown. Simply change byName to byType and the following happens:

Result: XML no-parameter construct injectionnullXML property method into com. Gongj. PopulateBean. OrderService @ 6 e5e91e4 byName method into com. Gongj. PopulateBean. 6 e5e91e4 OrderService @ com.gongj.populateBean.OrderService@6e5e91e4Copy the code

This is because both the setOrder method and the setOrderService method have arguments of type OrderService.

constructor

Add a parameterized constructor to the previous UserService. Change byType to constructor

  • The sample
public UserService(OrderService orderService1,OrderService orderService2){
		System.out.println("Two parameter construction injection" + orderService1 +"= = ="+ orderService2); orderService = orderService1; } the result: Two parameters have a reference structure into com. Gongj. PopulateBean. 46 fbb2c1 OrderService @. = = = com gongj. PopulateBean. 46 fbb2c1 OrderService @ com.gongj.populateBean.OrderService@46fbb2c1Copy the code

If there are more than one constructor, the constructor with more arguments is called.

Annotation way

As Spring has evolved, many annotations have replaced XML configurations, such as @bean for < Bean >, @scope for Scope, @Autowired for Autowire, and so on. We’ll use the @AutoWired injection method for this article.

If you use the @autowired annotation, you can omit the set method above.

public class UserService {
	@Autowired
	OrderService orderService;

	public void test(a){ System.out.println(orderService); }}Copy the code

And you can see that in the UserService class it becomes very concise. Because there are using the ClassPathXmlApplicationContext this container, so we need to increase in XML configuration, as follows:

<context:annotation-config/>
Copy the code

This configuration activates beans that have already been registered with the Spring container, which we have already registered with Spring via XML.

Of course, you might think why register beans in XML when I’m already using annotations? If you feel this way, you can add the configuration to the XML to specify the scan package path as follows:

<context:component-scan base-package="com.gongj.populateBean"/>
Copy the code

What it does: has all the functionality of

and can also scan classes under the specified package and register them as beans.

Add the Component annotation to the class to register it with the Spring container

@Component
public class UserService {}
Copy the code

If you don’t want to have an XML file at all, you can do this:

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppApplication.class);
Copy the code

Switch the container, the use of AnnotationConfigApplicationContext.

Add AppApplication class, the content of the class is as follows:

@ComponentScan(basePackages = {"com.gongj.populateBean"})
public class AppApplication {}Copy the code

Use the ComponentScan annotation to specify the package path for scanning. The above code is no longer outlined.

OK! Let’s get back to the @autowired annotation. After using the Autowired annotation, we can omit the set method and increase our fishing time (* ~)!

  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.