Spring——Bean的自动装配

自动装配是使用spring满足bean依赖的一种方法,spring会在应用上下文中为某个bean寻找其依赖的bean。

Spring中bean有三种装配机制,分别是:

  • 在xml中显式配置;

  • 在java中显式配置;

  • 隐式的bean发现机制和自动装配。

这里的第三种就是自动化的装配bean。

Spring的自动装配需要从两个角度来实现,或者说是两个操作:

  • 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;

  • 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;

案例

新建三个pojo,Cat Dog 都有一个叫的方法,还有一个User:

public class Cat {
   public void shout() {
       System.out.println("miao~");
  }
}
public class Dog {
   public void shout() {
       System.out.println("wang~");
  }
}
public class User {
   private Cat cat;
   private Dog dog;
   private String str;
}

编写Spring的配置文件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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.hyq.pojo.Dog"/>
   <bean id="cat" class="com.hyq.pojo.Cat"/>

   <bean id="user" class="com.hyq.pojo.User">
       <property name="cat" ref="cat"/>
       <property name="dog" ref="dog"/>
       <property name="str" value="huyiqi"/>
   </bean>
</beans>

这是以前我们手动装配的bean

这里的自动装配又两种:

  • byName
  • byType

byName: 它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

		<bean id="dog" class="com.hyq.pojo.Dog"/>
   	 	<bean id="cat" class="com.hyq.pojo.Cat"/>

 <!--autowire byName (按名称自动装配)-->
    <bean id="user" class="com.hyq.pojo.User" autowire="byName">
        <property name="str" value="huyiqi"/>
    </bean>

byName: 如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

		<bean id="dog" class="com.hyq.pojo.Dog"/>
   	 	<bean id="cat" class="com.hyq.pojo.Cat"/>

 <!-- autowire byType (按类型自动装配)-->
  <bean id="user" class="com.hyq.pojo.User" autowire="byType">
        <property name="str" value="huyiqi"/>
    </bean>

成功结果:
在这里插入图片描述

自动装配(注解开发)

需要spring的配置文件引入:

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

并且开启注解支持

<context:annotation-config/>

@Autowired注解:

  • @Autowired是按类型自动转配的,不支持id匹配。
  • 需要导入 spring-aop的包!
public class User {
   @Autowired
   private Cat cat;
   @Autowired
   private Dog dog;
   private String str;

   public Cat getCat() {
       return cat;
  }
   public Dog getDog() {
       return dog;
  }
   public String getStr() {
       return str;
  }
}

这时在spring配置文件中只需要这点就可以:

<context:annotation-config/>

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

结果与刚刚输出一致则为成功。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Aholic 冲冲冲

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

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

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

打赏作者

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

抵扣说明:

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

余额充值