spring-依赖注入

依赖注入

1、构造器注入

  1. 使用无参构造方法创建对象,默认!

  2. 假设我们要使用有参构造创建对象

    1. 下标赋值

      <!--第一种通过下标赋值-->
      <bean id="user" class="com.vekzjj.pojo.User">
          <constructor-arg index="0" value="vekzjj"/>
      </bean>
      
    2. 类型

      <!--第二种通过类型创建-->
      <bean id="user" class="com.vekzjj.pojo.User">
          <constructor-arg type="java.lang.String" value="vekzjj"/>
      </bean>
      
    3. 参数名

      <!--第三种,直接通过参数名来赋值-->
      <bean id="user" class="com.vekzjj.pojo.User">
          <constructor-arg name="name" value="vekzjj"/>
      </bean>
      

2、Set方式注入【重点】

  • 依赖注入:本质是Set注入
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,又容器注入

【环境搭建】

  1. 复杂类型

    package com.vekzjj.pojo;
    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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="address" class="com.vekzjj.pojo.Address">
            <property name="address" value="西安"/>
        </bean>
    <!--普通值注入,value-->
        <bean id="student" class="com.vekzjj.pojo.Student">
            <property name="name" value="vekzjj"/>
    <!--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>打游戏</value>
                    <value>看电影</value>
                </list>
            </property>
    <!--Map注入-->
            <property name="card">
                <map>
                    <entry key="QQ" value="2088374723"/>
                    <entry key="电话" value="13999007878"/>
                </map>
            </property>
    <!--Set-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>csgo</value>
                    <value>永劫无间</value>
                </set>
            </property>
    <!--null-->
            <property name="wife">
                <null/>
            </property>
    <!--Properties-->
            <property name="info">
                <props>
                    <prop key="学号">20202501033</prop>
                    <prop key="班级">20-23</prop>
                    <prop key="sex"></prop>
                </props>
            </property>
        </bean>
    </beans>
    
  4. 测试类

    public class DemoTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
            System.out.println(student.toString());
        }
    }
    
  5. 输出

    Student{
        name='vekzjj',
        address=Address{address='西安'},
        books=[红楼梦, 西游记, 水浒传, 三国演义],
        hobbys=[听歌, 打游戏, 看电影],
        card={
            QQ=2088374723,
            电话=13999007878
        },
        games=[LOL, csgo, 永劫无间], wife='null',
        info={
            学号=20202501033,
            班级=20-23,
            sex=男
        }
    }
    

3、第三方式注入

1、 p-namespace

  1. 在bean的namespace中加入:

    xmlns:p="http://www.springframework.org/schema/p"
    
  2. <!--p命名空间注入可以直接注入属性的值-->
        <bean id="user" class="com.vekzjj.pojo.User" p:name="vekzjj" p:age="20"/>
    

2、c-namespace

  1. 在bean的namespace中加入:

    xmlns:c="http://www.springframework.org/schema/c"
    
  2. <!--c命名空间注入,通过构造器注入-->
    <bean id="user2" class="com.vekzjj.pojo.User" c:name="zjj" c:age="20"/>
    

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

4、bean的作用域(scope)

ScopeDescription
singleton(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototypeScopes a single bean definition to any number of object instances.
requestScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
  1. 单例模式(Spring默认机制)

    <bean id="user2" class="com.vekzjj.pojo.User" c:name="zjj" c:age="20" scope="singleton"/>
    
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象

    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
    
  3. 其他的request、session、application(这些只能在web开发中使用到!)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值