Spring入门——Bean管理的注解实现及例子
2017-02-16 10:44
417 查看
从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中
当我们需要使用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默认情况下是noBean定义及作用域例子:
配置文件:<?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
相关文章推荐
- Spring4 Bean管理的注解实现
- Spring入门_bean配置与注解实现
- Spring2.0用注解实现事务管理
- Spring2.0注解实现事务管理
- 利用spring AOP注解实现日志管理
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- Spring2.0用注解实现事务管理
- Spring入门Blog[五、Spring容器组建注解@Component和Resouces实现完全注解配置]
- 【第十二章】零配置 之 12.1 概述 12.2 注解实现Bean依赖注入 ——跟我学spring3
- Spring in Action入门之装配管理Bean
- Spring注解实现Bean定义
- flex4 + spring + blazeds , 使用anonation(注解)机制,利用push技术的实现例子和过程。
- 【第十二章】零配置 之 12.3 注解实现Bean定义、12.4基于Java类定义Bean配置元数据 ——跟我学spring3
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- flex4 + spring + blazeds , 使用anonation(注解)机制,利用push技术的实现例子和过程。
- AOP入门的简单例子(Spring AOP实现)
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- 零配置 之 12.3 注解实现Bean定义 ——跟我学spring3
- spring3——注解实现Bean依赖注入