Spring strategy learning notes (1.11) -- automatically assemble beans with xml configuration

Source: Internet
Author: User

When a bean needs to access another bean, it can be displayed as a reference to assemble it. However, if your container can automatically assemble beans, you can save the trouble of manual configuration and assembly.

The Spring IoC container can help you automatically assemble beans. You only need to specify the automatic assembly mode in the autowire attribute of <bean>. The following is the automatic assembly Mode Supported by spring 3.x.

No: automatic assembly is not performed. Assembly dependency must be displayed

 Byname: assemble a bean with the same name for each bean attribute.

Bytype: A bean whose assembly type is compatible with each bean attribute. If more than one bean is found, an unsatisfieddependencyexception is thrown.

Constructor: For each constructor parameter, first find the bean compatible with the parameter. Then select the constructor with the maximum number of matching parameters. If there is ambiguity, an unsatisfieddependencyexception will be thrown.

The default mode is no, but you can modify the default-autowire attribute of the <beans> root element. This default mode will be overwritten by the mode specified by bean itself.

Note: Spring 3.x does not support autodetect automatic assembly mode. Use @ autowired.Replace annotation.

Although the automatic assembly function is very powerful, the cost is to reduce the readability of bean configuration. Because automatic assembly is executed by spring at runtime, you cannot obtain the bean assembly method from the bean configuration file.We recommend that you only apply automatic assembly to applications that are dependent on complex components.ProgramMedium.

(1) automatic assembly by type

You can set the autowire attribute of the bean sequencegeneratorBytypeThe prefixgenerator attribute is not set. Then spring will try to assemble beans compatible with prefixgenerator. In this example, dateprefixgenerator is automatically assembled.

 
<Bean id = "sequencegenerator" class = "com. jackie. codeproject. springrecipesnote. springioc. sequencegenerator "autowire =" bytype "> <property name =" initial "value =" 100000 "/> <property name =" suffix "value =" A "/> </bean> <bean id = "dateprefixgenerator" class = "com. jackie. codeproject. springrecipesnote. springioc. dateprefixgenerator "> <property name =" pattern "value =" yyyymmdd "/> </bean>

The main problem with assembly by type is that sometimesThere is more than one bean compatible with the target type in the IOC container.. In this case, spring will not be able to determine which bean is best suited to this attribute, so an exception will be thrown.

 
<Bean id = "sequencegenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. sequencegenerator "autowire =" bytype "> <property name =" initial "value =" 100000 "/> <property name =" suffix "value =" A "/> </bean> <bean id = "dateprefixgenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. dateprefixgenerator "> <property name =" pattern "value =" yyyymmdd "/> </bean> <bean id =" yearprefixgenerator "class =" com. codeproject. jackie. springrecipesnote. springioc. yearprefixgenerator "> <property name =" pattern "value =" YYYY "/> </bean>

Run the program,An unsatisfieddependencyexception is thrown.:

 
Org. springframework. beans. factory. unsatisfieddependencyexception: Error creating bean with name 'sequencegenerator' defined in class path resource [applicationcontext. XML]: unsatisfied dependency expressed through bean property 'prefixgenerator': no qualifying bean of Type [COM. codeproject. jackie. springrecipesnote. springioc. prefixgenerator] is defined: Expected single matching bean but found 2: dateprefixgenerator, yearprefixgenerator; Nested exception is Org. springframework. beans. factory. nouniquebeandefinitionexception: no qualifying bean of Type [COM. codeproject. jackie. springrecipesnote. springioc. prefixgenerator] is defined: Expected single matching bean but found 2: dateprefixgenerator, yearprefixgenerator

(2) automatic assembly by name

Byname is another automatic assembly mode. Spring will try to assemble a bean with the same attribute name instead of a compatible type, which can solve the automatic assembly problem by type.

 
<Bean id = "sequencegenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. sequencegenerator "autowire =" byname "> <property name =" initial "value =" 100000 "/> <property name =" suffix "value =" A "/> </bean> <bean id = "prefixgenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. dateprefixgenerator "> <property name =" pattern "value =" yyyymmdd "/> </bean>

Automatic Assembly by name does not work in any situation. Sometimes the name and attribute of the target bean cannot be the same. In practice, it is often necessary to specify ambiguous dependencies while maintaining automatic assembly of other dependencies. This means that the display assembly and automatic assembly are mixed.

(3) automatic assembly according to construction program

The constructor automatic assembly mode is more complex than the bytype mode. For beans with a single constructor, spring will try to assemble a type-compatible bean for each constructor parameter. For beans with multiple constructors, spring first tries to find a type-compatible bean for each parameter of each constructor, and then selects the constructor with the maximum number of matching parameters.

Assume that sequencegenerator has a default constructor and a constructor with prefixgenerator parameters.

 
Public sequencegenerator () {} public sequencegenerator (prefixgenerator) {This. prefixgenerator = prefixgenerator ;}

In this case, the second constructor matches and is selected, because spring can find a bean compatible with prefixgenerator.

<Bean id = "sequencegenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. sequencegenerator "autowire =" constructor "> <property name =" initial "value =" 100000 "/> <property name =" suffix "value =" A "/> </bean> <bean id = "dateprefixgenerator" class = "com. codeproject. jackie. springrecipesnote. springioc. dateprefixgenerator "> <property name =" pattern "value =" yyyymmdd "/> </bean>

If this Assembly mode is used, constructor ambiguity should be avoided.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.