Spring JavaConfig参考文档
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成为一个很好的配置选项。@Configurationpublic class WebConfiguration { // bean definitions follow}
@Configuration是一个class级别的annotation,它指示了配置里定义的bean的一些默认值。
@Configuration(defaultAutowire = Autowire.BY_TYPE, defaultLazy = Lazy.FALSE)public classDataSourceConfiguration extends ConfigurationSupport {}
@Bean (scope = DefaultScopes.SESSION)public ExampleBean exampleBean() { return new ExampleBean();}
@Beanpublic ExampleBean exampleBean() { return ExampleFactory.createBean();}
@Bean(aliases = {"anniversaries"})public List<Date> birthdays() { List<Date> dates = new ArrayList<Date>(); Calendar calendar = Calendar.getInstance(); calendar.set(1977, 05, 28); dates.add(calendar.getTime()); dates.add(computeMotherInLawBirthday()); return dates;}
public class AwareBean implements BeanFactoryAware { private BeanFactory factory; // BeanFactoryAware setter public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.factory - beanFactory; } public void close() { // do clean-up }}@Bean(destroyMethodName = "close", lazy = Lazy.TRUE)public AwareBean createBeanFactoryAwareBean() { return new AwareBean();}
@Configurationpublic abstract class ExternalBeanConfiguration { @Bean public TestBean james() { TestBean james = new TestBean(); // inject dependency from ann() james.setSpouse(ann()); return james; } // Will be taken from the parent context @ExternalBean public abstract TestBean ann();}
@Configurationpublic class ExternalBeanOnNormalMethod { @ExternalBean public TestBean ann() { System.out.println("this code will not execute as the method " + "will be overriden with a bean look up at runtime"); }}
// a HTTP Session-scoped bean exposed as a proxy@Bean(scope = DefaultScopes.SESSION)@ScopedProxypublic UserPreferences userPreferences() { return new UserPreferences();}@Beanpublic Service userService() { UserService service = new SimpleUserService(); // a reference to the proxied 'userPreferences' bean service.setUserPreferences(userPreferences()); return service;}@Configurationpublic abstract class VisibilityConfiguration { @Bean public Bean publicBean() { Bean bean = new Bean(); bean.setDependency(hiddenBean()); return bean; } @Bean protected HiddenBean hiddenBean() { return new Bean("protected bean"); } @Bean private HiddenBean secretBean() { Bean bean = new Bean("private bean"); // hidden beans can access beans defined in the 'owning' context bean.setDependency(outsideBean()); } @ExternalBean public abstract Bean outsideBean()}
<beans> <!-- the configuration above --> <bean ref="publicBean"/> <!-- this will *not* work --> <property name="anotherDependency" ref="hiddenBean"/> </bean></beans>
<bean id="rod" scope="singleton"> <constructor-arg>Rod Johnson</constructor-arg></bean><bean id="book" scope="prototype"> <constructor-arg>Expert One-on-On J2EE Design and Development</constructor-arg> <property name="author" ref="rod" /></bean>
@Bean (scope = "customer")public Bag shopingBag() { return new Basket();}@Bean (scope = "shift")public Manager shopManager() { ...}@Configurationpublic class ColorsConfiguration { // create a bean with name 'blue' @Bean public Color blue() { ... } ...}// dependency lookup for the blue colorapplicationContext.getBean("blue");
<!-- Java Configuration post processor --><bean value="CLASS"/> </bean> </property></bean>
// dependency lookup for the blue color using the new naming schemeapplicationContext.getBean("ColorsConfiguration.blue");<bean id="myBean" name="code">@Configurationpublic class MyConfig extends ConfigurationSupport { @Bean public ExampleBean anotherBean() { ExampleBean bean = new ExampleBean("anotherBean"); bean.setDep(getBean("myBean")); // use utility method to get a hold of 'myBean' return bean; }}@Configurationpublic class MyOtherConfig implements BeanFactoryAware { private BeanFactory beanFactory; public void setBeanFactory(BeanFactory beanFactory) { // get access to the owning bean factory this.beanFactory = beanFactory; } @Bean public ExampleBean yetAnotherBean() { ExampleBean bean = new ExampleBean("yetAnotherBean"); bean.setDep(beanFactory.getBean("myBean")); // use dependency lookup return bean; }}
<beans> <!-- Spring configuration --> <bean name="code"><beans><!-- a possible configurable configuration --><bean value="TESTING"/> <property name="monitoring" value="true"/> <property name="certificates" value="classpath:/META-INF/config/MyCompany.certs"/></bean><!-- Java Configuration post processor --><bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/></beans>7 楼 hideto 2007-07-14 第八章、Roadmap