Bean自动装配,注解

7 Bean的自动装配

  • 自动装配是spring满足bean依赖的一种方式
  • spring会在上下文中自动寻找,并自动给bean配置属性

在spring中有三种装配的方式

  1. xml中显示配置
  2. java中显示配置
  3. 隐式的自动装配bean

7.1 测试

环境搭建:一个人有两个宠物

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


    <bean id="cat2" class="com.kuang.pojo.Cat"/>
    <bean id="dog1" class="com.kuang.pojo.Dog"/>

    <!---
    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
    byType:会自动在容器上下文中查找,和自己对象属性类型对应的beanid
    -->
    <bean id="person" class="com.kuang.pojo.Person" autowire="byType">
        <property name="name" value="liutao"/>
    </bean>


</beans>

总结:

  • byname:保证所有beanid唯一,并且这个bean需要和自动注入的属性的set方法的值一致;
  • byType:保证所有beanclass唯一,并且这个bean需要和自动注入的属性类型一致;

7.4 使用注解实现自动装配

要使用注解须知:
1.导入约束
2.配置注解的支持 : context:annotation-config/

<?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方法上使用
使用AutoWired 我们不同编写set方法,前期在spring中存在

科普:
1@nullable 字段标记了这个注释,说明这个字段可以为null

如果@AutoWired自动装配的环境比较复杂,自动装配无法通过一个注解【@AutoWired】完成的时候,可以使用@qualifier(value= “xxx”)配合使用,指定一个唯一的bean

public class Person {
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    @Autowired
    @Qualifier(value = "cat111")
    private Cat cat;
    private String name;

    public Dog getDog() {
        return dog;
    }

    public Cat getCat() {
        return cat;
    }

    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}

@Rescource注解
小结
@Rescource和 @Autowired区别

  • 都自动装配,都可以放在属性字段上
  • @Autowired bytype方式实现,必须要求这个对象存在
  • @Rescource byname 然后bytype,如果两个都找不到,就会报错
  • 执行顺序不同
  • component:组件,放在类上,说明这个类被spring管理了,就是bean
  • 属性如何注入

//@Component等价于<bean id="user" class="com.kuang.pojo.User">
@Component

public class User {
    @Value("Liutao")
    //等价于<property name="name" value="Liutao"/>
    public String name ;
}
  • 衍生的注解
    @Component有几个衍生注解,在web开发中,会按照mvc三层架构分层
    • dao【@Repository】
    • service【@service】
    • controller【@Controller】
      这四个注解的功能是一样的,都是代表将某个类注册到spring中,装配Bean
  • 自动装配注解
  • 作用域
//@Component等价于<bean id="user" class="com.kuang.pojo.User">
@Component
@Scope("singleton")

public class User {
    @Value("Liutao")
    //等价于<property name="name" value="Liutao"/>
    public String name ;
}
  • 小结
    xml与注解:
    • xml更加万能,适用于任何场合!维护简单方便
    • 注解不是自己的类使用不了,维护相对复杂
      xml与注解的最佳时间
    • xml用来管理bean
    • 注解只负责完成属性的注入
    • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,需要开启注解支持
	<context:component-scan base-package="com.kuang"/>
    <context:annotation-config/>
- 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值