spring commonly used injection method What?

1, xml configuration
bean affirmed register
<bean> node registration bean
<bean> node parameter refers to factory-bean plant bean, factory-method parameter specifies the factory method
bean implantation
<property> node using the set injection mode
<constructor- arg> node uses constructor injection
Found codes
maven pom file 
<dependency>
<the groupId> org.springframework </ the groupId>
<the artifactId> Spring-Beans </ the artifactId>
<Version> 4.2.4.RELEASE </ Version>
</ dependency >
<dependency>
<the groupId> org.springframework </ the groupId>
<the artifactId> Spring-context </ the artifactId>
<Version> 4.2.4.RELEASE </ Version>
</ dependency>
A, <the bean> + <Property>,set方法注入
class Bowl
package constxiong.interview.inject;
public class Bowl {
public void putRice() {
System.out.println("盛饭...");
}
}
class Person
package constxiong.interview.inject;
public class Person {
private Bowl bowl;
public void eat() {
bowl.putRice();
System.out.println("开始吃饭...");
}
public void setBowl(Bowl bowl) {
this.bowl = bowl;
}
}
spring 配置文件
<?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>
测试类
package constxiong.interview.inject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectTest {
static void main public (String [] args) {
the ApplicationContext the ClassPathXmlApplicationContext context = new new ( "spring_inject.xml");
the Person Person = (the Person) context.getBean ( "Person");
person.eat ();
}
}
B, modified and for the profile class Person, <bean> + < constructor-arg> nodes constructor injection
class the Person
Package constxiong.interview.inject;
public class the Person {
Private Bowl Bowl;
public the 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>factory class, static factory methodC, <the bean> node factory-method parameter specifies a static factory method
</ Beans>


package constxiong.interview.inject;
public class BowlFactory {
public static final Bowl getBowl() {
return new Bowl();
}
}
spring 配置文件
<?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">;
<The bean ID = "Bowl" class = "constxiong.interview.inject.BowlFactory" Factory-Method = "getBowl" />
<the bean ID = "Person" class = "constxiong.interview.inject.Person">
<constructor- Arg name = "Bowl" REF = "Bowl"> </ constructor-Arg>
</ bean>
</ Beans>
D, non-static factory method, specify factory bean and factory method
factory class, non-static factory method
package constxiong. interview.inject;
public class BowlFactory {
public Bowl getBowl () {
return new new Bowl ();
}
}
profiles
<? 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>bean // registration control layer @Controller@Component // register all beanbean affirmed registration2, annotation
</ Beans>




@Service // registered service layer bean
the @Repository // registered dao layer bean
injected into the bean
@Autowired act on the construction method, AxiTrader rebate www.fx61.com/brokerlist/axitrader.html fields, methods, commonly used in the member variables field above.
@Autowired + @Qualifier injection, the specified name of the bean
@Resource JDK injection own annotations, and can specify the name of the bean type of
test code
E, spring profile, scans the directory provided annotations
<? 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.
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> ;
<context: Component Base-Package-Scan =" 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
// // Register the Controller @ control layer bean
// service // @ bean registered service layer
bean // @ Repository // registered dao layer
public class Bowl {
public void putRice () {
System.out.println ( "Shengfan ...");
}
}
the Person class
Package Penalty for 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
// // Register the Controller @ control layer bean
// service // @ bean registered service layer
bean // @ Repository // registered dao layer
public class Person { br /> @ Autowired is
// @Qualifier ( "Bowl")
// @Resource (name = "Bowl")
Private Bowl Bowl;
public void EAT () {
bowl.putRice ();
System.out.println ( "start eat ... ");
}
}
test class above
A, B, C, D, E test results ok
Shengfan ...
I began to eat ...

Guess you like

Origin blog.51cto.com/14511863/2438676