Spring configuration uses-Bean automatic assembly _spring

Source: Internet
Author: User
Basic concepts

Automatic assembly (Autowire) means that the entire assembly process is automatically completed and is no longer specified by US manually.

There are several ways to assemble automatically in Spring: The name specification no means no automatic assembly, at which point you must manually specify the dependent bean byname to automatically assemble based on the property name, which checks the entire container for the same Bean identity as the property name. Bytype is automatically assembled based on the type of the attribute, and the entire container is checked for the same Bean as the property type by default. constructor is similar to Bytype, and is also automatically assembled according to type, where the matching is the type of the constructor parameter, mainly used for constructor (construct) injection of the Default-autowire attribute of the default and beans label about AU Todetect to determine whether the structure of the Bean construction parameters, if there is a constructor automatic assembly, otherwise use Bytype automatic assembly. Currently obsolete, Spring 3.0 is no longer used

This means that the automatic assembly is not used, and the manual configuration is used. Here, take the construction injection as an example, introduce the process of manual assembly: first the Bean to be defined

public class cat{} public

class Animals {public
    Animals (cat cat) {}
}
defined in the XML configuration file
<bean id= "Cat" class= "Com.demo.Cat"/>

<!--① Manual assembly, you must manually specify the dependent bean--> <bean id= "for the member variable
Animals "class=" com.demo.Animals ">
    <constructor-arg name=" cat "ref=" cat "/>
</bean>

<!--② also means that at this time Autowire attribute is no, same as ① function--> <bean id= "Animals"
class= "com.demo.Animals" autowire= "no" >
  <constructor-arg name= "Cat" ref= "cat"/>
</bean>
To create a container validation
public static void Main (string [] args) {
    string location = ...
    ApplicationContext factory = new Filesystemxmlapplicationcontext (location);
}
ByName

Automatic assembly based on the property name, which checks the entire container for the same Bean identity (including Id,name,alias) as the property name, and is only applicable to setter injection. Define Bean

public class cat{} public

class Animals {
    private cat cat;
    Omit Sertter/getter ...
}
defined in the XML configuration file
<bean id= "Cat" class= "Com.demo.Cat"/>

<!--reduces configuration workloads compared to manual assembly--> <bean id=
"Animals" Com.demo.Animals "autowire=" ByName "/>
Call validation
String location = ...
ApplicationContext factory = new Filesystemxmlapplicationcontext (location);
Animals Animals = (Animals) factory.getbean ("Animals");
System.out.println (Animals.getcat ());

It should be noted that when a bean that identifies a match to the property name does not exist in the container, the container does not throw an exception by default, but instead treats the property as NULL, which is the default output when the Get method is invoked. This feature is also applicable to all setter injections. Bytype

Automatically assembled based on the type of the property, the default checks the entire container for the same Bean as the property type, which applies only to setter injection.

Similar to the ByName automatic assembly method, the configuration is simply to replace byname with Bytype, as follows:

<bean id= "Animals" class= "com.demo.Animals" autowire= "Bytype"/>

But there's a huge problem with this approach, and we know that ByName is looking for a bean in the container that is the same as the property name, because the Spring container does not allow two identical beans to exist, so matching the result by default has only one matching bean.

But unlike Bytype, the Spring container does not limit the existence of the same type of beans, as long as their identities are different. Here's how to do this: define the Bean

public class cat{} public

class Animals {
    private cat cat;
    Omit Sertter/getter ...
}
defined in the XML configuration file
<!--① exist with multiple types, identifying different beans-->
<bean id= "CAT1" class= "Com.demo.Cat"/> <bean id= "cat2" class= "
Com.demo.Cat "/>

<!--② even if you do not specify an identity, Spring automatically generates different identities, acting as ①-->
<bean class=  " Com.demo.Cat "/>
<bean  class= "Com.demo.Cat"/> <bean id= "Animals class="

com.demo.Animals "autowire=" Bytype "/>
To create a container validation

This throws the following exception message:

Org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying Bean of type ...

Does this mean that the same type of beans cannot exist in a container when automatic assembly is used Bytype, and the answer, of course, is no, Spring has introduced a campaign mechanism to solve the problem. 1.primary

When a bean identifies primary as true, it represents itself as the primary bean, which defaults to flase. When more than one Bean of the same type is present, it is selected automatically when assembled.

<!--correct-->
<bean  class= "Com.demo.Cat" primary= "true"/> <bean class= "  com.demo.Cat" />

<!--error-->
<bean  class= "Com.demo.Cat" primary= "false"  /> <bean " Com.demo.Cat "/>

<!--error-->
<bean class=" Com.demo.Cat "  primary=" true "/>
< Bean  class= "Com.demo.Cat" primary= "true"/>
2.autowire-candidate

As with primary, when a bean identifies autowire-candidate as false, it means that it is no longer a candidate for the bean, but rather the primary bean, which defaults to True,defalut. When multiple beans of the same type exist, automatic assembly selects the bean with the property false.

<!--correct-->
<bean  class= "Com.demo.Cat" autowire-candidate= "false"/> <bean "class=  " Com.demo.Cat "/>

<!--error--> <bean class=" Com.demo.Cat "autowire-candidate=  " true "/>
<bean  class= "Com.demo.Cat"/>

<!--error--> <bean class=  "Com.demo.Cat" autowire-candidate= "Default"/>
<bean  class= "Com.demo.Cat"/>

<!--error-->
< Bean  class= "Com.demo.Cat" autowire-candidate= "ture"/> <bean class=  "Com.demo.Cat" autowire-candidate= "Default"/>
Constructor

Similar to Bytype, the same is automatically assembled according to the type, where the matching is the type of the constructor parameter, mainly used for constructor (construction) injection. Define Bean

public class cat{} public

class Animals {public
    Animals (cat cat) {}
}
defined in the XML configuration file
<bean  class= "Com.controller.Cat"  />

<bean id= "Animals" class= "com.demo.Animals" autowire= "Constructor"/>

Similarly, this method also has the same type of Bean hidden trouble, the solution is consistent with bytype, through primary, Autowire-candidate.

It should be noted that, unlike ByName and Bytype, this method employs a constructed injection rather than a setter injection. If no parameterless constructor is defined in the bean, an exception is thrown if no matching Bean is found during automatic assembly. Rather than byname, bytype treat it as null. Default

Related to the Default-autowire property of the bean's parent tag beans, consistent with autowire, which also has values such as no, default, Bytype, ByName, constructor.

Assuming that the default-autowire of beans is bytype, it means that the Autowire for default in the Bean label defaults to the injection method of Bytype.

The specific configuration forms are as follows:

<beans xmlns= "Http://www.springframework.org/schema/beans" 
    xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " 
    xsi:schemalocation="
        Http://www.springframework.org/schema/beans 
        http:// Www.springframework.org/schema/beans/spring-beans-3.1.xsd " 
    default-autowire=" Bytype ">

    <bean  class= "Com.controller.Cat"  />

    <!--at this time, Bytype automatic assembly mode--> <bean id= "Animals"
    Com.demo.Animals "autowire=" "Default"/>
AutoDetect

Determine whether the structure of the Bean construction parameters, if the use of constructor automatic assembly, otherwise use Bytype automatic assembly.

After Spring 3.0 The way has been abandoned, and this is no longer explored here. If you want to configure the injection method, you need to replace the 3.0 XSD with 2.5 xsd, or you will get an error.

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.