对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

通过以下方式实例化Spring容器AnnotationConfigApplicationContext

以下章节记录了斯普林的AnnotationConfigApplicationContext,春季引入 3.0。多功能应用上下文实施不仅能够接受@Configuration类作为输入,但也为纯@Component课程与课程 并附有JSR-330元数据注释。spring-doc.cadn.net.cn

什么时候@Configuration类作为输入提供,称为@Configuration类别本身 注册为Beans定义,并全部声明@Bean类内的方法 也被注册为Beans定义。spring-doc.cadn.net.cn

什么时候@Component以及JSR-330级,均注册为Beans 定义,并且假设DI元数据如下@Autowired@Inject是 在必要时用于这些职业。spring-doc.cadn.net.cn

简单构造

类似于 Spring XML 文件作为实例化ClassPathXmlApplicationContext,你可以使用@Configuration当 实例化一个AnnotationConfigApplicationContext.这使得 Spring容器的无XML使用情况,如下示例所示:spring-doc.cadn.net.cn

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注释型 作为构造子的输入,如下示例所示:spring-doc.cadn.net.cn

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(我的服务Impl),依赖1依赖2使用Spring 依赖注入注释,如@Autowired.spring-doc.cadn.net.cn

通过使用注册(班级<?>...)

你可以实例化一个AnnotationConfigApplicationContext通过使用无arg构造子 然后通过使用register()方法。这种方法尤其有用 当程序化构建AnnotationConfigApplicationContext.如下 示例展示了如何实现:spring-doc.cadn.net.cn

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()
}

启用组件扫描扫描(字符串......)

要启用组件扫描,你可以在@Configuration类别如下:spring-doc.cadn.net.cn

@Configuration
@ComponentScan(basePackages = "com.acme") (1)
public class AppConfig  {
	// ...
}
1 该注释支持组件扫描。
@Configuration
@ComponentScan(basePackages = ["com.acme"]) (1)
class AppConfig  {
	// ...
}
1 该注释支持组件扫描。

有经验的 Spring 用户可能熟悉 XML 声明的等价物,来自 斯普林斯上下文:命名空间,如下示例所示:spring-doc.cadn.net.cn

<beans>
	<context:component-scan base-package="com.acme"/>
</beans>

在前面的例子中,com.acme包裹会被扫描以寻找任何@Component- 注释类,这些类注册为春豆 容器内的定义。AnnotationConfigApplicationContext暴露了扫描(字符串......)该方法能够实现与 以下示例展示了:spring-doc.cadn.net.cn

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,因此它们是分量扫描的候选对象。在上述例子中, 假设AppConfigcom.acme包裹(或任何包裹) 在下面),在通话时会被接收到扫描().后refresh(),所有@Bean方法在容器内被处理并注册为豆定义。

支持Web应用AnnotationConfigWebApplicationContext

一个WebApplicationContext的变体AnnotationConfigApplicationContext可获得 跟AnnotationConfigWebApplicationContext.你可以在 配置春季ContextLoaderListenerservlet 听者,春季 MVC调度器服务,等等。如下web.xml摘要配置为典型 Spring MVC 网页应用(注意使用context类上下文参数和 init-param):spring-doc.cadn.net.cn

<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>
对于程序化用例,a通用WebApplicationContext可以用作 替代方案AnnotationConfigWebApplicationContext.参见通用WebApplicationContext详情请用Javadoc。