Spring的依赖注入和自动装配(byName、byType)

Spring的依赖注入和自动装配(byName、byType)

1、概念

1.1依赖注入:又可以叫做控制反转, 即将初始化的控制权交给他人(Spring容器)。

2、文件位置截图

项目文件路径图

3、代码

3.1、要点说明:

注意:在实体类中要有相应的getter和setter方法
1、包括:构造器注入的测试、集合注入测试、byName注入测试、byType注入测试、没有指定autowire的类型测试
2、相关说明
2.1、构造器注入的方式:名称、下标、类型
2.2、byType:搜索整个配置文件中的bean,如果有一个相同类型的bean则自动装配,如果没有或超过一个则显示异常(即没有com.pojo.Child或多于一个)。
2.3、byName: 搜索整个配置文件中的bean,如果有相同名称的bean则自动装配(相同id或name),否则显示异常。

3.2、实体类对象

3.2.1、Person类
package com.pojo;

public class Person {
    private String pName;
    private String pAccount;
    private Child child;
    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        this.child = child;
    }
    public String getpName() {
        return pName;
    }
    public void setpName(String pName) {
        this.pName = pName;
    }
    public String getpAccount() {
        return pAccount;
    }
    public void setpAccount(String pAccount) {
        this.pAccount = pAccount;
    }
}


3.2.2、Child类
package com.pojo;

public class Child {
    private String cName;
    private String cAccount;
    public String getcName() {
        return cName;
    }
    public void setcName(String cName) {
        this.cName = cName;
    }
    /*记得把无参构造器补上*/
    public Child() {
    }
    public Child(String cName) {
        this.cName = cName;
    }
    public Child(String cName, String cAccount) {
        this.cName = cName;
        this.cAccount = cAccount;
        System.out.println("有参构造器信息-->名称:"+cName+",账号:"+cAccount);
    }
    public String getcAccount() {
        return cAccount;
    }
    public void setcAccount(String cAccount) {
        this.cAccount = cAccount;
    }
    @Override
    public String toString() {
        return "Child{" +
                "cName='" + cName + '\'' +
                ", cAccount='" + cAccount + '\'' +
                '}';
    }
}

3.2.3、Test类
package com.pojo;

public class Test {
    private Integer age;
    private Child child;
    public Test() {
        System.out.println("test的无参构造");
    }
    public Test(Integer age) {
        this.age = age;
        if(age<18){
            System.out.println("你"+age+"岁还是未成年人!");
        }else{
            System.out.println("你"+age+"岁已经是成年人了!");
        }
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        this.child = child;
    }
}

3.2.4、TestCollection类
package com.pojo;


