DI依赖注入环境

一、构造器注入

Controller
public class FooController {
  
  private final FooService fooService;
  
  @Autowired
  public FooController(FooService fooService) {
      this.fooService = fooService;
  }
  
}

 二、Set方式注入【重点】

· 依赖注入:Set注入

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

【环境搭建】

1、复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

2、真实测试对象

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;
}

3、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.z.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="小亮"/>
    </bean>

</beans>

4、测试类

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());
        System.out.println(student.getAddress());
    }
}

5、完善注入

    <bean id="address" class="com.z.pojo.Address">
        <property name="address" value="济南"></property>
    </bean>

    <bean id="student" class="com.z.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="小亮"/>

        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>

        <!--List-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>CS2</value>
            </list>
        </property>

        <!--Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="123123455623123423"/>
                <entry key="银行卡" value="132312312312"/>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value>CS2</value>
                <value>LOL</value>
            </set>
        </property>

        <!--NULL-->
        <property name="wife">
            <null/>
        </property>

        <!--properites-->
        <property name="info">
            <props>
                <prop key="driver">20231</prop>
                <prop key="username">小亮</prop>
                <prop key="url">男</prop>
                <prop key="password">123456</prop>

            </props>
        </property>

    </bean>

  System.out.println(student.toString());

输出结果:

Student{
        name='小亮', address=Address{address='济南'},
        books=[红楼梦, 三国演义, 水浒传, 西游记],
        hobbys=[听歌, CS2],
        card={身份证=123123455623123423, 银行卡=132312312312},
        games=[CS2, LOL],
        Wife='null',
        info={password=123456, url=男, driver=20231, username=小亮}
        }

三、拓展方式注入

使用c命名和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"
       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命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.z.pojo.User" p:age="18"/>
    <!--c命名空间注入,通过构造器注入:construc-args-->
    <bean id="user2" class="com.z.pojo.User" c:age="18" c:name="小亮"/>

</beans>

测试:

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

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

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

 四、bean的作用域

表 3. Bean 作用域

ScopeDescription
singleton(默认)将每个 Spring IoC 容器的单个 bean 定义范围限定为单个对象实例。
prototype将单个 bean 定义的作用域限定为任意数量的对象实例。
request将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有一个在单个 bean 定义后面创建的 bean 实例。仅在可感知网络的 Spring ApplicationContext中有效。
session将单个 bean 定义的范围限定为 HTTP Session的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。
application将单个 bean 定义的范围限定为ServletContext的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。
websocket将单个 bean 定义的范围限定为WebSocket的生命周期。仅在可感知网络的 Spring ApplicationContext上下文中有效。

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

<bean id="user2" class="com.z.pojo.User" c:age="18" c:name="小亮" scope="singleton"/>

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

<bean id="user2" class="com.z.pojo.User" c:age="18" c:name="小亮" scope="prototype"/>

3.其余的reques、session、application这些只能在web开发中使用到!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值