依赖注入(DI)

本文详细介绍了Spring框架中的依赖注入(DI)概念,包括构造器注入和setter方式注入。通过实例展示了如何在XML配置文件中进行各种类型的注入,如常量、Bean、数组、List、Map、Set、Null以及Properties。此外,还提到了拓展注入的p命名和c命名空间注入,并解释了Spring中bean的作用域,包括单例和原型模式。
摘要由CSDN通过智能技术生成


依赖注入(DI)

概念

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 。 Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源(也可以说是bean对象中的所有属性) , 由容器来设置和装配 .

1、构造器注入

1、下标赋值

<!-- 第一种根据index参数下标设置 -->
<bean id="user" class="com.zhu.pojo.User">
    <!--index指的是有参构造中参数的下标,下标从0开始;-->
	<constructor-arg index="0" value="学好spring"/>
</bean>

2、通过类型创建,不推荐使用

<bean id="userT" class="com.kuang.pojo.UserT">
   <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

3、直接通过参数名来设置

<bean id="user" class="com.zhu.pojo.User">
   <constructor-arg name="name" value="joker" />
</bean>
<!-- 比如参数名是name,则有name="具体值" -->
  • 注册bean之后就对象的初始化了(类似 new 类名())
  • name方式还需要无参构造和set方法
  • index和type只需要有参构造

2、set方式注入

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is 。

【环境搭建】

1、创建两个实体类Student类与 Address类

//真实测试对象
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
    //set/get方法;重写toString
}
//复杂类型
public class Address {
    private String address;
     //set/get方法;重写toString
}

2、编写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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.zhu.pojo.Student">
<!--第一种,普通值注入,value-->
        <property name="name" value="张三"/>
    </bean>
</beans>

3、编写测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}
完善xml注入信息
1、常量注入
<bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
 </bean>

测试:

 @Test
 public void test01(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
     Student student = (Student) context.getBean("student");
 
     System.out.println(student.getName());
 
 }
2、Bean注入

注意点:这里的值是一个引用,ref

 <bean id="addr" class="com.kuang.pojo.Address">
     <property name="address" value="重庆"/>
 </bean>
 
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>
3、数组注入
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西游记</value>
             <value>红楼梦</value>
             <value>水浒传</value>
         </array>
     </property>
 </bean>
4、List注入
 <property name="hobbys">
     <list>
         <value>听歌</value>
         <value>看电影</value>
         <value>爬山</value>
     </list>
 </property>
5、Map注入
 <property name="card">
     <map>
         <entry key="中国邮政" value="456456456465456"/>
         <entry key="建设" value="1456682255511"/>
     </map>
 </property>
6、set注入
 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>
7、Null注入
<property name="wife">
     <null/>
</property>
8、Properties注入
 <property name="info">
     <props>
         <prop key="学号">20190604</prop>
         <prop key="性别"></prop>
         <prop key="姓名">小明</prop>
     </props>
 </property>

3、拓展注入

p命名和c命名注入

User.java :【注意:P命名空间需要无参构造器,C命名空间需要有参构造器!】

 public class User {
     private String name;
     private int age;
    //set方法、toString方法
    //有参构造,无参构造
 }

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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法 
	p命名空间注入。可以直接注入属性的值;property-->
    <bean id="user" class="com.zhu.pojo.User" p:name="朱桐兵" p:age="18"/>

<!-- c命名空间注入,通过构造器注入:construct-args
	 C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
    <bean id="user2" class="com.zhu.pojo.User" c:name="朱朱" c:age="18"/>
</beans>

测试代码:

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

注意点:p命名和c命名空间不能直接使用,需要导入xml约束!

 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:c="http://www.springframework.org/schema/c"

4、 bean的作用域

在这里插入图片描述

  1. 单例模式(Spring默认机制)

    在这里插入图片描述

  2. 原型模式: 每次从容器中get的时候,都产生一个新对象!

    在这里插入图片描述

  3. 其余的request、session、application这些只能在web开放中使用!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值