Spring JavaConfig参考文档

Spring JavaConfig参考文档

http://dodomail.iteye.com/blog/125989

Spring JavaConfig参考文档 
Spring JavaConfig Reference Documentation 
官方地址:  http://static.springsource.org/spring-javaconfig/docs/1.0-m2/reference/html/

Spring JavaConfig参考文档 
Spring JavaConfig Reference Documentation 
Rod Johnson 
Costin Leau 
version 1.0-m2 
Copies of this document may be made for your own use and for distribution to others, provided that you do not 
charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether 
distributed in print or electronically. 
2007.05.08


目录 
1. 介绍 
2. 组件 
2.1 @Configuration 
2.2 @Bean 
2.3 @ExternalBean 
2.4 @ScopedProxy 
3. Bean可见度 
4. 装配依赖 
5. 命名策略 
6. 混合XM和annotations 
7. 使用Java Configuration 
8. Roadmap 

第一章、介绍 
IoC中提到,Spring IoC核心为一个称为bean的概念。 
这个概念定义了一个对象被Spring容器初始化、装配和管理的方式。 
虽然Spring本身可以从任何metadata读取内容并装换成Java代码,但是XML是描述beans配置最流行的方式。 
JDK5+中引入的Annotations允许源代码组件提供额外的metadata,这些metadata可以影响运行时语义。 
这让annotations成为一个很好的配置选项。

第二章、组件 
Java Configuration使用annotations来让开发人员不离开Java世界就可以创建和配置beans。 
简短来说,开发人员使用Java代码来初始化和配置beans,然后指示容器使用它们。 
在继续之前,请注意,Spring仍保持相同的语义,而不管采用何种配置方式: Java或者XML。 
让我们看看JavaConfig依赖的最重要的annotations:

2.1 @Configuration 
@Configuration标记指示配置类: 

代码

  1. @Configuration  
  2. public class WebConfiguration {   
  3.   // bean definitions follow   
  4. }   
<script>render_code();</script>
@Configuration是一个class级别的annotation,它指示了配置里定义的bean的一些默认值。 

代码

  1. @Configuration(defaultAutowire = Autowire.BY_TYPE, defaultLazy = Lazy.FALSE)   
  2. public classDataSourceConfiguration extends ConfigurationSupport {}   
<script>render_code();</script>
它可以认为是<beans/>标签的替代品。 
用@Configuration标注的类继承ConfigurationSupport是明智的,因为该类提供了一些辅助方法。

2.2 @Bean 
@Bean的名字暗示了一个bean定义(<bean/>标签),让我们以一个简单的例子开始: 

代码

  1. @Bean (scope = DefaultScopes.SESSION)   
  2. public ExampleBean exampleBean() {   
  3.   return new ExampleBean();   
  4. }   
<script>render_code();</script>
上面的代码指示Spring容器使用方法名(作为bean的名字)和返回值(实际的bean实例)来创建一个bean。 
该bean拥有session作用域,这意味着调用exampleBean()方法将为每个HTTP会话创建一个新的bean实例。 
由于使用纯Java,我们在处理静态方法时没有必要使用factory-method

代码

  1. @Bean  
  2. public ExampleBean exampleBean() {   
  3.   return ExampleFactory.createBean();   
  4. }   
<script>render_code();</script>
或者使用FactoryBean/MethodInvokingFactoryBean来创建复杂对象: 

代码

  1. @Bean(aliases = {"anniversaries"})   
  2. public List<Date> birthdays() {   
  3.   List<Date> dates = new ArrayList<Date>();   
  4.   Calendar calendar = Calendar.getInstance();   
  5.   calendar.set(1977, 05, 28);   
  6.   dates.add(calendar.getTime());   
  7.   dates.add(computeMotherInLawBirthday());   
  8.   return dates;   
  9. }   
<script>render_code();</script>
@Bean是一个method级别的annotation并指示用来创建和配置一个bean实例的Java代码。 
该标记支持XML bean定义的大部分选项,如autowiringlazy-initdependency-checkdepends-onscoping。 
并且,lifecycle方法和*Aware接口完全支持: 

代码

  1. public class AwareBean implements BeanFactoryAware {   
  2.   private BeanFactory factory;   
  3.   // BeanFactoryAware setter   
  4.   public void setBeanFactory(BeanFactory beanFactory) throws BeansException {   
  5.     this.factory - beanFactory;   
  6.   }   
  7.   public void close() {   
  8.     // do clean-up   
  9.   }   
  10. }   
  11.   
  12. @Bean(destroyMethodName = "close", lazy = Lazy.TRUE)   
  13. public AwareBean createBeanFactoryAwareBean() {   
  14.   return new AwareBean();   
  15. }   
