Spring-Bean的定义装配以及作用域的注解实现

[IOC]

  • Classpath扫描与组件管理
  • 类的自动检测与注册Bean
  • \< context:annotation-config/>
  • @Component,@Repository,@Service,@Controller
  • @Required
  • @Autowired
  • @Qualifier
  • @Resource

1. Classpath扫描与组件管理

从Spring3.0开始,Spring JavaConfig项目提供了很多特性,其中包括使用java而不是XML定义Bean,比如 @Configuration,@Bean,@Import,@DependsOn
@Component是一个通用注解,可以用于任何Bean
@Repository,@Service,@Controller是更具有针对性的注解,分别用于注解DAO(持久层)、Service(服务层)、Controller(控制层 )

元注解(Meta-annotations) qualifier?

2.类的自动检测与注册Bean

@Repository
public class JpaSongFinder {
}

在类上注解会被Spring扫描到,并将其作为一个bean注入到ApplicationContext中去。

2.01.< context:annotation-config/>

< context:annotation-config/>仅会查找在同一个application context中的bean注解。

<?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" >
        < context:annotation-config base-package="com.your.package" />
</beans>
2.02.< context:component-scan/>
<context:component-scan base-package="com.your.package"/>

自动扫描制定包下的所有Bean,包含< context:annotation-config>,也就可以不使用 < context:annotation-config>.

  • AutowiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
    都会被包含进来。
2.03.使用过滤器进行自定义扫描

在默认< context:component-scan/>扫描的情况下,类被扫描识别并注册的条件是使用:
1. @Component,(或者自定义注解)
2. @Repository,
3. @Service,
4. @Controller
任意一个注解。
我们想要修改这个默认设定时,可以通过过滤器修改。
比如:忽略所有@Controller注解,并用Sdut代替

<beans>
    <context:component-scan base-package="com.your.package">
        <context:include-filter type="regex"
                 experession=".Sdut.*Controller"/>
        <context:exclude-filter type="annotation"
                 experession="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

还可以使用use-default-filters=”false” 禁用自动发现与注册。

@Required

这个注解仅指定这个Bean(属性)在配置时就初始化(填充)。
适用于Bean属性的setter方法.

@Required
public void setMovieFinder(MovieFinder movieFinder){
    this.movieFinder = movieFinder;
}

配合@Autowired使用方法为:

@Autowired(required=ture)
public void setMovieFinder(MovieFinder movieFinder){
    this.movieFinder = movieFinder;
}
  • !注意每个类只能有一个构造器被标记为required=ture

@Autowired

自动装配,相当于CDI的@Inject。

@Autowired
private UserService userService;
  1. 可以注解标准的解析依赖接口:BeanFactory,ApplicationContext,Environment
private Map<String,MovieCatalog> movieCatalogs

@Autowired
public void setMovieCatalogs(Map<String,MovieCatalog> movieCatalogs){
    this.movieCatalogs = movieCatalogs;
}

(其中:Bean可以结合org.springframework.core.Ordered接口或者@Order注解实现有序化。)

适用于fields,constructors,multi-argument methods这些允许在参数级别使用@Qualifier注解缩小范围的情况。

@Qualifier

按类型装配多个bean实例的情况下,可以使用@Qualifier注解来缩小范围:

@Qualifier("str")
自定义的@Qualifier注解

如果我们用@Qualifier注解符合规定的@interface,那么该@interface升级为我们自定义的注解。

  • 定义
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Teemo {
    String value();
}
  • 使用
@Autowired
@Teemo("Action")
private MovieCatalog actionCatalog;
private MovieCatalog comedyCatalog;

@Autowired
private void setComedyCatalog(@Teemo("Comedy") MovieCatalog comedyCatalog){
    this.comedyCatalog = comedyCatalog;
}

用于指定方法参数(包括构造器参数)。
也可用于注解集合类型的变量。

  • 虽然配合@Qualifier能实现通过名称实现指定自动装配的bean。但是一般通过@Resource注解来实现。而且@Resource可以弥补Autowired无法依据类型装载Map(集合)类型Bean的缺点。

@Resource

整理自:
http://www.imooc.com/video/4030
参考资料:
http://www.imooc.com/video/4030

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值