基于Java的容器配置
- 基本概念:@Bean 和@Configuration
@Bean注解标注在方法上
@Configuration注解标注在类上
@Bean用于指示一个方法实例化,配置和初始化一个对象为Spring IOC容器管理的新对象。对于那些熟悉XML <beans/>配置的人来说, @Bean注解与<bean/>扮演这相同的角色。@Bean注解可以用在Spring中任何有@Component注解的地方,然而,他们总是被用在标注有@Configuration注解的beans中。
@Configuration 注解表明它的主要目的就是作为bean defintions文件(bean 定义文件)。此外,@Configuration允许在同一个类中通过简单地调用@Bean方法来定义bean之间的依赖关系。最简单的@Configuration类如下所示:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
在xml中
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
- 一般在@Configuration中用@Bean定义Bean以及Bean的依赖关系
Full @Configuration vs 'lite' @Beans mode?
When @Bean methods are declared within classes that are not annotated with @Configuration they are referred to as being processed in a 'lite' mode. For example, bean methods declared in a @Component or even in a plain old class will be considered 'lite'.
Unlike full @Configuration, lite @Bean methods cannot easily declare inter-bean dependencies. Usually one @Bean method should not invoke another @Bean method when operating in 'lite' mode.
Only using @Bean methods within @Configuration classes is a recommended approach of ensuring that 'full' mode is always used. This will prevent the same @Bean method from accidentally being invoked multiple times and helps to reduce subtle bugs that can be hard to track down when operating in 'lite' mode.
使用AnnotationConfigApplicationContext实例化Spring容器
这种多功能ApplicationContext实现不仅可以接受@Configuration类作为输入,还可以接受 @Component注解标注的类和JSR-330元数据标注的类。
当@Configuration类作为输入时,@Configuration注解所标注的类本省也会被注册为一个bean definition,以及在该类中所有标注有@Bean的方法也被注册为bean definitions。
当提供@Component 和 JSR-330类的适合,它们也被注册为bean definitions.(假设依赖注入(DI)元数据比如@Autowired或@Inject已经标注在了这些类所需要标注的地方)。
- 简单构造
和用Spring XML初始化容器用ClassPathXmlApplicationContext相似,@Configuration类可以通过AnnotationConfigApplicationContext初始化容器。
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
无参构造器初始化,然后在用register()设置配置
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
```
## 支持web应用的 AnnotationConfigWebApplicationContext
一个AnnotationConfigApplicationContext的WebApplicationContext变体
AnnotationConfigWebApplicationContext。这个实现使得我们可以配置Spring ContextLoaderListener servlet listener, Spring MVC DispatcherServlet,等等。下面的web.xml的一个片段是用来配置典型的Spring MVC web应用的,注意contextClass context-param 和 init-param的用法:
````xml
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
声明一个bean
简单的通过@Bean注解标注在一个方法上,就能声明一个bean。通过这个方法的返回值类型在ApplicationContext中注册一个bean definition。默认情况下,bean名称将与方法名称相同。以下是@Bean方法声明的简单示例:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
XML配置实现
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
两种方式都在ApplicationContext中声明了一个名为transferService的可用的bean,绑定类型为对象实例TransferServiceImpl:
transferService -> com.acme.TransferServiceImpl
接受生命周期回调
该@Bean注释支持指定任意的初始化和销毁回调方法,就像Spring XML init-method和元素destroy-method上的属性一样bean:
public class Foo {
public void init() {
// initialization logic
}
}
public class Bar {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}
指定Bean的范围
使用@Scope注解
@Configuration
public class MyConfiguration {
@Bean
@Scope(“prototype”)
public Encryptor encryptor(){
// ...
}
}
自定义Bean命名
默认情况下,配置类使用@Bean方法的名称作为生成bean的名称。但是,name属性可以覆盖此功能
@Configuration
public class AppConfig {
@Bean(name =“myFoo”)
public Foo foo(){
return new Foo();
}
}
有时候需要给出一个bean命名的多个名称(为了这个目的name属性可以接受一个String数组)
@Configuration
public class AppConfig {
@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}