Spring automatic assembly (13)

public class Customer 
{
	private Address address;
}
public class Address 
{
	private String fulladdress;
}

1. Default assembly, designated by ref

<bean id="customer" class="org.qww.common.Customer">
        <property name="person" ref="person" />
</bean>
	<bean id="person" class="org.qww.common.Person" />

This configuration method is the default value.

2. Assembly name, byName

<bean id="customer" class="org.qww.Customer" autowire="byName" />
<bean id="address" class="org.qww.common.Address" >
	<property name="fulladdress" value="YiLong Road, CA 188" /> 
</bean>

customer attribute names and the same address, as address will bean injection, when the Address is not the same name can not be injected, is as follows:

<bean id="customer" class="org.qww.common.Customer" autowire="byName" />
<bean id="addressABC" class="org.qww.common.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
</bean>

Customer then the output is:

Customer [address=null]

3. injection, byType by type

<bean id="customer" class="org.qww.Customer" autowire="byType" />
<bean id="address11" class="org.qww.common.Address" >
	<property name="fulladdress" value="YiLong Road, CA 188" /> 
</bean>

Type and the type of bean address11 Customer address in the same address by injection into the injection type will address11, Customer output is:

Customer [address=Address [fulladdress=YiLong Road, CA 188]]

When the container there are two types of Address bean, by type of automatic configuration will throw an exception:

<bean id="customer" class="org.qww.Customer" autowire="byType" />
<bean id="address11" class="org.qww.common.Address" >
	<property name="fulladdress" value="YiLong Road, CA 188" /> 
</bean>
<bean id="address12" class="org.qww.common.Address" >
	<property name="fulladdress" value="YiLong Road, CA 188" /> 
</bean>

Initialization container, throws the following exception:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: 
...
No unique bean of type [com.yiibai.common.Ability] is defined: 
expected single matching bean but found 2: [steal, invisible]; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 

4. By constructor injection, but also by the injection type, constructor

The default constructor calls the injected bean, as follows:

public class SpringHelloWord {
	private Hello hello;
	private String say;
	private Hello2 hello2;
	public String getSay() {
		return say;
	}
	public void setSay(String say) {
		this.say = say;
	}
	public SpringHelloWord() {
		System.out.println("1");
	}
	public SpringHelloWord(Hello hello) {
		this.hello=hello;
		System.out.println("2");
	}
	public SpringHelloWord(Hello hello,String say) {
		this.hello=hello;
		this.say=say;
		System.out.println("3");
	}
	public SpringHelloWord(Hello hello,Hello2 hello2) {
		this.hello=hello;
		this.hello2=hello2;
		System.out.println("4");
	}
	public void say() {
		System.out.println("Spring Hello Word");
	}
	public Hello getHello() {
		return hello;
	}
	public void setHello(Hello hello) {
		this.hello = hello;
	}
	public Hello2 getHello2() {
		return hello2;
	}
	public void setHello2(Hello2 hello2) {
		this.hello2 = hello2;
	}
}

When the configuration is as follows:

	<bean id="helloBean" class="Spring.SpringHelloWord" autowire="constructor">
	</bean>
	<bean id="hello" class="Spring.Hello">
	</bean>

When acquiring helloBean, output 2, the call is SpringHelloWord (Hello hello) constructor.

When the configuration is as follows:

	<bean id="helloBean" class="Spring.SpringHelloWord" autowire="constructor">
	</bean>
	<bean id="hello" class="Spring.Hello">
	</bean>
	<bean id="hello2" class="Spring.Hello2">
	</bean>

Call is SpringHelloWord (Hello hello, Hello2 hello2).

Because the constructor injection is invoked by the type of injection, so when there are two types of injected bean will throw an exception.

5.autodetect

If the default constructor is found, use the "automatic assembly with the structure"; otherwise, use the "by type automatic assembly."

Guess you like

Origin blog.csdn.net/qq_36831305/article/details/89343434