spring bean自动装配

Spring bean自动装配

原理:

  • spring可以在应用上下文中自动扫描,找到并给bean装配属性

方法:

  • byType:通过类型
  • byName:通过名字

测试

  1. 搭建环境

    一个person拥有两个宠物

  • person类:(省略 get set 构造 tostring)
public class Person {
    private String name;
    private Dog dog;
    private Cat cat;
}
  • 宠物
public class Cat {
    public void voice(){
        System.out.println("miao~~~~");
    }
}


public class Dog {
    public void voice(){
        System.out.println("wang~~~~");
    }
}

  • spring配置文件 (applicationContent.xml)byName
<?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="dog" class="com.yxx.pojo.Dog"/>
    <bean id="cat" class="com.yxx.pojo.Cat"/>

    <!--byName 根据名字匹配 -->
    <bean id="person" class="com.yxx.pojo.Person" autowire="byName">
        <property name="name" value="小明"/>
    </bean>
</beans>

byName注意点 :他是根据你的setXXX()方法与应用上下用中的bean中的id匹配,如果setXXX()中的XXX与bean中id不匹配则无法完成自动装配。

  • spring配置文件 (applicationContent.xml)byType
<?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  class="com.yxx.pojo.Dog"/>
    <bean  class="com.yxx.pojo.Cat"/>

    <!--byName 根据名字匹配 -->
<!--    <bean id="person" class="com.yxx.pojo.Person" autowire="byName">-->
<!--        <property name="name" value="小明"/>-->
<!--    </bean>-->

    <bean id="person" class="com.yxx.pojo.Person" autowire="byType">
        <property name="name" value="小明"/>
    </bean>
</beans>

byType注意点:同一个类型不能出现两个,只能出现一个。可以不写id属性

  • 测试代码
@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContent.xml");
    Person person = (Person) context.getBean("person");
    person.getCat().voice();
    person.getDog().voice();
}

使用注解实现自动装配

要使用注解的话,必须导入约束 xmlns:context=“http://www.springframework.org/schema/context”

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

必须知道几个注解:

  • @Autowired : 可以在属性上直接用,也可以在set方法上面用。原理:是先通过byName的方法自动装配,如果找不到在通过byType方法。如果byName找不到的情况下,配置文件(applicationContent.xml)中又有多个类型,则会出错
  • @Qualifier:与@Autowired 配合使用,出现上面出错的情况下使用这个注解,这个相当于byName
  • @Resource:这个是java原生的注解 我感觉和@Autowired是差不多的

测试

  • person类
public class Person {
    private String name;
    @Autowired
    @Qualifier(value = "dog11111")
    private Dog dog;
    @Autowired
    private Cat cat;
}
  • applicationContent.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--使用注解必须加上 -->
    <context:annotation-config/>

    <bean id="dog" class="com.yxx.pojo.Dog"/>
    <bean id="dog11111" class="com.yxx.pojo.Dog"/>
    <bean id="cat" class="com.yxx.pojo.Cat"/>
    <bean id="catiiiii" class="com.yxx.pojo.Cat"/>
    <bean id="person" class="com.yxx.pojo.Person"/>
</beans>
  • 测试代码与上面一样
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值