使用注解自动化装配Bean

Spring从两个角度来实现自动化装配:
1、组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。让Spring能够自动识别哪些类需要被配置成Bean,从而减少对<\bean>元素的使用。
2、自动装配(autowiring):Spring自动满足bean之间的依赖。

1、组件扫描
组件扫描默认是不启用的,如果使用XML来启用组件扫描的话,需要在Maven工程中的src/resources目录下创建一个Spring配置文件XXX-context.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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--有助于完全消除Spring中的property和constructor-arg元素,但是还是需要bean元素显式定义Bean-->
    <context:annotation-config/>

    <!--既能消除property和constructor-arg元素,还允许自动检测Bean和定义Bean-->
    <!--会扫描制定的包及其所有的子包-->
    <context:component-scan base-package="org.springaction"/>

</beans>

默认情况下,<\context:component-scan>可以将下面注解所标注的类,自动扫描:
1、@Component :通用的构造型注解,标识该类为Spring组件.
2、@Controller :标识将该类定义为Spring MVC controller。
3、@Repository :标识将该类定义为数据仓库。
4、@Service :标识将该类定义为服务

使用component-scan进行扫描时,会使用默认的构造器来创建bean。

import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc {
    public void play() {
      System.out.println("test");
    }
}

测试一下:

//要新建在测试test目录中

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springaction.CompactDisc;
import org.springaction.SgtPeppers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//自动创建Spring的应用上下文
@RunWith(SpringJUnit4ClassRunner.class)
//需要加载的配置地址
@ContextConfiguration(locations = {"classpath:smart.xml"})
public class CDPlayTest {

    @Autowired
    private SgtPeppers cd;

    @Test
    public void doplay(){
        cd.play();
    }
}
//结果说明自动装配成功

Spring应用上下文中所有的bean都会给定一个ID,如果没有明确设置ID,那么Spring会根据类名为其制定一个ID。具体来讲,这个bean所给定的ID就是将类名的第一个字母变成小写。

也可以为这个bean设置不同的ID,例如:

//设置新的ID
@Component("lonely")
public class SgtPeppers implements CompactDisc {
    public void play() {
      System.out.println("test");
    }
}

过滤组件扫描
如果基于注解让所有实现某一接口的类都自动注册,不得不浏览接口实现类的原码,并用@Component(或其他)标注它们。这是不方便的,如果使用第三方的接口实现,或许没有源码的访问权限来添加注解。

替换掉基于注解的组件扫描策略,增加一个包含过滤器来要求<\context:component-scan>自动注册所有的接口实现类。例如:

<context:component-scan base-package="org.springaction">
        <!--过滤器类型是:assignable,派生于该接口CompactDisc的所有类自动注册为Bean-->
        <context:include-filter type="assignable" expression="org.springaction.CompactDisc"/>
        <!--过滤器类型是:annotation,使用自定义的@Myauto注解的类不需要注册为Bean,其他都注册为Bean-->
        <context:exclude-filter type="annotation" expression="org.springaction.Myauto"/>
    </context:component-scan>

过滤器类型:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值