Spring04 DI

1、基于构造函数注入

  • 构造参数名注入
<bean id="address" class="com.study.pojo.Address">
    <property name="address" value="China"/>
</bean>

<bean id="people" class="com.study.pojo.People">
    <!-- 基本类型注入 -->
    <constructor-arg name="name" value="Amy"/>
    <constructor-arg name="age" value="2"/>
    <!-- 引用类型注入 -->
    <constructor-arg ref="address"/>
</bean>
  • 构造参数类型注入
<bean id="address" class="com.study.pojo.Address">
    <property name="address" value="China"/>
</bean>

<bean id="people" class="com.study.pojo.People">
    <constructor-arg type="java.lang.String" value="Amy"/>
    <constructor-arg type="int" value="2"/>
    <constructor-arg type="com.study.pojo.Address" ref="address"/>
</bean>
  • 构造参数索引注入
<bean id="address" class="com.study.pojo.Address">
    <property name="address" value="China"/>
</bean>

<bean id="people" class="com.study.pojo.People">
    <constructor-arg index="0" value="Amy"/>
    <constructor-arg index="1" value="2"/>
    <constructor-arg index="2" ref="address"/>
</bean>

2、基于 set 方法注入

  • 准备一个复杂类型对象
public class Address {

    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
  • 准备一个真实测试对象
public class Student {

    private String name;
    private Address address;
    private List<String> animals;
    private Map<String,String> map;
    private Set<String> set;
    private String city;
    private String[] fruits;
    private Properties properties;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public List<String> getAnimals() {
        return animals;
    }

    public void setAnimals(List<String> animals) {
        this.animals = animals;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String[] getFruits() {
        return fruits;
    }

    public void setFruits(String[] fruits) {
        this.fruits = fruits;
    }

    public Properties getProperties() {
        return properties;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", animals=" + animals +
                ", map=" + map +
                ", set=" + set +
                ", city='" + city + '\'' +
                ", fruits=" + Arrays.toString(fruits) +
                ", properties=" + properties +
                '}';
    }
}
  • bean 配置文件进行各种类型属性注入
<bean id="address" class="com.study.pojo.Address">
    <property name="address" value="中国北京"/>
</bean>
<bean id="student" class="com.study.pojo.Student">
    <!-- 属性值注入 -->
    <property name="name" value="Jack"/>
    <!-- 引用注入 -->
    <property name="address" ref="address"/>
    <!-- 数组注入 -->
    <property name="fruits">
        <array>
            <value>apple</value>
            <value>grape</value>
            <value>banana</value>
        </array>
    </property>
    <!-- list注入 -->
    <property name="animals">
        <list>
            <value>dog</value>
            <value>cat</value>
            <value>bear</value>
        </list>
    </property>
    <!-- null注入 -->
    <property name="city">
        <null/>
    </property>
    <!-- map注入 -->
    <property name="map">
        <map>
            <entry key="name" value="jack"/>
            <entry key="age" value="18"/>
        </map>
    </property>
    <!-- set注入 -->
    <property name="set">
        <set>
            <value>car</value>
            <value>bus</value>
        </set>
    </property>
    <!-- properties注入 -->
    <property name="properties">
        <props>
            <prop key="server.port">8080</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
</bean>
<bean id="people" class="com.study.pojo.People">
    <!-- 空值注入 -->
    <property name="name" value=""/>
</bean>
  • 测试类
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        // Student{name='Jack', address=Address{address='中国北京'}, animals=[dog, cat, bear], map={name=jack, age=18}, set=[car, bus], city='null', fruits=[apple, grape, banana], properties={server.port=8080, development=development@example.org}}
        System.out.println(student.toString());
    }
}

3、拓展方式注入

  • 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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.study.pojo.Address">
        <property name="address" value="中国北京"/>
    </bean>
    <!-- p命名空间注入,利用的是set方法注入,可以直接注入属性的值 -->
    <bean id="people1" class="com.study.pojo.People" p:name="Amy" p:age="2" p:address-ref="address"/>
    <!-- c命名空间注入,利用的是有参构造函数的构造参数注入属性值 -->
    <bean id="people2" class="com.study.pojo.People" c:name="Jack" c:age="2" c:address-ref="address"/>
    
</beans>
  • 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people1 = (People) context.getBean("people1");
        People people2 = (People) context.getBean("people2");
        // hello,everybody. my name is Amy. I am 2 years old! I come from 中国北京
        people1.sayHello();
        // hello,everybody. my name is Jack. I am 2 years old! I come from 中国北京
        people2.sayHello();
    }
}
  • 注意点
    p 命名空间和c 命名空间注入方式,需要先导入相关约束,不然不能使用
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

4、bean 的作用域
在这里插入图片描述

  • 单例模式(Spring 默认的机制)
<bean id="people1" class="com.study.pojo.People" p:name="Amy" p:age="2" p:address-ref="address" scope="singleton"/>
  • 原型模式:每次从容器中获取bean时,都会产生新的对象
<bean id="people2" class="com.study.pojo.People" c:name="Jack" c:age="2" c:address-ref="address" scope="prototype"/>
  • 其余的 request、session、application ,都只能在web开发中使用到
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值