【Spring】学习笔记05-DI注入

1.构造器注入

前面已经说过了

2.通过Set方式注入

依赖注入

依赖:bean对象的创建依赖于容器

注入:bean对象中的所有属性由容器来注入!

环境搭建

pojo实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
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 Properties info;
    private String wife;
    
}

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_bean" class="com.wang.pojo.Address">
        <constructor-arg index="0" value="北京市海淀区"/>
    </bean>
    <bean id="student" class="com.wang.pojo.Student">
<!--        普通值注入,value-->
        <property name="name" value="王广元"/>
<!--        Bean注入,ref-->
        <property name="address" ref="address_bean"/>
<!--        数组注入-->
        <property name="books">
            <array>
                <value>你的名字</value>
                <value>我的天才女友</value>
                <value>万历十五年</value>
            </array>
        </property>
        <!--            list注入-->
        <property name="hobbys">
            <list>
                <value>jungle</value>
                <value>yoga</value>
                <value>music</value>
            </list>
        </property>
<!--        map注入-->
        <property name="card">
            <map>
                <entry key="学生卡" value="200"></entry>
                <entry key="洗澡卡" value="20"></entry>
                <entry key="剪发卡" value="200"></entry>
            </map>
        </property>
<!--        set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>Dota</value>
            </set>
        </property>
<!--        null值注入-->
        <property name="wife">
            <null></null>
        </property>
<!--       Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">1120201390</prop>
                <prop key="大学">大连海事大学</prop>
                <prop key="专业">电子信息</prop>
            </props>
        </property>

     </bean>
</beans>

 

单元测试

  @Test
    public void test01(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Student student = (Student)context.getBean("student");
        System.out.println(student.toString());
    }

测试结果

Student{name='王广元', address=Address(address=北京市海淀区), books=[你的名字, 我的天才女友, 万历十五年], hobbys=[jungle, yoga, music], card={学生卡=200, 洗澡卡=20, 剪发卡=200}, games=[LOL, Dota], info={学号=1120201390, 专业=电子信息, 大学=大连海事大学}, wife='null'}

进程已结束,退出代码为 0

 

3.其他拓展方式注入

pojo类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private int age;

}

 

p命名空间注入

XML Shortcut with the p-namespace

The p-namespace lets you use the bean element’s attributes (instead of nested <property/> elements) to describe your property values collaborating beans, or both.

Spring supports extensible configuration formats with namespaces, which are based on an XML Schema definition. The beans configuration format discussed in this chapter is

defined in an XML Schema document. However, the p-namespace is not defined in an XSD file and exists only in the core of Spring.

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"
       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">
<!--    p命名空间注入,可以直接注入属性-->
    <bean id="user_p" class="com.wang.pojo.User" p:age="28" p:name="user_p">

    </bean>
</beans>

注意:要使用p命名空间必须添加xmlns命名约束

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

单元测试:

 

@Test
    public void test02(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        User user_p = context.getBean("user_p", User.class);
        System.out.println(user_p.toString());

    }

 

 

c命名空间注入

 

XML Shortcut with the c-namespace

Similar to the XML Shortcut with the p-namespace, the c-namespace, introduced in Spring 3.1, allows inlined attributes for configuring the constructor arguments rather then nested constructor-arg elements.

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"
       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">
    <!--    p命名空间注入,可以直接注入属性-->
    <bean id="user_c" class="com.wang.pojo.User" c:name="user_c" c:age="18">
    </bean>
    <bean id="user_c1" class="com.wang.pojo.User" c:_0="user_c_0" c:_1="18"/>
</beans>

注意:此时通过c命名空间注入属性,我们既可以通过实体类的属性(c:name),也可以通过实体类的属性下标(c:_0)进行注入

同时必须得导入xmlns约束

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

单元测试
   @Test
    public void test03(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("UserBean_c_namespace.xml");
        User user_c = context.getBean("user_c", User.class);
        System.out.println(user_c.toString());
        User user_c1 = context.getBean("user_c1", User.class);
        System.out.println(user_c1.toString());
    }

 

 

 

 

posted @ 2022-06-05 22:28  王广元  阅读(25)  评论(0编辑  收藏  举报
分享到: