be based on Spring@Autowired Annotation and automatic assembly

1 method of configuration file

When we write code for the spring framework. Always follow the rule that all beans injected into spring are recommended to be defined as private domain variables. And write the get and set methods.

Boss has two attributes of office and car types:

Listing 3 Boss. java

System. out. Println must implement the toString method

We declare office and car as beans in the spring container and inject them into the boss bean: the following is the configuration file beans xml:

Listing 4 beans. XML configures the above three classes as beans

When we run the following code, the console will correctly type the boss information:

Listing 5 Test class: annoioctest java

This shows that the spring container has correctly completed the creation and assembly of beans.

2 @Autowired

Spring 2.5 introduces @ Autowired annotation, which can label class member variables, methods and constructors to complete automatic assembly. Use @ Autowired to eliminate set and get methods.

To achieve our goal of streamlining procedures. This needs to be done:

*In ApplicationContext Add in XML:

Spring parses @ Autowired through a beanpostprocessor, so to make @ Autowired work, you must declare the autowiredannotationbeanpostprocessor bean in the spring container in advance.

*Modify the method of the bean originally injected into the Spirng container.

Add the label @ Autowired on the domain variable and remove the corresponding get and set methods

Listing 6 Boss annotated with @ Autowired java

*In ApplicationContext Remove the < porpery > tag from the original reference in XML.

In this way, when the spring container starts, the Autowired annotation beanpostprocessor will scan all beans in the spring container. When @ Autowired annotation is found in the bean, it will find the bean matching it (matching by type by default) and inject it into the corresponding place.

According to the above configuration, spring will directly use the java reflection mechanism to automatically inject the two private member variables of car and office in boss. Therefore, after using @ Autowired for member variables, you can delete their setter methods (setcar() and setoffice()) from the boss.

Of course, you can also annotate the method or constructor through @ Autowired. If the constructor has two input parameters, bean1 and bean2 respectively, @ Autowired will find beans matching their types and create countryservice beans as input parameters of countryservice (bean1, bean1, bean2, bean2). Look at the following code: pair method

At this time, @ Autowired will find the beans of the input parameter type of the marked method and call the method to inject these beans automatically. The constructor is annotated with the following methods:

Since the boss () constructor has two input parameters, car and office, @ Autowired will find beans matching their types and create boss beans as input parameters of boss (car, office).

The above article is based on Spring@Autowired Annotation and automatic assembly details are all the contents shared by Xiaobian. I hope we can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>