How can spring automatically assemble bean s?

Keywords: Programming Spring xml Attribute encoding

The autowire parameter of the < bean > node in the spring configuration file can control how beans are automatically assembled

  • Default - the default is the same as "no"
  • no - does not auto assemble, requires < ref / > nodes or parameters
  • byName - assemble by name
  • byType - assemble by type
  • Constructor - assemble according to constructor

 

Document interpretation

Attribute : autowire
Controls whether bean properties are "autowired". This is an 
 automagical process in which bean references don't need to 
 be coded explicitly in the XML bean definition file, but rather 
 the Spring container works out dependencies. The effective 
 default is "no". There are 4 modes: 1. "no" The traditional 
 Spring default. No automagical wiring. Bean references must 
 be defined in the XML file via the <ref/> element (or "ref" 
 attribute). We recommend this in most cases as it makes 
 documentation more explicit. Note that this default mode also 
 allows for annotation-driven autowiring, if activated. "no" 
 refers to externally driven autowiring only, not affecting any 
 autowiring demands that the bean class itself expresses. 2. 
 "byName" Autowiring by property name. If a bean of class Cat 
 exposes a "dog" property, Spring will try to set this to the 
 value of the bean "dog" in the current container. If there is no 
 matching bean by name, nothing special happens. 3. "byType" 
 Autowiring if there is exactly one bean of the property type in 
 the container. If there is more than one, a fatal error is raised, 
 and you cannot use byType autowiring for that bean. If there is 
 none, nothing special happens. 4. "constructor" Analogous to 
 "byType" for constructor arguments. If there is not exactly one 
 bean of the constructor argument type in the bean factory, a 
 fatal error is raised. Note that explicit dependencies, i.e. 
 "property" and "constructor-arg" elements, always override 
 autowiring. Note: This attribute will not be inherited by child 
 bean definitions. Hence, it needs to be specified per concrete 
 bean definition. It can be shared through the 'default-autowire' 
 attribute at the 'beans' level and potentially inherited from 
 outer 'beans' defaults in case of nested 'beans' sections (e.g. 
 with different profiles).
 
Data Type : string
Default Value : default
Enumerated Values : 
	- default
	- no
	- byName
	- byType
	- constructor

 

Code example

1. no mode

For spring configuration files to inject bean s with ref parameters, there must be an object setter method, which is the setFr method of Person.

No < property name = "fr" ref = "fr" >.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="person" class="constxiong.interview.assemble.Person" autowire="no">
		<property name="fr" ref="fr"></property>
	</bean>
	<bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
		
</beans>

 

Fishing rod bean

package constxiong.interview.assemble;
 
 
/**
 * Fishing rod
 * @author ConstXiong
 * @date 2019-07-17 09:53:15
 */
public class FishingRod {
 
	/**
	 * Being used
	 */
	public void used() {
		System.out.println("Go fishing...");
	}
}

 

Human bean

package constxiong.interview.assemble;
 
 
/**
 * people
 * @author ConstXiong
 * @date 2019-07-17 09:54:56
 */
public class Person {
 
	private FishingRod fr;
	
	/**
	 * Go fishing
	 */
	public void fish() {
		fr.used();
	}
	
	public void setFr(FishingRod fr) {
		this.fr = fr;
	}
	
}

 

Test code

package constxiong.interview.assemble;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AssembleTest {
 
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_assemble.xml");
		Person person = (Person)context.getBean("person");
		person.fish();
	}
	
}

 

2. byName also needs the corresponding setter method to inject

Modify spring profile autowire="byName"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="person" class="constxiong.interview.assemble.Person" autowire="byName"></bean>
	<bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
		
</beans>
 

 

3. byType also needs the corresponding setter method to inject

Modify spring profile autowire="byType"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="person" class="constxiong.interview.assemble.Person" autowire="byType"></bean>
	<bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
		
</beans>

Other invariable

 

4. constructor does not need setter method, but needs to inject bean through construction method

Modify the spring configuration file autowire="byType"

The Person class removes the setFr method and adds the constructor to set the fr property

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
	    http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
	<bean id="person" class="constxiong.interview.assemble.Person" autowire="constructor"></bean>
	<bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
		
</beans>
package constxiong.interview.assemble;
 
 
/**
 * people
 * @author ConstXiong
 * @date 2019-07-17 09:54:56
 */
public class Person {
 
	private FishingRod fr;
	
	public Person(FishingRod fr) {
		this.fr = fr;
	}
	
	/**
	 * Go fishing
	 */
	public void fish() {
		fr.used();
	}
	
}

 

1. The test results of 2, 3 and 4 are consistent and printed

Go fishing...


Original link
 

Posted by jagat21 on Fri, 03 Jan 2020 18:08:32 -0800