依赖注入(DI)的三种方式

        个人对依赖注入的理解是:为bean所代表的对象的属性赋值 。对象的属性有多种类型,主要包括基本数据类型、String、引用类型、集合类型、属性类型、空字符串以及空值null。在通过配置文件进行依赖注入时,主要有以下三种配置方式:

1. 有参构造器注入

        这种方式的前提是,在bean所对应的类中显式定义有参构造函数,涉及到的属性才能够使用<constructor-arg/>标签进行赋值。具体参照链接Spring IOC(控制反转)_鲸鱼-D的博客-CSDN博客中IOC创建对象方式中的方式2。

2.Set方式注入(重点)

        前提: 类中有无参构造函数(默认or显式定义)以及setter方法。

  • Student类:类中的成员属性包括各种数据类型的变量
    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 wife1;
        private String wife2;
        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 getWife1() {
            return wife1;
        }
    
        public void setWife1(String wife1) {
            this.wife1 = wife1;
        }
    
        public String getWife2() {
            return wife2;
        }
    
        public void setWife2(String wife2) {
            this.wife2 = wife2;
        }
    
        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 +
                    ", wife1='" + wife1 + '\'' +
                    ", wife2='" + wife2 + '\'' +
                    ", info=" + info +
                    '}';
        }
    }
  • 配置文件
    <?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.jing.pojo.Address">
            <property name="address" value="Beijing"/>
        </bean>
    
        <bean id="student" class="com.jing.pojo.Student">
            <!--基本数据类型herString注入-->
            <property name="name" value="YuJing"/>
            <!--引用类型注入-->
            <property name="address" ref="address"/>
            <!--数组类型注入-->
            <property name="books">
                <array value-type="java.lang.String">
                    <value>红楼梦</value>
                    <value>西游记</value>
                    <value>水浒传</value>
                    <value>三国演义</value>
                </array>
            </property>
            <!--单列集合List注入-->
            <property name="hobbies">
                <list value-type="java.lang.String">
                    <value>看综艺</value>
                    <value>敲代码</value>
                    <value>听歌</value>
                </list>
            </property>
            <!--双列结合Map注入-->
            <property name="card">
                <map key-type="java.lang.String" value-type="java.lang.String">
                    <entry key="YuJing" value="123456"/>
                    <entry key="JingYu" value="654321"/>
                </map>
            </property>
            <!--单列结合Set-->
            <property name="games">
                <set value-type="java.lang.String">
                    <value>LOL</value>
                    <value>COC</value>
                    <value>BOB</value>
                </set>
            </property>
            <!--空值null-->
            <property name="wife1">
                <null/>
            </property>
            <!--空字符串注入-->
            <property name="wife2" value=""/>
            <!--Properties注入-->
            <property name="info">
                <props>
                    <prop key="driver">com.mysql.jdbc.driver</prop>
                    <prop key="url">http://mysql</prop>
                    <prop key="username">root</prop>
                    <prop key="password">root</prop>
                </props>
            </property>
        </bean>
    
    </beans>

3. 扩展方式注入 

        为了简化上述两种配置方式,Spring为我们提供了与上述两种方式分别对应的扩展方式:

  • c命名空间:简化方式1,除了方式1中的前提以外,还需要在配置文件中加入约束xmlns:c="http://www.springframework.org/schema/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命名空间注入,可以通过构造器注入:construct-args-->
        <bean id="student1" class="com.jing.pojo.Student" c:name="郭靖" c:wife="黄蓉"></bean>
    
    </beans>
  • p命名空间:简化方式2,除了方式2中的前提以外,还需要再配置文件中加入约束xmlns:p="http://www.springframework.org/schema/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命名空间注入,可以通过构造器注入:property-->
        <bean id="student2" class="com.jing.pojo.Student" p:name="郭靖" p:wife="黄蓉"></bean>
    
    </beans>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值