spring_01<bean>的两种注入方式

spring的核心思想之一:控制反转也就是依赖注入。在spring的世界里,所有的java对象都是一个bean。程序中类和类之间的互相调用,不再是以往的new的创建。而是依靠spring容器通过反射读取xml配置文件中的一个个的bean来生成一个个的具体的实现类,供程序使用。同时将编程方式提升至面向接口编程。在一定程度上降低了程序的耦合。同时面向接口对后期功能的扩展以及修改降低了难度。只需要修改或增加实现类即可。

下面展示了spring注入bean的两种方式:设置注入、构造注入。

(1)设置注入。
接口Person.java

package com.demo.interfaces;

public interface Person {
    public void drive();
}

实现类 Chinese.java

package com.demo.impls;

import com.demo.interfaces.Person;
import com.demo.interfaces.Shop;

public class Chinese implements Person{

    private Shop shop;

    public void setShop(Shop shop) {
        this.shop = shop;
    }

    @Override
    public void drive() {

        System.out.println(shop.work());    

    }

}


接口Shop.java
package com.demo.interfaces;

public interface Shop {
public String work();
}

实现类SShop.java

package com.demo.impls;

import com.demo.interfaces.Shop;

public class Sshop implements Shop{

@Override
public String work() {
    return "卖汽车";
}

}

spring 配置文件beans.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="chinese" class="com.demo.impls.Chinese">
        <property name="shop" ref="sshop"></property>
    </bean>

    <bean id="sshop" class="com.demo.impls.Sshop"></bean>

</beans>

测试类Test.java
public static void main(String[] args) {
        ApplicationContext ctx = new    ClassPathXmlApplicationContext("beans.xml");
        Person p =  ctx.getBean("chinese",Person.class);
        p.drive();
}

最后输出结果:卖汽车。

这是第一种设值注入方式,也就是根据

<bean id="chinese" class="com.demo.impls.Chinese">
        <property name="shop" ref="sshop"></property>
    </bean>

中的name来指定调用Chinese实现类中的哪个set方法,ref指定set方法的传入参数。

(2)构造注入

接口Person.java

package com.demo.interfaces;

public interface Person {
    public void drive();
}

实现类 Chinese.java

package com.demo.impls;

import com.demo.interfaces.Person;
import com.demo.interfaces.Shop;

public class Chinese implements Person{

    private Shop shop;

    public Chinese(Shop shop){
        this.shop = shop;
    }

    @Override
    public void drive() {

        System.out.println(shop.work());    

    }

}


接口Shop.java
package com.demo.interfaces;

public interface Shop {
public String work();
}

实现类SShop.java

package com.demo.impls;

import com.demo.interfaces.Shop;

public class Sshop implements Shop{

@Override
public String work() {
    return "卖汽车";
}

}

spring 配置文件beans.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="chinese" class="com.demo.impls.Chinese">
        <constructor-arg ref="sshop"></constructor-arg>
    </bean>

    <bean id="sshop" class="com.demo.impls.Sshop"></bean>

</beans>

测试类Test.java
public static void main(String[] args) {
        ApplicationContext ctx = new    ClassPathXmlApplicationContext("beans.xml");
        Person p =  ctx.getBean("chinese",Person.class);
        p.drive();
}

最后输出结果:卖汽车。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值