1.set注入:
spring-config-dao.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd"
default-autowire="byName">
<bean id="helloService" class="actions.dispatcher.exec.depend.HelloServiceImpl">
</bean>
<bean id="setDependDemo" class="actions.dispatcher.exec.depend.SetDependDemo" init-method="testSetDepend">
<property name="helloService" ref="helloService"></property>
</bean>
</beans>
public class SetDependDemo {
private HelloServiceImpl helloService;
public void setHelloService(HelloServiceImpl helloService) {
this.helloService = helloService;
}
private void testSetDepend(){
helloService.sayHello();
}
}
public class HelloServiceImpl implements HelloService {
public void sayHello() {
System.out.println("sayHello");
}
}
public interface HelloService {
public void sayHello();
}
web应用启动后,会打印日志sayHello
2.构造器注入:
spring-config-dao.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd"
default-autowire="byName">
<bean id="helloService" class="actions.dispatcher.exec.depend.HelloServiceImpl">
</bean>
<bean id="setDependDemo" class="actions.dispatcher.exec.depend.SetDependDemo" init-method="testSetDepend">
<constructor-arg index="0" type="java.lang.String" value="xxxxx"></constructor-arg>
<constructor-arg ref="helloService"></constructor-arg>
</bean>
</beans>
java代码如下:
public class SetDependDemo {
private HelloServiceImpl helloService;
private String name;
public SetDependDemo(String name, HelloServiceImpl helloService) {
this.name = name;
this.helloService = helloService;
}
private void testSetDepend() {
helloService.sayHello();
System.out.println(this.name);
}
}
web应用启动后,日志会打印sayHello和xxxxx
3.实例工厂方法注入:
spring-config--dao.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd"
default-autowire="byName">
<!--工厂类bean-->
<bean id="carFactory" class="actions.dispatcher.exec.depend.CarFactory" />
<!--factory-bean指定工厂类Bean,factory-method指定工厂类Bean创建Bean的工厂方法-->
<bean id="car" factory-bean="carFactory" factory-method="createQiRuiCar" />
<bean id="buyCar" class="actions.dispatcher.exec.depend.BuyCar" init-method="buyCar">
<property name="car" ref="car"></property>
</bean>
</beans>
java代码如下:
public class CarFactory {
//创建Car的工厂方法
public Car createQiRuiCar(){
Car car = new Car();
car.setBrand("奇瑞QQ");
return car;
}
}
public class BuyCar {
private Car car;
public void setCar(Car car) {
this.car = car;
}
public void buyCar() {
System.out.println(car.getBrand());
}
}
web应用启动后,日志会打印奇瑞QQ,
5.静态工厂方法注入:
spring-config.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd"
default-autowire="byName">
<bean id="buyCar" class="actions.dispatcher.exec.depend.BuyCar" init-method="buyCar">
<property name="staticCar" ref="staticCar"></property>
</bean>
<bean id="staticCar" class="actions.dispatcher.exec.depend.CarFactory" factory-method="createStaticCar"></bean>
</beans>
public class CarFactory {
public static Car createStaticCar(){
Car car = new Car();
car.setBrand("大众");
return car;
}
}
public class BuyCar {
private Car staticCar;
public void setCar(Car car) {
this.car = car;
}
public void buyCar() {
System.out.println(staticCar.getBrand());
}
public void setStaticCar(Car staticCar) {
this.staticCar = staticCar;
}
}
public class Car {
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
}
注意:
<bean id="staticCar" class="actions.dispatcher.exec.depend.CarFactory" factory-method="createStaticCar"></bean>
<bean id="car" factory-bean="carFactory" factory-method="createQiRuiCar" />
两者区别:一个是静态工厂方法注入,一个是实例方法工厂注入