spring四种依赖注入方式

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>


java代码如下:

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>


java代码如下:

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" />
两者区别:一个是静态工厂方法注入,一个是实例方法工厂注入



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值