|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
通过使用 AnnotationConfigApplicationContext 实例化 Spring 容器
以下各节记录了Spring的AnnotationConfigApplicationContext,该功能在Spring 3.0中引入。这种灵活的ApplicationContext实现不仅可以接受@Configuration类作为输入,还可以接受普通的@Component类以及带有JSR-330元数据注解的类。
当提供@Configuration个类作为输入时,@Configuration类本身会被注册为一个Bean定义,该类中所有声明的@Bean方法也会被注册为Bean定义。
当提供@Component和JSR-330类时,它们会被注册为bean定义,并假设在这些类中必要时会使用DI元数据,如@Autowired或@Inject。
简单构造
在很大程度上,Spring XML文件在实例化一个
ClassPathXmlApplicationContext 时被用作输入,你也可以在实例化一个
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 {
// ...
}
| 1 | 这个注解启用了组件扫描。 |
@Configuration
@ComponentScan(basePackages = ["com.acme"]) (1)
class AppConfig {
// ...
}
| 1 | 这个注解启用了组件扫描。 |
|
经验丰富的Spring用户可能熟悉Spring的<code>0</code>命名空间中的XML声明,如下例所示:
|
在前面的示例中,扫描了 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 应用程序的支持
一个 WebApplicationContext 变体的 AnnotationConfigApplicationContext 已经可用
通过 AnnotationConfigWebApplicationContext。当
配置 Spring ContextLoaderListener servlet 监听器、Spring MVC
DispatcherServlet 等时,可以使用此实现。以下 web.xml 代码片段配置了一个典型的
Spring MVC Web 应用程序(注意使用了 contextClass 的 context-param 和
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 以获取详细信息。 |