Java技术专题

Spring依赖注入

2018-10-07  本文已影响11人  爱撒谎的男孩

Spring依赖注入

【了解】Spring管理对象的生命周期(只有在单例的模式有意义,这个是默认的)

为单个bean指定生命周期方法

public class Person {
    private String name;
    private int age;
    //person类的初始化方法
    public void init(){
        System.out.println("初始化方法");
    }
    public void destory(){
        System.out.println("销毁方法");
    }
}
    <!--
        init-method : 定义初始化方法,直接写上方法名称即可
        destroy-method: 定义销毁方法,直接写上方法名即可
     -->
    <bean id="" class="cn.tedu.spring.beans.Person" init-method="init" destroy-method="destory"></bean>

为容器中所有的bean指定生命周期方法

<beans default-init-method="init" default-destroy-method="destroy">
    <bean id="person" class="cn.tedu.spring.bean.Person"></bean>
</bean>

【重要】注入属性值

实现目标

Setter注入

前提

基本数据类型变量的注入

实现
public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

<bean id="person" class="cn.tedu.spring.beans.Person">
        <!-- 直接使用property配置参数
            name:指定属性的字段,这个是set方法后面的单词首字母小写的值,比如SetUsername(),那么此时的name值为username
            value: 指定属性的值
            ref : 指定前面定义的bean的id,用于设置引用类型的参数值
         -->
        <property name="name" value="陈加兵"></property>
        <property name="age" value="22"></property>
</bean>

引用类型的属性注入

实现
public class Address {
    private String city;  //城市
    private String pro ;  //省份
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getPro() {
        return pro;
    }
    public void setPro(String pro) {
        this.pro = pro;
    }
}
public class Person {
    private String name;
    private int age;
    private Address address; // Address的对象作为成员变量

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

    <!-- 创建一个Address的实例 -->
    <bean id="address" class="cn.tedu.spring.beans.Address">
        <property name="city" value="无锡"></property>
        <property name="pro" value="江苏"></property>
    </bean>

    <bean id="person" class="cn.tedu.spring.beans.Person">
        <property name="name" value="陈加兵"></property>
        <property name="age" value="22"></property>

        <!-- 这里的ref引用的是上面配置的Address的实例中的id值 -->
        <property name="address" ref="address"></property>
    </bean>

【了解】构造器注入(无参,有参)

前提

实现

public class Person {
    private String name;
    private int age;
    //构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
    <bean id="person" class="cn.tedu.spring.beans.Person">
        <!--
            name: 这个相当于index,也是指定参数列表的属性,不过这里是直接使用参数列表中的变量名
            value: 为参数设置的值
            index: 构造方法的参数列表的索引,从0开始
            ref:引用类型的值,这里的值应该是上面已经定义好的bean的id值
         -->
        <constructor-arg index="0" value="陈加兵"></constructor-arg>
        <constructor-arg index="1" value="22"></constructor-arg>
    </bean>

注入基本型

注入非基本型(ref)

注入集合

实现

public class Message {
    private List<String> cities;   //城市 。List集合
    private Set<String> friend;   //Set集合
    private Map<Integer,String> bookes;  //Map集合
    private Properties properties;   //Properties集合
    public List<String> getCities() {
        return cities;
    }
    public void setCities(List<String> cities) {
        this.cities = cities;
    }
    public Set<String> getFriend() {
        return friend;
    }
    public void setFriend(Set<String> friend) {
        this.friend = friend;
    }
    public Map<Integer, String> getBookes() {
        return bookes;
    }
    public void setBookes(Map<Integer, String> bookes) {
        this.bookes = bookes;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

<bean id="message" class="cn.tedu.spring.beans.Message">
        <!-- List集合的注入 -->
        <property name="cities">
            <list>
                <value>徐州</value>
                <value>无锡</value>
                <value>常州</value>
            </list>
        </property>

        <!-- Set集合的注入 -->
        <property name="friend">
            <set>
                <value>Jack</value>
                <value>Tom</value>
                <value>陈加兵</value>
            </set>
        </property>

        <!-- Map集合的注入 -->
        <property name="bookes">
            <map>
                <entry key="1001" value="java编程基础"></entry>
                <entry key="1002" value="java编程思想"></entry>
            </map>
        </property>

        <!-- properties的集合的注入 -->
        <property name="properties">
            <props>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
@Test
    public void test2() {
        // spring的配置文件
        String conf = "applicationContext.xml";
        // 获取spring容器
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                conf);
        Message message=(Message) context.getBean("message");
        List<String> cities=message.getCities();
        Set<String> friends=message.getFriend();
        Map<Integer, String> bookes=message.getBookes();
        Properties properties=message.getProperties();
        System.out.println(cities);
        System.out.println(friends);
        System.out.println(bookes);
        System.out.println(properties);
        context.close();
    }

【重点了解Properties的注入】引用方式注入集合(<util:>)

root=root
password=root
url=jdbc:mysql://localhost:3306/hirbernate?useUnicode=true&characterEncoding=UTF-8
driver=com.mysql.jdbc.Driver
    <util:list id="cities">
        <value>徐州</value>
        <value>无锡</value>
        <value>常州</value>
    </util:list>

    <util:set id="friends">
        <value>Jack</value>
        <value>Tom</value>
        <value>陈加兵</value>
    </util:set>

    <util:map id="bookes">
        <entry key="1001" value="java编程基础"></entry>
        <entry key="1002" value="java编程思想"></entry>
    </util:map>

    <!-- 引入外部的Properties文件,location指定的就是位置 -->
    <util:properties id="properties" location="classpath:jdbc.properties"></util:properties>

    <bean id="message" class="cn.tedu.spring.beans.Message">
        <!-- List集合的注入 ref指定的上面定义的List的id -->
        <property name="cities" ref="cities"></property>

        <!-- Set集合的注入 -->
        <property name="friend" ref="friends"></property>

        <!-- Map集合的注入 -->
        <property name="bookes" ref="bookes"></property>

        <!-- properties的集合的注入 -->
        <property name="properties" ref="properties"></property>
    </bean>

【了解】其他类型的注入

为数组注入值

private String[] names;

    public String[] getNames() {
        return names;
    }
    public void setNames(String[] names) {
        this.names = names;
    }
        <!-- 为数组赋值 -->
        <property name="names">
            <array>
                <value>Alex</value>
                <value>Billy</value>
            </array>
        </property>

注入空字符串

为引用类型的数据注入null值

<property name="xxx">
    <null/>
</property>

显示的确定数据类型

<property name="xxx">
    <value type="数据类型">值</value>
</property>
上一篇 下一篇

猜你喜欢

热点阅读