<script>render_code();</script>
除了destroyMethodName,@Bean标记也支持initMethodName。

2.3 @ExternalBean 
@ExternalBean是一个简单的markup标记,它用来注入在父application context中定义的"外部"beans,让我们看看例子: 

代码

  1. @Configuration  
  2. public abstract class ExternalBeanConfiguration {   
  3.   @Bean  
  4.   public TestBean james() {   
  5.     TestBean james = new TestBean();   
  6.     // inject dependency from ann()   
  7.     james.setSpouse(ann());   
  8.     return james;   
  9.   }   
  10.   
  11.   // Will be taken from the parent context   
  12.   @ExternalBean  
  13.   public abstract TestBean ann();   
  14. }   
<script>render_code();</script>
当JavaConfig遇到@ExternalBean时,它将覆盖该方法,这样任何时候该方法被调用时,将在父application context里查找该方法名的bean。 
这样,你的配置保持纯Java和重构友好性。 
注意@ExternalBean也在普通方法上工作;上面的例子使用抽象方法来避免写入无法执行的dummy code: 

代码

  1. @Configuration  
  2. public class ExternalBeanOnNormalMethod {   
  3.   @ExternalBean  
  4.   public TestBean ann() {   
  5.     System.out.println("this code will not execute as the method " +   
  6.       "will be overriden with a bean look up at runtime");   
  7.   }   
  8. }   
<script>render_code();</script>

2.4 @ScopedProxy 
Spring通过使用scoped proxies来提供方便的方式与scoped dependencies工作。 
当使用XML配置时创建这样的proxy最简单的方式为<aop:scoped-proxy/>元素。 
JavaConfig提供@ScopedProxy标记作为替换品,它提供相同的语义和配置选项。 
参考文档里XML scoped proxy的例子在JavaConfig里看起来像这样: 

代码

  1. // a HTTP Session-scoped bean exposed as a proxy   
  2. @Bean(scope = DefaultScopes.SESSION)   
  3. @ScopedProxy  
  4. public UserPreferences userPreferences() {   
  5.   return new UserPreferences();   
  6. }   
  7.   
  8. @Bean  
  9. public Service userService() {   
  10.   UserService service = new SimpleUserService();   
  11.   // a reference to the proxied 'userPreferences' bean   
  12.   service.setUserPreferences(userPreferences());   
  13.   return service;   
  14. }   
<script>render_code();</script>

第三章、Bean可见度 
JavaConfig的一个很好的特性是bean可见度。 
JavaConfig使用方法可见度修饰符来决定从该方法得到的bean是否可以通过owning application context/bean factory来访问。 
考虑如下配置: 

代码

  1. @Configuration  
  2. public abstract class VisibilityConfiguration {   
  3.   @Bean  
  4.   public Bean publicBean() {   
  5.     Bean bean = new Bean();   
  6.     bean.setDependency(hiddenBean());   
  7.     return bean;   
  8.   }   
  9.   
  10.   @Bean  
  11.   protected HiddenBean hiddenBean() {   
  12.     return new Bean("protected bean");   
  13.   }   
  14.   
  15.   @Bean  
  16.   private HiddenBean secretBean() {   
  17.     Bean bean = new Bean("private bean");   
  18.     // hidden beans can access beans defined in the 'owning' context   
  19.     bean.setDependency(outsideBean());   
  20.   }   
  21.   
  22.   @ExternalBean  
  23.   public abstract Bean outsideBean()   
  24. }   
<script>render_code();</script>
和如下XML配置一起使用: 

代码

  1. <beans>  
  2.   <!-- the configuration above -->  
  3.   <bean class="my.java.config.VisibilityConfiguration"/>  
  4.   
  5.   <!-- Java Configuration post processor -->  
  6.   <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>  
  7.   
  8.   <bean id="mainBean" class="my.company.Bean">  
  9.     <!-- this will work -->  
  10.     <property name="dependency" ref="publicBean"/>  
  11.     <!-- this will *not* work -->  
  12.     <property name="anotherDependency" ref="hiddenBean"/>  
  13.   </bean>  
  14. </beans>  
