对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
使用 实例化 Spring 容器AnnotationConfigApplicationContext
以下部分记录了 Spring 的AnnotationConfigApplicationContext
,在 Spring 中引入
3.0. 这个多才多艺的ApplicationContext
implementation 不仅能够接受@Configuration
类作为输入,但也作为普通类@Component
类和类
使用 JSR-330 元数据进行注释。
什么时候@Configuration
类作为输入提供,@Configuration
类本身
注册为 Bean 定义,并且所有声明@Bean
类中 methods
也注册为 Bean 定义。
什么时候@Component
和 JSR-330 类,它们被注册为 bean
定义,并假设 DI 元数据(如@Autowired
或@Inject
是
在必要时使用这些类。
结构简单
与在实例化ClassPathXmlApplicationContext
,您可以使用@Configuration
类作为输入,当
实例化AnnotationConfigApplicationContext
.这允许完全
Spring 容器的无 XML 用法,如下例所示:
-
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 注释的类
作为构造函数的 input,如下例所示:
-
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
通过使用 no-arg 构造函数
,然后使用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()
}
启用 Component Scanningscan(String…)
要启用组件扫描,您可以对@Configuration
类,如下所示:
-
Java
-
Kotlin
@Configuration
@ComponentScan(basePackages = "com.acme") (1)
public class AppConfig {
// ...
}
1 | 此注释启用组件扫描。 |
@Configuration
@ComponentScan(basePackages = ["com.acme"]) (1)
class AppConfig {
// ...
}
1 | 此注释启用组件扫描。 |
有经验的 Spring 用户可能熟悉 XML 声明等价物
Spring的
|
在前面的示例中,com.acme
扫描包以查找任何@Component
-annotated 类,并且这些类被注册为 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 package (或任何 package
在下面),它会在调用scan() .后refresh() ,则其所有@Bean 方法在容器中被处理并注册为 Bean 定义。 |
支持 Web 应用程序AnnotationConfigWebApplicationContext
一个WebApplicationContext
的变体AnnotationConfigApplicationContext
可用
跟AnnotationConfigWebApplicationContext
.在以下情况下,可以使用此实现
配置 SpringContextLoaderListener
servlet 侦听器, Spring MVCDispatcherServlet
等。以下内容web.xml
snippet 配置一个典型的
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 了解详细信息。 |