使用AnnotationConfigApplicationContext实例化Spring容器
AnnotationConfigApplicationContext
以下部分将介绍Spring的AnnotationConfigApplicationContext,该组件从Spring 3.0开始引入。这种多功能的ApplicationContext实现不仅能够接受@Configuration类作为输入,还能接受普通的@Component类以及带有JSR-330元数据的类。
当输入为@Configuration类时,@Configuration类本身会被注册为一个bean定义,该类中声明的所有@Bean方法也会被注册为bean定义。
当提供了@Component和JSR-330类时,它们会被注册为bean定义,并且假定在这些类中会根据需要在适当的位置使用诸如@Autowired或@Inject这样的DI元数据。
简单构造
就像在实例化ClassPathXmlApplicationContext时使用Spring XML文件作为输入一样,当实例化AnnotationConfigApplicationContext时,也可以使用@Configuration类作为输入。如下例所示,这样可以完全不使用XML来使用Spring容器:
- Java
- Kotlin
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext(AppConfig::class.java)
val myService = ctx.getBean<MyService>()
myService.doStuff()
}
如前所述,AnnotationConfigApplicationContext并不局限于仅与@Configuration类一起使用。任何带有@Component注解或JSR-330注解的类都可以作为构造函数的输入提供,如下例所示:
- Java
- Kotlin
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext(MyServiceImpl::class.java, Dependency1::class.java, Dependency2::class.java)
val myService = ctx.getBean<MyService>()
myService.doStuff()
}
前面的例子假设MyServiceImpl、Dependency1和Dependency2使用了Spring的依赖注入注解,如@Autowired。
通过使用register(Class<?>…)编程方式构建容器
你可以通过使用无参数构造函数来实例化一个AnnotationConfigApplicationContext,然后使用register()方法对其进行配置。当需要以编程方式构建AnnotationConfigApplicationContext时,这种方法特别有用。以下示例展示了如何操作:
- Java
- Kotlin
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();
}
import org.springframework.beans.factory.getBean
fun main() {
val ctx = AnnotationConfigApplicationContext()
ctx.register(AppConfig::class.java, OtherConfig::class.java)
ctx.register(AdditionalConfig::class.java)
ctx.refresh()
val myService = ctx.getBean<MyService>()
myService.doStuff()
}
使用scan(String…)启用组件扫描
要启用组件扫描,你可以按照以下方式对你的@Configuration类进行注释:
- Java
- Kotlin
@Configuration
@ComponentScan(basePackages = "com.acme") 1
public class AppConfig {
// ...
}
此注释启用组件扫描。
@Configuration
@ComponentScan(basePackages = ["com.acme"]) 1
class AppConfig {
// ...
}
此注释启用组件扫描。
有经验的Spring用户可能熟悉Spring的context:命名空间中的等效XML声明,如下例所示:
<beans>
<context:component-scan base-package="com.acme"/>
</beans>
在前面的例子中,com.acme 包被扫描以查找任何带有 @Component 注解的类,这些类会被注册为 Spring 容器中的 Bean 定义。AnnotationConfigApplicationContext 提供了 scan(String…) 方法,以实现相同的组件扫描功能,如下例所示:
- Java
- Kotlin
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
}
fun main() {
val ctx = AnnotationConfigApplicationContext()
ctx.scan("com.acme")
ctx.refresh()
val myService = ctx.getBean<MyService>()
}
请记住,@Configuration 类被 元注释 为 @Component,因此它们是组件扫描的候选对象。在前面的例子中,假设 AppConfig 被声明在 com.acme 包(或其下的任何包)中,在调用 scan() 时它会被发现。当执行 refresh() 时,它的所有 @Bean 方法都会被处理,并作为 Bean 定义注册到容器中。
使用AnnotationConfigWebApplicationContext支持Web应用程序
AnnotationConfigApplicationContext 的一个 WebApplicationContext 变体是 AnnotationConfigWebApplicationContext。在配置 Spring 的 ContextLoaderListener 服务器端监听器、Spring MVC 的 DispatcherServlet 等时,可以使用这种实现。以下 web.xml 段落配置了一个典型的 Spring MVC Web 应用程序(注意 contextClass 和 init-param 的使用):
<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>
对于编程使用场景,GenericWebApplicationContext可以作为AnnotationConfigWebApplicationContext的替代选择。详情请参阅GenericWebApplicationContext的Javadoc。