ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring学习(六、依赖注入 DI)

2021-02-27 17:35:31  阅读:146  来源: 互联网

标签:依赖 String DI spring private name address public 注入


六、依赖注入

1.构造器注入

<bean id="hello" class="com.lzt.pojo.Hello">
    <constructor-arg name="str" value="lzt"/>
</bean>

2.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 Properties info;
        private String wife;
    
  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.lzt.pojo.Student">
            <!--第一种,普通值注入,直接使用value-->
            <property name="name" value="lzt"/>
        </bean>
    
    </beans>
    
  4. 测试类

    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = (Student) context.getBean("student");
            String name = student.getName();
            System.out.println(name);
        }
    }
    

完善注入信息:

<?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="address" class="com.lzt.pojo.Address">
        <property name="address" value="南极"/>
    </bean>

    <bean id="student" class="com.lzt.pojo.Student">
        <!--普通值注入,直接使用value-->
        <property name="name" value="lzt"/>

        <!--bean 注入,使用ref-->
        <property name="address" ref="address"/>

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

        <!--list注入-->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>打游戏</value>
                <value>敲代码</value>
            </list>
        </property>

        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="22222111123"/>
                <entry key="学生号" value="11111"/>
                <entry key="电话号码" value="333333"/>
            </map>
        </property>

        <!--set注入-->
        <property name="games">
            <set>
                <value>1</value>
                <value>2</value>
                <value>4</value>
                <value>3</value>
            </set>
        </property>

        <!--null注入-->
        <property name="wife">
            <null/>
        </property>

        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="学号">222</prop>
                <prop key="年龄">12</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>

</beans>

执行结果:

/*Student{name='lzt'
 address=Address{address='南极'}
 books=[红楼梦, 西游记, 水浒传, 三国演义]
 hobbies=[听歌, 打游戏, 敲代码]
 card={身份证=22222111123,
       学生号=11111,
       电话号码=333333}
 games=[1, 2, 4, 3]
 info={学号=222,
       性别=男,
       年龄=12}
 wife='null'}*/

3.拓展方式植入注入

我们可以通过p命名空间和从命名空间进行注入

  • 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"**加上这句**
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--p命名空间注入,可以直接注入属性;p:property -->
        <bean id="user" class="com.lzt.pojo.Student" P:name="sss"/>
            
    </beans>
    
  • c 命名空间注入

    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!--c命名空间注入,可以通过构造器注入属性;c:construct-args-->
        <bean id="user1" class="com.lzt.pojo.Student" c:name="2222" c:books="sss"/>
    
    </beans> 
    

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

标签:依赖,String,DI,spring,private,name,address,public,注入
来源: https://www.cnblogs.com/LiuOneZero/p/14456424.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有