4-DI(依赖注入)

DI三种方式

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的属性,由容器来注入

1、构造器注入

请参阅博客

2、Set方式注入

  • 环境搭建
public class Student {
    //bean | ref | idref | list | set | map | props | value | null
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String girlfriend; //null
    private Properties info;
	
	//getter/setter、toString方法
}
  • 配置
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.shang.Student">
        <!-- 普通值注入,value-->
        <property name="name" value="shang"/>
        
        <!-- bean注入 ref-->
        <property name="address" ref="address"/>
        
          <!-- array-->
        <property name="books">
            <array>
                <value>《红楼梦》</value>
                <value>《西游记》</value>
            </array>
        </property>
        
        <!-- list-->
        <property name="hobbys">
            <list>
                <value>乒乓球</value>
                <value>篮球</value>
                <value>羽毛球</value>
            </list>
        </property>

        <!-- set -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>王者荣耀</value>
                <value>绝地求生</value>
            </set>
        </property>
        
        <!-- null -->
        <property name="girlfriend">
            <null/>
        </property>
        
        <!-- map -->
        <property name="card">
            <map>
                <entry key="银行卡" value="216516165214899*2032**"></entry>
                <entry key="身份证" value="19842215982622s5/a0*"></entry>
            </map>
        </property>
        
        <!-- Properties-->
        <property name="info">
            <props>
                <prop key="座右铭">空谈误国,实干兴邦</prop>
                <prop key="星座">天蝎座</prop>
            </props>
        </property>
    </bean>

    <bean id="address" class="com.shang.Address">
        <property name="addr" value="安徽"/>
    </bean>

</beans>
  • 测试
public class HelloWorldTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
}
  • 结果

Student{

  • name=‘shang’,
  • address=Address{addr=‘安徽’},
  • books=[《红楼梦》, 《西游记》],
  • hobbys=[乒乓球, 篮球, 羽毛球],
  • card={银行卡=2165161652148992032**, 身份证=19842215982622s5/a0},
  • games=[LOL, 王者荣耀, 绝地求生],
  • girlfriend=‘null’,
  • info={座右铭=空谈误国,实干兴邦, 星座=天蝎座}
    }

3、扩展方法注入

  • 搭建环境
public class User {
    private String name;
    private Address address;
 // 无参构造器/有参构造器/ggetter/setter、toString方法
}

c命名空间(构造器注入的快捷方式)

  • 配置
  1. 引入约束 xmlns:c="http://www.springframework.org/schema/c
<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
  1. 使用c命名空间
<?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: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="user" class="com.shang.User" c:name="shang" c:address-ref="address"></bean>


    <bean id="address" class="com.shang.Address">
        <property name="addr" value="anhui"/>
    </bean>

</beans>
  • 测试结果
public class HelloWorldTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

在这里插入图片描述

p命名空间(Set方式注入的快捷方式)

  • 配置
  1. 引入约束 xmlns:p="http://www.springframework.org/schema/p
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
</beans>
  1. 使用p命名空间
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.shang.User" p:name="shang" p:address-ref="address">
    </bean>


    <bean id="address" class="com.shang.Address">
        <property name="addr" value="anhui"/>
    </bean>

</beans>
  • 测试结果
public class HelloWorldTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

在这里插入图片描述
[下一篇:bean的作用域]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值