依赖注入

概念
  • 依赖注入(Dependency Injection,DI)。

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

构造器注入

前面已经介绍过,参考4、IOC创建对象的方式

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> hobbies;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
    }
    
  3. beans.xml

    
    <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.study.pojo.Student">
            
            <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());
        }
    }
    
  5. 完善注入信息

    <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="黑心白莲"/>
    
    <property name="address" ref="address"/>
    
    <property name="books">
    <array>
    <value>红楼梦</value>
    <value>西游记</value>
    <value>水浒传</value>
    <value>三国演义</value>
    </array>
    </property>
    
    <property name="hobbies">
    <list>
    <value>打篮球</value>
    <value>看电影</value>
    <value>敲代码</value>
    </list>
    </property>
    
    <property name="card">
    <map>
    <entry key="身份证" value="123456789987456321"/>
    <entry key="银行卡" value="359419496419481649"/>
    </map>
    </property>
    
    <property name="games">
    <set>
    <value>LOL</value>
    <value>COC</value>
    <value>BOB</value>
    </set>
    </property>
    
    <property name="wife">
    <null/>
    </property>
    
    <property name="info">
    <props>
    <prop key="driver">20191029</prop>
    <prop key="url">102.0913.524.4585</prop>
    <prop key="user">黑心白莲</prop>
    <prop key="password">123456</prop>
    </props>
    </property>
    </bean>
    

P命名空间注入 : 需要在头文件中加入约束文件

导入约束 : xmlns:p="http://www.springframework.org/schema/p"

<bean id="user" class="com.study.pojo.User" p:name="狂神" p:age="18"/>

c 命名空间注入 : 需要在头文件中加入约束文件

导入约束 : xmlns:c="http://www.springframework.org/schema/c"

<bean id="user" class="com.study.pojo.User" c:name="狂神" c:age="18"/>

发现问题:爆红了,刚才我们没有写有参构造!

解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!

测试:

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

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

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
bean的作用域

Spring03-依赖注入(DI)插图

几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用 框架),只能用在基于web的Spring ApplicationContext环境。

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

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

    <bean id="user2" class="com.study.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
    

.study.pojo.User” c:name=“狂神” c:age=“22” scope=“singleton”/>


2. 原型模式:每次从容器中get的时候,都会产生一个新对象!
```xml
  1. 其余的request、session、application、这些只能在web开发中使用到!
本站无任何商业行为
个人在线分享 » Spring03-依赖注入(DI)
E-->