<script>render_code();</script>
JavaConfig遇到如上的配置时,它将创建3个beans: publicBean, hiddenBean和secretBean。 
它们是互相可见的,但是在'owning' application context(启动JavaConfig的application context)里创建的beans将只能看到publicBean。 
hiddenBean和secretBean只能被在VisibilityConfiguration里创建的beans访问。 
任何被@Bean标注的非public方法(protected, private和default)将创建一个'hidden' bean。 
在上面的例子里,mainBean使用publicBean和hiddenBean配置。 
但是,由于后者是hidden的,在运行时Spring将抛出异常: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hiddenBean' is defined 
... 
为了提供可见度功能,JavaConfig利用Spring提供的application context hierarchy,将所有的hidden beans放在一个子application context里一个 
特殊的配置类里。这样,hidden beans可以访问在父(或owning)context里定义的beans,但是相反不行。

四、装配依赖 
为了装配一个bean,通常需要简单的使用Java提供的constructs: 

代码

  1. @Bean(scope = DefaultScopes.SINGLETON)   
  2. public Person rod() {   
  3.   return new Person("Rod Johnson");   
  4. }   
  5.   
  6. @Bean(scope = DefaultScopes.PROTOTYPE)   
  7. public Book book() {   
  8.   Book book = new Book("Expert one-on-one J2EE Design and Development");   
  9.   book.setAuthor(rod()); // rod() method is actually a bean reference !   
  10.   return book;   
  11. }   
<script>render_code();</script>
上面的例子中,book的author使用rod方法的返回值。 
但是,由于book和rod方法都被@Bean标记,结果得到的Spring管理的beans将遵循容器语义: rod bean将是singleton而book bean将是prototype。 
当创建配置时,Spring知道annotation context并且将用名为"rod"的bean的引用来代替rod()方法。 
每次book bean被请求时容器将返回一个新的Book实例(prototype),但是对rod bean则将返回同一实例(singleton)。 
上面的代码等同于: 

代码

  1. <bean id="rod" class="Person" scope="singleton">  
  2.   <constructor-arg>Rod Johnson</constructor-arg>  
  3. </bean>  
  4.   
  5. <bean id="book" class="Book" scope="prototype">  
  6.   <constructor-arg>Expert One-on-On J2EE Design and Development</constructor-arg>  
  7.   <property name="author" ref="rod" />  
  8. </bean>  
<script>render_code();</script>
注意上面的例子使用两个常见的scopes类型,而任何类型的scoping都可以被指定: 

代码

  1. @Bean (scope = "customer")   
  2. public Bag shopingBag() {   
  3.   return new Basket();   
  4. }   
  5.   
  6. @Bean (scope = "shift")   
  7. public Manager shopManager() {   
  8.   ...   
  9. }   
<script>render_code();</script>

五、命名策略 
到目前为止,上面所有的例子里,bean的名字都来自于方法名: 

代码

  1. @Configuration  
  2. public class ColorsConfiguration {   
  3.   // create a bean with name 'blue'   
  4.   @Bean  
  5.   public Color blue() {   
  6.     ...   
  7.   }   
  8.   ...   
  9. }   
  10.   
  11. // dependency lookup for the blue color   
  12. applicationContext.getBean("blue");   
<script>render_code();</script>
在某些情况下,以方法名作为同样的bean名字并不合适,不同的类将覆盖定义。 
为了定制该行为,我们可以实现BeanNamingStrategy接口来提供自己的名字生成策略。 
但是,在写你自己的代码之前,看看默认实现MethodNameStrategy提供的选项: 

代码

  1. <!-- Java Configuration post processor -->  
  2. <bean class="org.springframework.config.java.process.ConfigurationPostProcessor">  
  3.   <property name="namingStrategy">  
  4.     <bean class="org.springframework.config.java.naming.MethodNameStrategy">  
  5.       <property name="prefix" value="CLASS"/>  
  6.     </bean>  
  7.   </property>  
  8. </bean>  
<script>render_code();</script>
这样配置后,bean的名字将为bean创建方法加上class名前缀: 

代码

  1. // dependency lookup for the blue color using the new naming scheme   
  2. applicationContext.getBean("ColorsConfiguration.blue");   
<script>render_code();</script>

六、混合XML和annotations 
Java和XML配置不是互斥的--它们可以同时在同一Spring程序里使用。为了从一个XML文件得到bean,我们需要使用Spring容器。 
前面提到,我们可以使用@ExternalBean标记(推荐方式)。 
当这种方式不适用时,可以访问@Configuration类使用的底层beanFactory。 
这可以通过继承ConfigurationSupport或实现BeanFactoryAware接口来实现。 
考虑下面的XML配置: 

