The central artifacts in Spring’s new Java-configuration support are @Configuration
-annotated classes and @Bean
-annotated methods.web
The @Bean
annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container. For those familiar with Spring's <beans/>
XML configuration the @Bean
annotation plays the same role as the <bean/>
element. You can use @Bean
annotated methods with any Spring @Component
, however, they are most often used with @Configuration
beans.spring
Annotating a class with @Configuration
indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration
classes allow inter-bean dependencies to be defined by simply calling other @Bean
methods in the same class. The simplest possible @Configuration
class would read as follows:app
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
The AppConfig
class above would be equivalent to the following Spring <beans/>
XML:ide
<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
The @Bean
and @Configuration
annotations will be discussed in depth in the sections below. First, however, we'll cover the various ways of creating a spring container using Java-based configuration.學習
The sections below document Spring’s AnnotationConfigApplicationContext
, new in Spring 3.0. This versatile ApplicationContext
implementation is capable of accepting not only @Configuration
classes as input, but also plain @Component
classes and classes annotated with JSR-330 metadata.ui
When @Configuration
classes are provided as input, the @Configuration
class itself is registered as a bean definition, and all declared @Bean
methods within the class are also registered as bean definitions.url
When @Component
and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as @Autowired
or @Inject
are used within those classes where necessary.spa
Simple construction.net
In much the same way that Spring XML files are used as input when instantiating a ClassPathXmlApplicationContext
, @Configuration
classes may be used as input when instantiating an AnnotationConfigApplicationContext
. This allows for completely XML-free usage of the Spring container:code
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);//如上所定義的AppConfig class 文件
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
As mentioned above, AnnotationConfigApplicationContext
is not limited to working only with @Configuration
classes. Any @Component
or JSR-330 annotated class may be supplied as input to the constructor. For example:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
The above assumes that MyServiceImpl
, Dependency1
and Dependency2
use Spring dependency injection annotations such as @Autowired
.
Building the container programmatically using register(Class<?>…)
An AnnotationConfigApplicationContext
may be instantiated using a no-arg constructor and then configured using the register()
method. This approach is particularly useful when programmatically building an AnnotationConfigApplicationContext
.
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(); }
Support for web applications with AnnotationConfigWebApplicationContext
A WebApplicationContext
variant of AnnotationConfigApplicationContext
is available with AnnotationConfigWebApplicationContext
. This implementation may be used when configuring the Spring ContextLoaderListener
servlet listener, Spring MVC DispatcherServlet
, etc. What follows is a web.xml
snippet that configures a typical Spring MVC web application. Note the use of the contextClass
context-param and 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>