Spring依赖注入(DI)

Spring依赖注入(DI)

基本概念:

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
    1.构造器注入
    在上一篇文章中已提到:https://blog.csdn.net/weixin_44777314/article/details/118499415
    2.Set 注入
    pojo类(student address两个类):
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 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> getHobbys() {
        return hobbys;
    }
    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }
    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 +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}


public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

xml配置文件(主要是学习多种类型的注入方式类型,看起来有点长,但是很多类型已经囊括):

<!--Bean注入 -->
    <bean id="addr" class="com.xiong.pojo.Address">
        <property name="address" value="重庆"/>
    </bean>
    <!--数组注入-->
    <bean id="student" class="com.xiong.pojo.Student">
        <property name="name" value="小红"/>
        <property name="address" ref="addr"/>
        <property name="books">
            <array>
                <value>刘备</value>
                <value>关羽</value>
                <value>张飞</value>
            </array>
        </property>
        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>喝水</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="中通" value="1231456"/>
                <entry key="顺丰" value="124564"/>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>刺激战场</value>
                <value>QQ飞车</value>
            </set>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">2015874643</prop>
                <prop key="班级">学前教育</prop>
                <prop key="性别">6班</prop>
            </props>
        </property>
    </bean>

测试类:


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

3.扩展方式注入

p命名和c命名注入(c命名方式注入需要创建有参构造函数才能实现其功能,主要在xml文件中引入第三方注入方式实现)
实体类:

   public class User {
     private String name;
     private int age; 
     public void setName(String name) {
         this.name = name;
    }
     public void setAge(int age) {
         this.age = age;
    }
     @Override
     public String toString() {
         return "User{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
    }
 }

XML配置文件:
1、P命名空间注入 : 需要在头文件中加入约束文件

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

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

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

测试:

 @Test
 public void test02(){
     ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
     User user = (User) context.getBean("user");
     System.out.println(user);
 }

注意事项:实现c命名的时候要构建有参、无参构造函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值