代码

  1. <bean id="myBean" class="MyBean"/>   
<script>render_code();</script>
为了引用myBean这个bean,我们可以使用下面的代码片段: 

代码

  1. @Configuration  
  2. public class MyConfig extends ConfigurationSupport {   
  3.   @Bean  
  4.   public ExampleBean anotherBean() {   
  5.     ExampleBean bean = new ExampleBean("anotherBean");   
  6.     bean.setDep(getBean("myBean")); // use utility method to get a hold of 'myBean'   
  7.     return bean;   
  8.   }   
  9. }   
  10.   
  11. @Configuration  
  12. public class MyOtherConfig implements BeanFactoryAware {   
  13.   private BeanFactory beanFactory;   
  14.   
  15.   public void setBeanFactory(BeanFactory beanFactory) {   
  16.     // get access to the owning bean factory   
  17.     this.beanFactory = beanFactory;   
  18.   }   
  19.   
  20.   @Bean  
  21.   public ExampleBean yetAnotherBean() {   
  22.     ExampleBean bean = new ExampleBean("yetAnotherBean");   
  23.     bean.setDep(beanFactory.getBean("myBean")); // use dependency lookup   
  24.     return bean;   
  25.   }   
  26. }   
<script>render_code();</script>
在使用ConfigurationSupport或BeanFactoryAware之前请三思,因为@ExternalBean以重构更友好的方式提供同样的功能。 
JavaConfig发布时包含了一个Petclinic示例,它使用Java和Groovy来替换部分XML配置--请参考示例程序获得更多信息。

七、使用Java Configuration 
为了使用annotations来配置我们的程序,我们可以使用:

a, AnnotationApplicationContext 
它接收Ant风格模式的类名来搜索annotations: 

代码

  1. ApplicationContext config = new AnnotationApplicationContext(SimpleConfiguration.class.getName());   
  2. ApplicatonContext aBunchOfConfigs = new AnnotationApplicationContext("**/configuration/*Configuration.class");   
<script>render_code();</script>
这种特有的application context将自动读取classpath下匹配给定模式的类并添加进来作为beans,缺点是这种方式不允许配置实例带参数。

b, Configuration post processor 

代码

  1. <beans>  
  2.   <!-- Spring configuration -->  
  3.   <bean class="org.springframework.samples.petclinic.JdbcConfiguration"/>  
  4.   <!-- Java Configuration post processor -->  
  5.   <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>  
  6. </beans>  
<script>render_code();</script>
这种方式允许更多的配置选项,因为它不仅提供对configuration processing(通过ConfigurationPostProcessor)的控制,也提供对配置实例本身。 
通过定义configuration为一个bean,Spring容器可以用来配置configuration(设置properties或者使用某个构造方法): 

代码

  1. <beans>  
  2. <!-- a possible configurable configuration -->  
  3. <bean class="org.my.company.cofig.AppConfiguration">  
  4.   <property name="env" value="TESTING"/>  
  5.   <property name="monitoring" value="true"/>  
  6.   <property name="certificates" value="classpath:/META-INF/config/MyCompany.certs"/>  
  7. </bean>  
  8.   
  9. <!-- Java Configuration post processor -->  
  10. <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>  
  11. </beans>  
<script>render_code();</script>

第八章、Roadmap 
该project相对来说很年轻,可以认为是beta版(hence, the milestone release)。 
后继的开发将关注于自动配置发现和简化。 
反馈、八哥和建议在Srping forumSpring issue tracking都是受欢迎的。

译者说: 
Spring JavaConfigGoogle Guice的区别主要在于它们两者的IoC理念不同: 
JavaConfig说IoC配置是必须无侵入的,所以单独弄了个@Configuration 
Guice说IoC配置是应用程序模型的一部分,所以配置都扔在领域模型代码中

最后感谢老婆公司领导命令她今天加班,译者今天才有一天的时间来翻译此文档。

JavaConfig和sannotations都以plugins的方式使用,Spring现在还没有集成它们,而JavaConfig是Spring的子项目,Spring要把Annotation配置集成到核心代码的话肯定选择JavaConfig了

你要采用annotation方式配置,就表示接受使用@Configuration

JavaConfig也提供了AOP的注解配置方式,不过文档中还没怎么提,不知道以后会不会替代AspectJ的注解方

猜你喜欢

转载自blog.csdn.net/dongjunpublic/article/details/81392600