spring DI

1.DI:依赖注入,给类的属性设置值,依赖注入不能单独存在,要在IOC的基础上
2.spring支持set注入和有参构造注入两种属性注入
3.DI的使用
1)有参构造注入
例子:创建的类:

public class MyTestBean {
    private String name;


    public MyTestBean(String name) {
        this.name = name;
    }


    public void test()
    {

        System.out.println("test..."+name);
    }

}

配置文件:

<?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-3.1.xsd">

    <bean id="myTestBean" class="main.java.MyTestBean">
        <constructor-arg name="name" value="tom"></constructor-arg>
    </bean>
</beans>

constructor-arg中的name是构造方法中的参数,value是要设置的值
测试类:

public class beanTest {

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyTestBean bean =(MyTestBean) context.getBean("myTestBean");
        bean.test();    
    }


}

运行结果:
这里写图片描述
2)set注入:
创建的类:

public class MyTestBean {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void test()
    {

        System.out.println("test..."+name);
    }

}

配置文件:

<?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-3.1.xsd">

    <bean id="myTestBean" class="main.java.MyTestBean">
        <property name="name" value="spring"></property>
    </bean>
</beans>

property标签中的name为属性名,value是要设置的值
测试类:

public class beanTest {

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyTestBean bean =(MyTestBean) context.getBean("myTestBean");
        bean.test();    
    }
}

运行结果:
这里写图片描述
set注入对象和复杂类型(注入对象是重点)
创建类:

public class UserDAO {

    public void test()
    {
        System.out.println("userdao...");
    }

}

public class UserService {
    private UserDAO userDAO;
    private String[] strs;
    private List<String> lists;
    private Map<String, String> maps;
    private Properties properties;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setStrs(String[] strs) {
        this.strs = strs;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void test()
    {
        System.out.println("userservice...");
        userDAO.test();
        System.out.println("strs"+strs);
        System.out.println("lists"+lists);
        System.out.println("maps"+maps);
        System.out.println("properties"+properties);
    }

}

配置文件:

<?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-3.1.xsd">

    <bean name="userDAO" class="main.java.UserDAO"></bean>
    <bean name="userService" class="main.java.UserService">
        <!-- set注入对象 -->
        <!-- ref是引用 -->
        <property name="userDAO" ref="userDAO" ></property>
        <!-- set注入数组 -->
        <property name="strs">
            <list>
                <value>spring</value>
                <value>springmvc</value>
            </list>
        </property>
        <!-- set注入list -->
        <property name="lists">
            <list>
                <value>a</value>
                <value>b</value>
            </list>
        </property>
        <!-- set注入map -->
        <property name="maps">
            <map>
                <entry key="abc" value="1"></entry>
                <entry key="def" value="2"></entry>
            </map>
        </property>
        <!-- set注入properties -->
        <property name="properties">
            <props>
                <prop key="1" >one</prop>
                <prop key="2">two</prop>
            </props>
        </property>
    </bean>
</beans>

测试类:

public class beanTest {

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService =(UserService) context.getBean("userService");
        userService.test(); 
    }
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值