您的位置:首页 > 编程语言 > Java开发

Spring入门——Bean管理的注解实现及例子

2017-02-16 10:44 417 查看


Classpath扫描与组件管理

从Spring3.0开始,Spring javaConfig项目提供了很多特性,包括使用Java而不是xml定义bean,比如
@Configuration,@Bean,@Import,@DependsOn
@Component是一个通用注解,可以用于任何bean
@Repository,@Service,@Controller是更有针对性的注解
-@Repository通常用于注解DAO类,即持久层
-@Service通常用于注解Service类,即服务层
-@Controller通常用于Controller类,即控制层(MVC)

类的自动检测与注册Bean
Spring可以自动检测类并注册Bean到ApplicationContext中






<context:annotation-config/>

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
  类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明
CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation-
config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。

通过在基于XML的Spring配置如下标签(请注意包含上下文命名空间)
<context:annotation-config/>仅会查找到同一个applicationContext中的bean注解



为了能够检测这些类并注册相应的Bean,需要下面内容



<context:component-scan>包含<context:annotation-config/>,通常在使用前者后,不再使用后者

使用过滤器进行自定义扫描

默认情况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用@component的自定义注解
可以通过过滤器修改上面的行为,如:下面例子的xml配置忽略所有的@Repository注解并用“stub”代替



include表示包含下面的内容,regex是以通配符的形式,exclude表示排除,排除的形式是annotation也就是注解,而这个注解是所有以Repository进行的注解
还可以使用user-default-filters="false"禁用自动发现与注册

定义bean

扫描过程中组件被自动检测,那么Bean名称是由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显示设置Bean Name)当没有指定名称是通常生成的名称为首字母小写的类名称,一般我们都会自定义名称



可以定义bean命名策略,实现BeanNameGenerator接口,并一定要包含一个无参数构造函器



作用域(Scope)

通常情况下自动查找的Spring组件,其scope是singleton,Spring2.5提供了一个标识scope的注解@Scope



也可以自定义Scope策略,实现ScopeMetadataResolver接口并提供一个无参构造器



代理方式

可以使用scoped-proxy属性指定代理,有三个值可选:no,interfaces,targetClass默认情况下是no



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-4.2.xsd" >

<context:component-scan base-package="com.txr.beanannotation"/>

</beans>
BeanAnnotation类
package com.txr.beanannotation;

import org.springframework.stereotype.Component;

@Component("bean")
public class BeanAnnotation {

public void say(String word)
{
System.out.println("BeanAnnotation :"+word);
}
}
测试
@Test
public void testAnnotationSay()
{
BeanAnnotation annotation=(BeanAnnotation)context.getBean("bean");
annotation.say("hello txr!!");
}
测试结果
BeanAnnotationhello txr!!


Scope测试
在BeanAnnotation中添加Scope注解
package com.txr.beanannotation;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Scope("prototype")
@Component("bean")
public class BeanAnnotation {

public void say(String word)
{
System.out.println("BeanAnnotation :"+word);
}
}


测试
@Test
public void testAnnotationScope()
{
BeanAnnotation annotation=(BeanAnnotation)context.getBean("bean");
BeanAnnotation annotation1=(BeanAnnotation)context.getBean("bean");
System.out.println(annotation==annotation1);
}


测试结果
false


类的自动检测与注册Bean
<context:annotation-config/>
@Component,@Repository,@Service,@Controller
@Required
@Autowired
@Qualifier
@Resource
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: