龙口网站设计,wordpress可以做门户网站,网站建设团队名称,网页升级紧急通知记好转载自 零配置 之 12.3 注解实现Bean定义 ——跟我学spring3
12.3 注解实现Bean定义
12.3.1 概述
前边介绍的Bean定义全是基于XML方式定义配置元数据#xff0c;且在【12.2注解实现Bean依赖注入】一节中介绍了通过注解来减少配置数量#xff0c;但并没有完全消除在XML…转载自 零配置 之 12.3 注解实现Bean定义 ——跟我学spring3
12.3 注解实现Bean定义
12.3.1 概述
前边介绍的Bean定义全是基于XML方式定义配置元数据且在【12.2注解实现Bean依赖注入】一节中介绍了通过注解来减少配置数量但并没有完全消除在XML配置文件中的Bean定义因此有没有方式完全消除XML配置Bean定义呢 Spring提供通过扫描类路径中的特殊注解类来自动注册Bean定义。同注解驱动事务一样需要开启自动扫描并注册Bean定义支持使用方式如下resources/chapter12/ componentDefinitionWithAnnotation.xml
beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aop xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocation http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd aop:aspectj-autoproxy / context:component-scan base-packagecn.javass.spring.chapter12/ /beans 使用context:component-scan标签来表示需要要自动注册Bean定义而通过base-package属性指定扫描的类路径位置。 context:component-scan标签将自动开启“注解实现Bean依赖注入”支持。 此处我们还通过aop:aspectj-autoproxy/用于开启Spring对AspectJ风格切面的支持。 Spring基于注解实现Bean定义支持如下三种注解
Spring自带的Component注解及扩展Repository、Service、Controller如图12-1所示JSR-250 1.1版本中中定义的ManagedBean注解是Java EE 6标准规范之一不包括在JDK中需要在应用服务器环境使用如Jboss如图12-2所示JSR-330的Named注解如图12-3所示。图12-1 Spring自带的Component注解及扩展 图12-2 JSR-250中定义的ManagedBean注解及自定义扩展 图12-3 JSR-330的Named注解及自定义扩展 图12-2和图12-3中的自定义扩展部分是为了配合Spring自带的模式注解扩展自定义的并不包含在Java EE 6规范中在Java EE 6中相应的服务层、DAO层功能由EJB来完成。 在Java EE中有些注解运行放置在多个地方如Named允许放置在类型、字段、方法参数上等因此一般情况下放置在类型上表示定义放置在参数、方法等上边一般代表使用如依赖注入等等。 12.3.2 Spring自带的Component注解及扩展
一、Component定义Spring管理Bean使用方式如下
Component(标识符)
POJO类 在类上使用Component注解表示该类定义为Spring管理Bean使用默认value可选属性表示Bean标识符。
1、定义测试Bean类:
package cn.javass.spring.chapter12;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
Component(component)
public class TestCompoment { Autowired private ApplicationContext ctx; public ApplicationContext getCtx() { return ctx; }
} 2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改 3、定义测试类和测试方法
package cn.javass.spring.chapter12;
//省略import
public class ComponentDefinitionWithAnnotationTest { private static String configLocation classpath:chapter12/componentDefinitionWithAnnotation.xml; private static ApplicationContext ctx new ClassPathXmlApplicationContext(configLocation); Test public void testComponent() { TestCompoment component ctx.getBean(component, TestCompoment.class); Assert.assertNotNull(component.getCtx()); }
} 测试成功说明被Component注解的POJO类将自动被Spring识别并注册到Spring容器中且自动支持自动装配。 AspectJ风格的切面可以通过Compenent注解标识其为Spring管理Bean而Aspect注解不能被Spring自动识别并注册为Bean必须通过Component注解来完成示例如下
package cn.javass.spring.chapter12.aop;
//省略import
Component
Aspect
public class TestAspect { Pointcut(valueexecution(* *(..))) private void pointcut() {} Before(valuepointcut()) public void before() { System.out.println(before); }
} 通过Component将切面定义为Spring管理Bean。 二、RepositoryComponent扩展被Repository注解的POJO类表示DAO层实现从而见到该注解就想到DAO层实现使用方式和Component相同
1、定义测试Bean类:
package cn.javass.spring.chapter12.dao.hibernate;
import org.springframework.stereotype.Repository;
Repository(testHibernateDao)
public class TestHibernateDaoImpl { }
2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testDao() {
TestHibernateDaoImpl dao
ctx.getBean(testHibernateDao, TestHibernateDaoImpl.class);
Assert.assertNotNull(dao);
} 测试成功说明被Repository注解的POJO类将自动被Spring识别并注册到Spring容器中且自动支持自动装配并且被Repository注解的类表示DAO层实现。 三、ServiceComponent扩展被Service注解的POJO类表示Service层实现从而见到该注解就想到Service层实现使用方式和Component相同
1、定义测试Bean类:
package cn.javass.spring.chapter12.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.javass.spring.chapter12.dao.hibernate.TestHibernateDaoImpl;
Service(testService)
public class TestServiceImpl { Autowired Qualifier(testHibernateDao) private TestHibernateDaoImpl dao; public TestHibernateDaoImpl getDao() { return dao; }
} 2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testService() { TestServiceImpl service ctx.getBean(testService, TestServiceImpl.class); Assert.assertNotNull(service.getDao());
}
测试成功说明被Service注解的POJO类将自动被Spring识别并注册到Spring容器中且自动支持自动装配并且被Service注解的类表示Service层实现。 四、ControllerComponent扩展被Controller注解的类表示Web层实现从而见到该注解就想到Web层实现使用方式和Component相同
1、定义测试Bean类:
package cn.javass.spring.chapter12.action;
//省略import
Controller
public class TestAction { Autowired private TestServiceImpl testService; public void list() { //调用业务逻辑层方法 }
}
2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testWeb() { TestAction action ctx.getBean(testAction, TestAction.class); Assert.assertNotNull(action);
} 测试成功说明被Controller注解的类将自动被Spring识别并注册到Spring容器中且自动支持自动装配并且被Controller注解的类表示Web层实现。
大家是否注意到Controller中并没有定义Bean的标识符那么默认Bean的名字将是以小写开头的类名不包括包名即如“TestAction”类的Bean标识符为“testAction”。 六、自定义扩展Spring内置了三种通用的扩展注解Repository、Service、Controller 大多数情况下没必要定义自己的扩展在此我们演示下如何扩展Component注解来满足某些特殊规约的需要
在此我们可能需要一个缓存层用于定义缓存Bean因此我们需要自定义一个Cache的注解来表示缓存类。
1、扩展Component
package cn.javass.spring.chapter12.stereotype;
//省略import
Target({ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
Documented
Component
public interface Cache{ String value() default ;
} 扩展十分简单只需要在扩展的注解上注解Component即可Repository、Service、Controller也是通过该方式实现的没什么特别之处
2、定义测试Bean类:
package cn.javass.spring.chapter12.cache;
Cache(cache)
public class TestCache { }
2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testCache() { TestCache cache ctx.getBean(cache, TestCache.class); Assert.assertNotNull(cache);
} 测试成功说明自定义的Cache注解也能很好的工作而且实现了我们的目的使用Cache来表示被注解的类是Cache层Bean。 12.3.3 JSR-250中定义的ManagedBean注解
javax.annotation.ManagedBean需要在实现Java EE 6规范的应用服务器上使用虽然Spring3实现了但javax.annotation.ManagedBean只有在Java EE 6环境中才有定义因此测试前需要我们定义ManagedBean类。
1、定义javax.annotation.ManagedBean注解类
package javax.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
public interface ManagedBean { String value() default ;
}
其和Component完全相同唯一不同的就是名字和创建者一个是Spring一个是Java EE规范。
2、定义测试Bean类:
package cn.javass.spring.chapter12;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
javax.annotation.ManagedBean(managedBean)
public class TestManagedBean { Resource private ApplicationContext ctx; public ApplicationContext getCtx() { return ctx; }
} 2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testManagedBean() { TestManagedBean testManagedBean ctx.getBean(managedBean, TestManagedBean.class); Assert.assertNotNull(testManagedBean.getCtx());
} 测试成功说明被ManagedBean注解类也能正常工作。
自定义扩展就不介绍了大家可以参考Component来完成如图12-2所示的自定义扩展部分。 12.3.4 JSR-330的Named注解
Named不仅可以用于依赖注入来指定注入的Bean的标识符还可以用于定义Bean。即注解在类型上表示定义Bean注解在非类型上如字段表示指定依赖注入的Bean标识符。
1、定义测试Bean类:
package cn.javass.spring.chapter12;
//省略import
Named(namedBean)
public class TestNamedBean { Inject private ApplicationContext ctx; public ApplicationContext getCtx() { return ctx; }
}
2、Spring配置文件使用chapter12/ componentDefinitionWithAnnotation.xml即可且无需修改
3、定义测试方法
Test
public void testNamedBean() {
TestNamedBean testNamedBean ctx.getBean(namedBean, TestNamedBean.class); Assert.assertNotNull(testNamedBean.getCtx());
}
测试成功说明被Named注解类也能正常工作。
自定义扩展就不介绍了大家可以参考Component来完成如图12-3所示的自定义扩展部分。 12.3.5 细粒度控制Bean定义扫描
在XML配置中完全消除了Bean定义而是只有一个context:component-scan标签来支持注解Bean定义扫描。
前边的示例完全采用默认扫描设置如果我们有几个组件不想被扫描并自动注册、我们想更改默认的Bean标识符生成策略该如何做呢接下来让我们看一下如何细粒度的控制Bean定义扫描具体定义如下
context:component-scan base-package resource-pattern**/*.class name-generatororg.springframework.context.annotation.AnnotationBeanNameGenerator use-default-filterstrue annotation-configtrue context:include-filter typeaspectj expression/ context:exclude-filter typeregex expression/
/context:component-scan
base-package表示扫描注解类的开始位置即将在指定的包中扫描其他包中的注解类将不被扫描默认将扫描所有类路径resource-pattern表示扫描注解类的后缀匹配模式即“base-packageresource-pattern”将组成匹配模式用于匹配类路径中的组件默认后缀为“**/*.class”即指定包下的所有以.class结尾的类文件name-generator默认情况下的Bean标识符生成策略默认是AnnotationBeanNameGenerator其将生成以小写开头的类名不包括包名可以自定义自己的标识符生成策略use-default-filters默认为true表示过滤Component、ManagedBean、Named注解的类如果改为false默认将不过滤这些默认的注解来定义Bean即这些注解类不能被过滤到即不能通过这些注解进行Bean定义annotation-config表示是否自动支持注解实现Bean依赖注入默认支持如果设置为false将关闭支持注解的依赖注入需要通过context:annotation-config/开启。
默认情况下将自动过滤Component、ManagedBean、Named注解的类并将其注册为Spring管理Bean可以通过在context:component-scan标签中指定自定义过滤器将过滤到匹配条件的类注册为Spring管理Bean具体定义方式如下
context:include-filter typeaspectj expression/
context:exclude-filter typeregex expression/
context:include-filter表示过滤到的类将被注册为Spring管理Bean
context:exclude-filter表示过滤到的类将不被注册为Spring管理Bean它比context:include-filter具有更高优先级
type表示过滤器类型目前支持注解类型、类类型、正则表达式、aspectj表达式过滤器当然也可以自定义自己的过滤器实现org.springframework.core.type.filter.TypeFilter即可
expression表示过滤器表达式。
一般情况下没必要进行自定义过滤如果需要请参考如下示例
1、cn.javass.spring.chapter12.TestBean14自动注册为Spring管理Bean
context:include-filter typeassignable expressioncn.javass.spring.chapter12.TestBean14/ 2、把所有注解为org.aspectj.lang.annotation.Aspect自动注册为Spring管理Bean
context:include-filter typeannotation
expressionorg.aspectj.lang.annotation.Aspect/
3、将把匹配到正则表达式“cn\.javass\.spring\.chapter12\.TestBean2*”排除不注册为Spring管理Bean
context:exclude-filter typeregex expressioncn\.javass\.spring\.chapter12\.TestBean2*/
4、将把匹配到aspectj表达式“cn.javass.spring.chapter12.TestBean3*”排除不注册为Spring管理Bean
context:exclude-filter typeaspectj expressioncn.javass.spring.chapter12.TestBean3*/
具体使用就要看项目需要了如果以上都不满足需要请考虑使用自定义过滤器。 12.3.6 提供更多的配置元数据
1、Lazy定义Bean将延迟初始化使用方式如下
Component(component)
Lazy(true)
public class TestCompoment {
……
} 使用Lazy注解指定Bean需要延迟初始化。
2、DependsOn定义Bean初始化及销毁时的顺序使用方式如下
Component(component)
DependsOn({managedBean})
public class TestCompoment {
……
}
3、Scope定义Bean作用域默认单例使用方式如下
Component(component)
Scope(singleton)
public class TestCompoment {
……
}
4、Qualifier指定限定描述符对应于基于XML配置中的qualifier标签使用方式如下
Component(component)
Qualifier(component)
public class TestCompoment {
……
} 可以使用复杂的扩展如Mysql等等。
5、Primary自动装配时当出现多个Bean候选者时被注解为Primary的Bean将作为首选者否则将抛出异常使用方式如下
Component(component)
Primary
public class TestCompoment {
……
}