Spring 2.5的新特性:配置简化和基于注解的功能

豆豆网   技术应用频道   2008年01月10日  【字号: 收藏本文

内容摘要:本文是探讨这些新特性的3篇系列文章中的第一篇。本文将主要关注于简化的配置和在Spring应用程序上下文(application context)核心新增的基于注解的功能。查看“samples/petclinic”目录下的“readme.txt”文件可以得知关于如何构建和部署PetClinic应用程序。

@Autowired
@VetSpecialty("dentistry")
private Clinic dentistryClinic;

  在使用XML配置来达到依赖解析的目标时,'qualifier' 子元素可以被加注到bean定义中。在下文的组件扫描部分,我们将呈现一个可供选择的非XML方法。

id="dentistryClinic" class="samples.DentistryClinic">
type="example.VetSpecialty" value="dentistry"/>

  为了避免对@Qualifier注解的任何依赖性,可以在Spring context中提供一个CustomAutowireConfigurer的bean定义并直接注册所有自定义注解类型:

<bean class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.VetSpecialty</value>
</set>
</property>
</bean>

  现在,自定义修饰符被显式声明了,就不再需要@Qualifier这个元注解符了。

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE,
ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface VetSpecialty { ... }

  其实,在配置AutowiredAnnotationBeanPostProcessor的时候,取代@Autowired注解都是有可能的。

class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
name="autowiredAnnotationType" value="example.Injected"/>

  大部分情况下,定义自定义‘标记’注解的能力结合通过名字或其他文法值进行匹配选项,足以完成自动装配过程的细粒度控制。但Spring还支持在qualifier注解上任意数目的任意属性。比如,下面是一个极为细粒度修饰的例子。

来源:InfoQ中文站    作者:沙晓兰    责编:豆豆技术应用

正在加载评论...