Spring02--自动装配、注解开发、AOP

1Bean的自动装配

自动装配在spring满足bean依赖的一种方式,spring会在上下文自动寻找,并自动给bean装配属性!!!【为某个bean寻找其依赖的bean】

在Spring中有三种装配的方式

  1. 在xml中显式的配置

  2. 在Java中显式配置

  3. 隐式的自动配置bean【重要】

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

    1. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
    2. 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;

    组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。

    推荐不使用自动装配xml配置 , 而使用注解 .

1.1测试

1搭建环境

实体类三个,猫狗和人,一个人有两个宠物

public class Dog {
    public  void shout(){
        System.out.println("wow");
    }
}
public class Cat {
    public  void shout(){
        System.out.println("miao");
    }
}
public class People {
    private String name;
    private Cat cat;
    private Dog dog;
    还有些方法
    }

2编写spring配置文件

    <bean id="cat" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People">
    <property name="name" value="wyq"/>
    <property name="cat" ref="cat"/>
    <property name="dog" ref="dog"/>
</bean>

3测试

public class Mytest {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = (People) context.getBean("people");
       people.getCat().shout();
       people.getDog().shout();
    }
}

结果:输出正常,环境ok

1.2按名字自动装配【autowire=“byName”】

由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。

采用自动装配将避免这些错误,并且使配置简单化。

测试:

  1. 修改bean配置,增加一个属性 autowire=“byName”

        <bean id="cat" class="com.kuang.pojo.Cat"/>
        <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People" autowire="byName">
        <property name="name" value="wyq"/>
    </bean>
    
  2. 再次测试,结果依旧成功输出

  3. 我们将 cat 的bean id修改为 catXXX,再次测试, 执行时报空指针java.lang.NullPointerException。因为按byName规则找不对应set方法,真正的setCat就没执行,对象就没有初始化,所以调用时就会报空指针错误

小结:

当一个bean节点带有 autowire byName的属性时。

  1. 将查找其类所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
  2. 去spring容器中寻找是否有此字符串名称id的对象
  3. 如果有,就取出注入;如果没有,就报空指针异常。

1.3按类型自动装配[autowire=“byType”]

注意:使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常【NoUniqueBeanDefinitionException】。

按类型装配会自动在容器上下文查找,和自己对象属性类型相同的bean

   <bean id="cat123" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People" autowire="byType">
    <property name="name" value="wyq"/>
</bean>

测试:

1、将user的bean配置修改一下 : autowire=“byType”

2、测试,正常输出

3、再注册一个cat 的bean对象!

     <bean id="cat123" class="com.kuang.pojo.Cat"/>
    <bean id="catg" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People" autowire="byType">
    <property name="name" value="wyq"/>
</bean>

最后一个bean飘红了,测试,报NoUniqueBeanDefinitionException

4、删掉cat2,将cat的bean名称改掉!测试!因为是按类型装配,所以并不会报异常,也不影响最后的结果。甚至将id属性去掉,也不影响结果。

byname是通过上下文,bytype看类型就行。

总结:

  • byName的时候,要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
  • byType要保证所有bean的class唯一,这个bean需要和自动注入的属性的类型一致

1.4使用注解自动装配【导context】

jdk1.5开始支持注解,spring2.5开始全面支持注解。

准备工作:利用注解的方式注入属性。

1导入约束-----context的约束,导入context文件头

xmlns:context=“http://www.springframework.org/schema/context”

2配置注解的支持

http://www.springframework.org/schema/contex

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

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

</beans>

1.4.1@Autowired【bytype】

  • @Autowired是按类型自动转配的,不支持id匹配。

  • 需要导入 spring-aop的包!

  • 直接在属性上使用也可以在set方法上使用

  • 使用autowired就可以不写set方法了,前提是自动装配的属性在IOC(Spring)容器中存在且符合byName

    测试

    1可以将User类中的set方法去掉【此时set方法有无都可】,在属性上面使用@Autowired注解

public class People {
    private String name;
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    }

2配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/context/spring-aop.xsd">

    <context:annotation-config/>
    <bean id="cat" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People" />

</beans>

注意:这种情况下,如果猫或者狗的bean的id写的不是cat或者dog就会报错!!!

补充:

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。

//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

1.4.2@Qualifier

  • @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
  • @Qualifier不能单独使用

测试

1、配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!

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

2、没有加Qualifier测试,直接报错

因为有两个猫,两个狗,无论是通过,名字还是通过类型都找不到。需压迫@Qualifier指定某个类实现哪个。如果给猫装一个狗的id直接报错

3、在属性上添加Qualifier注解

public class People {
    private String name;
    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;
    @Autowired
    @Qualifier(value = &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值