spring依赖注入

这篇博文详细介绍了Spring框架中注解依赖注入的两种主要方式:set注入和构造注入。set注入通过setter方法设置属性,包括普通值、Bean、数组、List、Map、Set、Null和Properties的注入方式。构造注入则利用构造函数完成对象初始化,支持通过p和c命名空间简化属性设置。这两种方式各有特点,适用于不同的场景。
摘要由CSDN通过智能技术生成

三种依赖注入

Spring注解依赖注入的三种方式的优缺点以及优先选择

这篇博文能够说明三种依赖注入的特点。

下面是对set注入和拓展注入的详细说明:

1.set注入(重点)

pojo包:
Student.java

package com.kuang.pojo;

import java.util.*;

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.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

Address.java

package com.kuang.pojo;

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 + '\'' +
                '}';
    }
}

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="addr" class="com.kuang.pojo.Address">
        <property name="address" value="上海市黄浦区"/>
    </bean>


    <bean id="student" class="com.kuang.pojo.Student">
        <!--第一种:普通纸注入,value 重点-->
        <property name="name" value="秦师傅"/>
        <!--第二种:Bean注入,ref是引用bean的名字,重点-->
        <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>
        <!--第五种:Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="123442324444"/>
                <entry key="信用卡" value="4789876789078"/>
                <entry key="储蓄卡" value="9999999999999"/>
            </map>
        </property>
        <!--第六种:set注入-->
        <property name="games">
            <set>
                <value>跑步</value>
                <value>俯卧撑</value>
                <value>深蹲</value>
            </set>
        </property>
        <!--第七种:Null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--第八种:Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">344222</prop>
                <prop key="性别"></prop>
                <prop key="url">www.fefe.com</prop>
            </props>
        </property>
        
    </bean>
    

</beans>

测试类:

 @Test
    public void testOne() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student = (Student) context.getBean("student");

        System.out.println(student.toString());
    }

2.拓展注入

User.java

package com.kuang.pojo;

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

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = 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 +
                '}';
    }
}

userBeans.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"
       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(properties)命名空间:属性依然要设置set方法-->
    <bean id="user" class="com.kuang.pojo.User" p:name="老舍" p:age="99"/>


    <!--c(构造:Constructor)命名空间:属性依然要设置set方法-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="刘海鹰" c:age="57"/>

</beans>

测试类:

	/*拓展注入的测试(p和c)*/
    @Test
    public void testTwo() {
        ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");

        //User user = context.getBean("user", User.class);
        User user = context.getBean("user2", User.class);

        System.out.println(user.toString());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值