import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TestCollection {
    private List list;
    private Set set;
    private Map map;
    private Properties properties;
    public List getList() {
        System.out.println("list:"+list);
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }

    public Set getSet() {
        System.out.println("set:"+set);
        return set;
    }
    public void setSet(Set set) {
        this.set = set;
    }
    public Map getMap() {
        System.out.println("map:"+map);
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public Properties getProperties() {
        System.out.println("properties:"+properties);
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

3.3、XML文件

3.3.1、Bean1.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="test" class="com.pojo.Test">
        <!--设值注入-->
        <property name="age" value="11"></property>
        <!--注入内置bean-->
        <property name="child">
            <bean id="child" class="com.pojo.Child" p:cName="lcb" p:cAccount="123">
                <!--通过name注入构造器-->
                <constructor-arg name="cName" value="lcb"></constructor-arg>
                <constructor-arg name="cAccount" value="123"></constructor-arg>
            </bean>
        </property>
        <!--三种注入构造器的方式-->
        <!--1、通过name注入构造器-->
        <!--<constructor-arg name="age" value="16"></constructor-arg>-->
        <!--2、通过下标注入构造器-->
        <!--<constructor-arg index="0" value="16"></constructor-arg>-->
        <!--3、通过类型注入构造器-->
        <constructor-arg type="java.lang.Integer" value="16"></constructor-arg>
    </bean>
</beans>

3.3.2、Bean2.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--集合注入-->
    <bean id="testCollection" class="com.pojo.TestCollection">
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="mp1" value="map1"></entry>
                <entry key="mp2" value="map2"></entry>
                <entry key="mp3" value="map3"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="one">1</prop>
                <prop key="two">2</prop>
                <prop key="three">3</prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>set1</value>
                <value>set2</value>
                <value>set3</value>
            </set>
        </property>
    </bean>


</beans>

3.3.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" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!--自动注入-->
    <!--
        注意:在实体类中要有相应的getter和setter方法
        byType:搜索整个配置文件中的bean,如果有一个相同类型的bean则自动装配,如果没有或超过一个则显示异常(即没有com.pojo.Child或多于一个)。
        byName: 搜索整个配置文件中的bean,如果有相同名称的bean则自动装配(相同id或name),否则显示异常。
    -->

    <!--一、通过byType注入-->
    <!--<bean id="person0" class="com.pojo.Person" autowire="byType">-->
        <!--<property name="pName" value="lcb"></property>-->
        <!--<property name="pAccount" value="123"></property>-->
    <!--</bean>-->
    <!--<bean id="childByType" class="com.pojo.Child">-->
        <!--<property name="cName" value="laj"></property>-->
        <!--<property name="cAccount" value="124"></property>-->
    <!--</bean>-->

    <!--二、通过byName注入-->
    <bean id="person0" class="com.pojo.Person" autowire="byName">
        <property name="pName" value="lcb"></property>
        <property name="pAccount" value="123"></property>
    </bean>
    <bean id="child" class="com.pojo.Child">
        <property name="cName" value="laj"></property>
        <property name="cAccount" value="124"></property>
    </bean>

    <!--三、如果不指定autowire的类型,可以通过以下方式实现注入-->
    <!--1-->
    <bean id="person1" class="com.pojo.Person">
        <property name="pName" value="lcb"></property>
        <property name="pAccount" value="123"></property>
        <!--注入内部bean-->
        <property name="child">
            <bean id="child" class="com.pojo.Child" autowire="byType">
                <property name="cName" value="laj"></property>
                <property name="cAccount" value="124"></property>
            </bean>
        </property>
    </bean>

    <!--2、parent="persons":定义继承-->
    <bean id="person2" class="com.pojo.Person" parent="persons">
    </bean>
    <bean id="persons" class="com.pojo.Person">
        <property name="pName" value="lcb"></property>
        <property name="pAccount" value="123"></property>
        <property name="child" ref="child2">
        </property>
    </bean>
    <bean id="child2" class="com.pojo.Child">
        <property name="cName" value="laj"></property>
        <property name="cAccount" value="124"></property>
    </bean>

    <!--3-->
    <bean id="person3" class="com.pojo.Person">
        <property name="child" ref="child3"></property>
        <property name="pName" value="lcb"></property>
        <property name="pAccount" value="123"></property>
    </bean>

    <bean id="child3" class="com.pojo.Child">
        <property name="cName" value="laj"></property>
        <property name="cAccount" value="124"></property>
    </bean>

    <!--
        使用p:的形式
        p表示对该bean里面的属性进行注入,格式为p:属性名=注入的对象
        效果与在bean里面使用<property>标签一样
     -->

    <!--4、通过内嵌bean实现注入-->
    <bean id="person4" class="com.pojo.Person" p:pAccount="123" p:pName="lcb">
        <property name="child">
            <bean class="com.pojo.Child" p:cName="laj" p:cAccount="124">
            </bean>
        </property>
    </bean>

    <!--5、通过p:child-ref连接另一个bean实现注入-->
    <bean id="person5" class="com.pojo.Person" p:pAccount="123" p:pName="lcb" p:child-ref="child5">
    </bean>
    <bean id="child5" class="com.pojo.Child">
        <property name="cName" value="laj"></property>
        <property name="cAccount" value="124"></property>
    </bean>

    <!--6、通过p:child-ref连接另一个bean实现注入(比较简洁)-->
    <bean id="person6" class="com.pojo.Person" p:pAccount="123" p:pName="lcb" p:child-ref="child6">
    </bean>
    <bean id="child6" class="com.pojo.Child" p:cName="laj" p:cAccount="124">
    </bean>
</beans>

3.4、测试类MainTest.java

package com;

import com.pojo.Person;
import com.pojo.Test;
import com.pojo.TestCollection;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainTest {
    public static void main(String[] args) {
        //构造器注入的测试
        test();
        //集合注入测试
        testCollection();
        //1、根据byName注入(测试前需要注释Beans.xml中的“二、通过byName注入”、“三、如果不指定autowire的类型,可以通过以下方式实现注入”这部分内容,
        // 即不能找到超过一个Child实体类,否则会有异常产生)
        //2、根据byType注入(测试前需要注释Beans.xml中的“一、通过byType注入”这部分内容)
        testAutowire("person0");
        //3、没有指定autowire的类型
        testAutowire("person1");
        testAutowire("person2");
        testAutowire("person3");
        testAutowire("person4");
        testAutowire("person5");
        testAutowire("person6");
    }

    /**
     * 测试三种注入构造器的方式
     */
    private static void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("java/src/Bean1.xml");
        Test test = (Test) context.getBean("test");
        System.out.println("注入年龄:"+test.getAge()+"--"+test.getChild());
    }

    /**
     * 测试集合注入
     */
    private static void testCollection(){
        /*D:\code\idea-code\springdemo1\src\main\resources\java\src\Bean1.xml*/
        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("src/main/resources/java/src/Bean2.xml");
        TestCollection testCollection = (TestCollection) context.getBean("testCollection");
        System.out.println("<-- 集合测试开始 -->");
        testCollection.getList();
        testCollection.getMap();
        testCollection.getSet();
        testCollection.getProperties();
        System.out.println("<-- 集合测试结束 -->");
    }

    /**
     * 测试spring的自动注入
     */
    private static void testAutowire(String p) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("java/src/Beans.xml");
        Person person = (Person) context.getBean(p);
        String cName = person.getChild().getcName();
        String cAccount = person.getChild().getcAccount();
        System.out.println(p + "==>账号:" + person.getpAccount() + ",用户名:" + person.getpName() + ",子信息:账号:" + cAccount + ",用户名:" + cName);
    }



}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚风岸影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值