Spring从入门到入土——依赖注入(DI)


相关文章

跟着官网学spring—快速入门指南

跟着官网学Spring—构建RESTful Web服务

Spring从入门到入土——概述以及IOC理论推导

Spring从入门到入土——快速上手Spring

Spring从入门到入土——依赖注入(DI)

Spring从入门到入土——Bean的作用域

Spring从入门到入土——自动装配

Spring从入门到入土——使用注解

Spring从入门到入土——AOP就这么简单

Spring从入门到入土——代理模式

Dependency Injection

概念

  • 依赖注入(DI)
  • 依赖:指Bean对象的创建依赖于容器。Bean对象的依赖资源
  • 注入:指Bean对象

注入方式

一共有三种:分别是构造器注入;Set注入;P命名和C命名注入

构造器注入

Spring从入门到入土——快速上手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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--   第一种根据参数名字来设置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!--        name 指参数名-->
        <constructor-arg name="name"value="zhonghu"/>
        
    </bean>
    
    
<!--    第二种根骨index参数下标来设置-->
    <bean id="userT" class="com.zhonghu.pojo.UserT">
<!--     index值构造方法,下标从0开始   -->
        <constructor-arg index="0" value="zhonghu"/>
    </bean>

    
    
<!--   第三种 根据参数类型设置 十分不推荐-->
    <bean id="useT"class="com.zhonghu.pojo.UserT">
        <constructor-arg type="java.lang.String" value="zhonghu"/>
    </bean>
</beans>

Set注入

​ 要求被注入的属性必须有set方法,set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is

测试pojo类:

Address.java

public class Address {
    private String Address;

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "Address='" + Address + '\'' +
                '}';
    }
}

Student.java

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;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
Bean注入:

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="address" class="com.zhonghu.pojo.Address">
        <property name="address" value="China"/>
    </bean>
    
    <bean id="student" class="com.zhonghu.pojo.Student">
        <!--        普通值注入-->
        <property name="name" value="冢狐"/>


        <!--        Bean注入-->
        <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="123456789"/>
                <entry key="性别" value=""/>
                <entry key="姓名" value="冢狐"/>
            </map>
        </property>
        <!--        set-->
        <property name="games">
            <set>
                <value>CSGO</value>
                <value>COC</value>
            </set>
        </property>
        <!--        Null-->
        <property name="wife">
            <null/>
        </property>
        <!--        Properties-->
        <property name="info">
            <props>
                <prop key="url">https://blog.csdn.net/issunmingzhi</prop>
                <prop key="用户名">root</prop>
                <prop key="密码">123456</prop>
            </props>
        </property>
    </bean>

</beans>

测试:

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.toString());
    }
}
结果:

p命名和c命名注入

user.java:【注意,这里没有有参构造器】

public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
p命名空间注入:

​ 就是set注入、需要在头文件中加入约束条件

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.zhonghu.pojo.User" p:name="冢狐" p:age="18"/>
c命名空间注入

​ 就是构造器注入、需要在头文件下加入约束文件

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.zhonghu.pojo.User" c:name="冢狐" c:age="18"/>

此时会发现出问题:因为没有写有参构造函数

C命名空间注入就是所谓的构造器注入

猜你喜欢

转载自blog.csdn.net/issunmingzhi/article/details/105941426
今日推荐