多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[toc] 下面的章节介绍Spring的`AnnotationConfigApplicationContext`,这是Spring 3.0中的新功能。 这种多功能的`ApplicationContext`实现不仅可以接受`@Configuration`类作为输入,还可以接受用`JSR-330`元数据注释的普通`@Component`类。 `@Configuration`作为输入,这个类注册为bean定义,包含的所有`@Bean`也作为bean的定义. `@Component`作为输入,这个类注册为bean定义,包含的`@Autowired`作为依赖注入. ## 简单的构造 与实例化`ClassPathXmlApplicationContext`时使用Spring XML文件作为输入的方式大致相同,在实例化`AnnotationConfigApplicationContext`时,`@Configuration`类可用作输入。 这允许完全无XML地使用Spring容器: ~~~ java public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } ~~~ 上面提到过,不仅可以使用`@Configuration`作为输入,也可以使用`@Component`作为输入. ~~~java public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); } ~~~ 上面假定`MyServiceImpl`,使用`@Autowired`注入`Dependency1 和 Dependency2`. ## Building the container programmatically using register(Class<?>…) `AnnotationConfigApplicationContext`可以使用无参数构造函数实例化,然后使用`register()`方法进行配置。 以编程方式构建`AnnotationConfigApplicationContext`时,此方法特别有用。 ~~~java 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(); } ~~~ ## Enabling component scanning with scan(String..) 为了启用组件扫描的功能, 只需如下注解`@Configuration`类 ~~~ @Configuration @ComponentScan(basePackages = "com.acme") public class AppConfig { ... } ~~~ 有经验的spring开发人员,会想到xml的配置 ~~~xml <beans> <context:component-scan base-package="com.acme"/> </beans> ~~~ `AnnotationConfigApplicationContext `支持扫描 ~~~ public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.acme"); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); } ~~~ ## Support for web applications with AnnotationConfigWebApplicationContext `AnnotationConfigApplicationContext`的`WebApplicationContext`变体可与`AnnotationConfigWebApplicationContext`一起使用。 在配置Spring `ContextLoaderListener` servlet侦听器,Spring MVC `DispatcherServlet`等时,可以使用此实现。接下来是配置典型Spring MVC Web应用程序的`web.xml`片段。 请注意`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> ~~~