Spring学习---依赖注入(DI)

依赖注入(DI)

构造器注入

在之前IOC创建对象部分有提过
详情看这篇大佬讲解的博客

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 girlfriend;
    
        //get、set方法
        //toString
    }
    

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="student" class="com.orange.pojo.Student">
        <property name="name" value="guolicheng"/>
    </bean>
</beans>
  1. 测试类

    public class Mytest {
          
          
        public static void main(String[] args) {
          
          
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            Student student = (Student) context.getBean("student");
            System.out.println(student.getName()); //输出名字
            System.out.println(student.getAddress()); //null  因为没有set注入
        }
    }
    

完善注入:

<?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.orange.pojo.Address">
        <property name="address" value="北京"/>
    </bean>

    <bean id="student" class="com.orange.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="hobbies">
            <list>
                <value>听jay的歌</value>
                <value>跑步</value>
                <value>编程</value>
                <value>打游戏</value>
            </list>
        </property>

        <!-- Map注入-->
        <property name="card">
            <map>
                <entry key="学生证" value="111222333444"/>
                <entry key="身份证" value="222333444555"/>
                <entry key="借阅证" value="333444555666"/>
            </map>
        </property>

        <!-- Set注入-->
        <property name="games">
            <set>
                <value>QQ飞车</value>
                <value>王者荣耀</value>
                <value>刺激战场</value>
            </set>
        </property>

        <!-- Null-->
        <property name="girlfriend">
            <null/>
        </property>

        <!-- Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">19406</prop>
                <prop key="专业">人工智能</prop>
                <prop key="性别"></prop>
                <prop key="年龄">20</prop>
            </props>
        </property>


    </bean>
</beans>

拓展方式注入

1.P命名注入

在配置文件顶部导入:

xmlns:p="http://www.springframework.org/schema/p"
<!--  p命名空间注入  -->
    <bean id="user" class="com.orange.pojo.User" p:name="小明" p:age="19"/>
2.C命名注入

在配置文件顶部导入:

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

显示灰色代表还未使用,c命名前提是得在实体类里需要编写有参和无参。

public User() {
    
    
}

public User(String name, int age) {
    
    
    this.name = name;
    this.age = age;
}
<!--  命名空间注入  -->
    <bean id="user2" class="com.orange.pojo.User" c:name="小华" c:age="18"/>

测试:

@Test
public void test2(){
    
    
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = (User) context.getBean("user");

    System.out.println(user);
}

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

猜你喜欢

转载自blog.csdn.net/qq_58372242/article/details/124494053