What are the common injection methods of spring?

Keywords: Programming Spring xml encoding Java

1. Configuration in xml

  • bean declaration and registration

< bean > node register bean
Factory bean parameter of < bean > node refers to factory bean, and factory method parameter specifies factory method

 

  • bean injection

< property > nodes are injected in set mode
< constructor Arg > node injection with construction method

 

Measured code

maven pom file

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>

 

a) < bean > + < property >, set method injection

class Bowl

package constxiong.interview.inject;

public class Bowl {

	public void putRice() {
		System.out.println("take cooked rice out of a cooker into a bowl...");
	}

}

class Person

package constxiong.interview.inject;

public class Person {

	private Bowl bowl;
	
	public void eat() {
		bowl.putRice();
		System.out.println("Start eating...");
	}

	public void setBowl(Bowl bowl) {
		this.bowl = bowl;
	}
	
}

spring profile

<?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="bowl" class="constxiong.interview.inject.Bowl" />
	
	<bean id="person" class="constxiong.interview.inject.Person">
		<property name="bowl" ref="bowl"></property>
	</bean>
	
</beans>

Test class

package constxiong.interview.inject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_inject.xml");
		Person person = (Person)context.getBean("person");
		person.eat();
	}
}

 

b) modify it to configuration file and class Person, and use construction method to inject the < bean > + < constructor Arg > node

class Person

package constxiong.interview.inject;
 
public class Person {
 
	private Bowl bowl;
	
	public Person(Bowl bowl) {
		this.bowl = bowl;
	}
	
	public void eat() {
		bowl.putRice();
		System.out.println("Start eating...");
	}
	
}

spring profile

<?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="bowl" class="constxiong.interview.inject.Bowl" />
	
	<bean id="person" class="constxiong.interview.inject.Person">
		<constructor-arg name="bowl" ref="bowl"></constructor-arg>
	</bean>
	
</beans>

 

c) The < bean > node factory method parameter specifies the static factory method

Factory class, static factory method

package constxiong.interview.inject;
 
public class BowlFactory {
 
	public static final Bowl getBowl() {
		return new Bowl();
	}
	
}

spring profile

<?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="bowl" class="constxiong.interview.inject.BowlFactory" factory-method="getBowl"/>
	
	<bean id="person" class="constxiong.interview.inject.Person">
		<constructor-arg name="bowl" ref="bowl"></constructor-arg>
	</bean>
	
</beans>

 

d) Non static factory method, factory bean and factory method need to be specified

Factory class, non static factory method

package constxiong.interview.inject;
 
public class BowlFactory {
 
	public Bowl getBowl() {
		return new Bowl();
	}
	
}

configuration file

<?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="bowlFactory" class="constxiong.interview.inject.BowlFactory"></bean>   
	<bean id="bowl" factory-bean="bowlFactory" factory-method="getBowl"/>
	
	<bean id="person" class="constxiong.interview.inject.Person">
		<constructor-arg name="bowl" ref="bowl"></constructor-arg>
	</bean>
	
</beans>

 

2, annotations

  • bean declaration and registration

@Component //Register all bean s
@Controller //Register control layer bean s
@Service //Register service layer bean s
@Repository //Register the bean s of dao layer

 

  • bean injection

@Autowired works on construction methods, fields, and methods, and is often used on member variable fields.
@Autowired + @Qualifier injection, specifying the bean name
@Resource JDK comes with annotation injection, which can specify bean name, type, etc

 

Test code

e) spring configuration file, set annotation scanning directory

<?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">
        
	<context:component-scan base-package="constxiong.interview" />
	
</beans>

 

class Bowl

package constxiong.interview.inject;
 
import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Controller;
//import org.springframework.stereotype.Repository;
//import org.springframework.stereotype.Service;
 
@Component //Register all bean s
//@Controller / / register the bean of control layer
//@Service / / register service layer bean s
//@Repository / / register dao layer bean s
public class Bowl {
 
	public void putRice() {
		System.out.println("take cooked rice out of a cooker into a bowl...");
	}
 
}

 

class Person

package constxiong.interview.inject;
 
//import javax.annotation.Resource;
//
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
 
@Component //Register all bean s
//@Controller / / register the bean of control layer
//@Service / / register service layer bean s
//@Repository / / register dao layer bean s
public class Person {
 
	@Autowired
//	@Qualifier("bowl")
//	@Resource(name="bowl")
	private Bowl bowl;
 
	public void eat() {
		bowl.putRice();
		System.out.println("Start eating...");
	}
	
}

 

Test similar to above

 

a. The test results of b, c, d and e are all ok

Rich rice...
Start eating


Original link
 

Posted by cornelombaard on Mon, 30 Dec 2019 06:13:24 -0800