Java-based Container Configuration
1.12. Java-based Container Configuration
This section covers how to use annotations in your Java code to configure the Spring container. It includes the following topics:
- Basic Concepts:
@Bean
and@Configuration
- Instantiating the Spring Container by Using
AnnotationConfigApplicationContext
- Using the
@Bean
Annotation - Using the
@Configuration
annotation - Composing Java-based Configurations
- Bean Definition Profiles
PropertySource
Abstraction- Using
@PropertySource
- Placeholder Resolution in Statements
1.12.1. Basic Concepts: `@Bean` and `@Configuration`
The central artifacts in Spring’s new Java-configuration support are @Configuration
-annotated classes and @Bean
-annotated methods.
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.
Annotating a class with @Configuration
indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration
classes let inter-bean dependencies be defined by calling other @Bean
methods in the same class. The simplest possible @Configuration
class reads as follows:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
The preceding AppConfig
class is equivalent to the following Spring <beans/>
XML:
<beans/>
XML:
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
Full @Configuration vs “lite” @Bean mode?
When
@Bean
methods are declared within classes that are not annotated with@Configuration
, they are referred to as being processed in a “lite” mode. Bean methods declared in a@Component
or even in a plain old class are considered to be “lite”, with a different primary purpose of the containing class and a@Bean
method being a sort of bonus there. For example, service components may expose management views to the container through an additional@Bean
method on each applicable component class. In such scenarios,@Bean
methods are a general-purpose factory method mechanism.Unlike full
@Configuration
, lite@Bean
methods cannot declare inter-bean dependencies. Instead, they operate on their containing component’s internal state and, optionally, on arguments that they may declare. Such a@Bean
method should therefore not invoke other@Bean
methods. Each such method is literally only a factory method for a particular bean reference, without any special runtime semantics. The positive side-effect here is that no CGLIB subclassing has to be applied at runtime, so there are no limitations in terms of class design (that is, the containing class may befinal
and so forth).In common scenarios,
@Bean
methods are to be declared within@Configuration
classes, ensuring that “full” mode is always used and that cross-method references therefore get redirected to the container’s lifecycle management. This prevents the same@Bean
method from accidentally being invoked through a regular Java call, which helps to reduce subtle bugs that can be hard to track down when operating in “lite” mode.
当@Bean方法在没有使用@Configuration注释的类中声明时,它们被称为以“精简”模式处理。在@Component或甚至在一个普通的旧类中声明的Bean方法被认为是“精简”的,包含类的主要目的不同,而@Bean方法是一种额外的好处。例如,服务组件可以通过每个适用组件类上的附加@Bean方法向容器公开管理视图。在这种场景中,@Bean方法是一种通用的工厂方法机制。
与完整的@Configuration不同,lite @Bean方法不能声明bean间的依赖。相反,它们对包含它们的组件的内部状态进行操作,并可选地对它们可能声明的参数进行操作。因此,这样的@Bean方法不应该调用其他的@Bean方法。每个这样的方法实际上只是特定bean引用的工厂方法,没有任何特殊的运行时语义。这里的积极的副作用是没有CGLIB子类必须在运行时应用,所以在类设计方面没有限制(也就是说,包含的类可能是final类等等)。
在常见的场景中,@Bean方法将在@Configuration类中声明,以确保始终使用“full”模式,并因此将跨方法引用重定向到容器的生命周期管理。这可以防止通过常规Java调用意外调用相同的@Bean方法,这有助于减少在“精简”模式下操作时难以跟踪的微妙bug。
The @Bean
and @Configuration
annotations are discussed in depth in the following sections. First, however, we cover the various ways of creating a spring container by using Java-based configuration.
1.12.2. Instantiating the Spring Container by Using `AnnotationConfigApplicationContext`
The following sections document Spring’s AnnotationConfigApplicationContext
, introduced 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.
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.
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.
Simple Construction
In much the same way that Spring XML files are used as input when instantiating a ClassPathXmlApplicationContext
, you can use @Configuration
classes as input when instantiating an AnnotationConfigApplicationContext
. This allows for completely XML-free usage of the Spring container, as the following example shows:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
As mentioned earlier, 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, as the following example shows:
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 preceding example assumes that MyServiceImpl
, Dependency1
, and Dependency2
use Spring dependency injection annotations such as @Autowired
.
Building the Container Programmatically by Using register(Class<?>…)
You can instantiate an AnnotationConfigApplicationContext
by using a no-arg constructor and then configure it by using the register()
method. This approach is particularly useful when programmatically building an AnnotationConfigApplicationContext
. The following example shows how to do so:
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…)
To enable component scanning, you can annotate your @Configuration
class as follows:
@Configuration
@ComponentScan(basePackages = "com.acme")
public class AppConfig {
// ...
}
- This annotation enables component scanning.
Experienced Spring users may be familiar with the XML declaration equivalent from Spring’s
context:
namespace, shown in the following example:<beans> <context:component-scan base-package="com.acme"/> </beans>
In the preceding example, the com.acme
package is scanned to look for any @Component
-annotated classes, and those classes are registered as Spring bean definitions within the container. AnnotationConfigApplicationContext
exposes the scan(String…)
method to allow for the same component-scanning functionality, as the following example shows:
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
}
Remember that
@Configuration
classes are meta-annotated with@Component
, so they are candidates for component-scanning. In the preceding example, assuming thatAppConfig
is declared within thecom.acme
package (or any package underneath), it is picked up during the call toscan()
. Uponrefresh()
, all its@Bean
methods are processed and registered as bean definitions within the container.
Support for Web Applications with AnnotationConfigWebApplicationContext
A WebApplicationContext
variant of AnnotationConfigApplicationContext
is available with AnnotationConfigWebApplicationContext
. You can use this implementation when configuring the Spring ContextLoaderListener
servlet listener, Spring MVC DispatcherServlet
, and so forth. The following web.xml
snippet 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>
For programmatic use cases, a
GenericWebApplicationContext
can be used as an alternative toAnnotationConfigWebApplicationContext
. See theGenericWebApplicationContext
javadoc for details.
1.12.3. Using the `@Bean` Annotation
@Bean
is a method-level annotation and a direct analog of the XML <bean/>
element. The annotation supports some of the attributes offered by <bean/>
, such as:
You can use the @Bean
annotation in a @Configuration
-annotated or in a @Component
-annotated class.
Declaring a Bean
To declare a bean, you can annotate a method with the @Bean
annotation. You use this method to register a bean definition within an ApplicationContext
of the type specified as the method’s return value. By default, the bean name is the same as the method name. The following example shows a @Bean
method declaration:
@Configuration
public class AppConfig {
@Bean
public TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}
The preceding configuration is exactly equivalent to the following Spring XML:
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
Both declarations make a bean named transferService
available in the ApplicationContext
, bound to an object instance of type TransferServiceImpl
, as the following text image shows:
transferService -> com.acme.TransferServiceImpl
You can also use default methods to define beans. This allows composition of bean configurations by implementing interfaces with bean definitions on default methods.
public interface BaseConfig {
@Bean
default TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}
@Configuration
public class AppConfig implements BaseConfig {
}
You can also declare your @Bean
method with an interface (or base class) return type, as the following example shows:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
However, this limits the visibility for advance type prediction to the specified interface type (TransferService
). Then, with the full type (TransferServiceImpl
) known to the container only once the affected singleton bean has been instantiated. Non-lazy singleton beans get instantiated according to their declaration order, so you may see different type matching results depending on when another component tries to match by a non-declared type (such as @Autowired TransferServiceImpl
, which resolves only once the transferService
bean has been instantiated).
If you consistently refer to your types by a declared service interface, your
@Bean
return types may safely join that design decision. However, for components that implement several interfaces or for components potentially referred to by their implementation type, it is safer to declare the most specific return type possible (at least as specific as required by the injection points that refer to your bean).
Bean Dependencies
A @Bean
-annotated method can have an arbitrary number of parameters that describe the dependencies required to build that bean. For instance, if our TransferService
requires an AccountRepository
, we can materialize that dependency with a method parameter, as the following example shows:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
The resolution mechanism is pretty much identical to constructor-based dependency injection. See the relevant section for more details.
Receiving Lifecycle Callbacks
Any classes defined with the @Bean
annotation support the regular lifecycle callbacks and can use the @PostConstruct
and @PreDestroy
annotations from JSR-250. See JSR-250 annotations for further details.
The regular Spring lifecycle callbacks are fully supported as well. If a bean implements InitializingBean
, DisposableBean
, or Lifecycle
, their respective methods are called by the container.
The standard set of *Aware
interfaces (such as BeanFactoryAware, BeanNameAware, MessageSourceAware, ApplicationContextAware, and so on) are also fully supported.
The @Bean
annotation supports specifying arbitrary initialization and destruction callback methods, much like Spring XML’s init-method
and destroy-method
attributes on the bean
element, as the following example shows:
public class BeanOne {
public void init() {
// initialization logic
}
}
public class BeanTwo {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public BeanOne beanOne() {
return new BeanOne();
}
@Bean(destroyMethod = "cleanup")
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
By default, beans defined with Java configuration that have a public
close
orshutdown
method are automatically enlisted with a destruction callback. If you have a publicclose
orshutdown
method and you do not wish for it to be called when the container shuts down, you can add@Bean(destroyMethod="")
to your bean definition to disable the default(inferred)
mode.You may want to do that by default for a resource that you acquire with JNDI, as its lifecycle is managed outside the application. In particular, make sure to always do it for a
DataSource
, as it is known to be problematic on Java EE application servers.The following example shows how to prevent an automatic destruction callback for a
DataSource
:@Bean(destroyMethod="") public DataSource dataSource() throws NamingException { return (DataSource) jndiTemplate.lookup("MyDS"); }
Also, with
@Bean
methods, you typically use programmatic JNDI lookups, either by using Spring’sJndiTemplate
orJndiLocatorDelegate
helpers or straight JNDIInitialContext
usage but not theJndiObjectFactoryBean
variant (which would force you to declare the return type as theFactoryBean
type instead of the actual target type, making it harder to use for cross-reference calls in other@Bean
methods that intend to refer to the provided resource here).
默认情况下,您可能希望对使用JNDI获取的资源这样做,因为它的生命周期是在应用程序之外管理的。特别是,要确保总是为DataSource这样做,因为众所周知,在Java EE应用程序服务器上这是有问题的。
下面的例子展示了如何防止DataSource的自动销毁回调:
@Bean(destroyMethod="")
public DataSource dataSource() throws NamingException {
return (DataSource) jndiTemplate.lookup("MyDS");
}
同样,对于@Bean方法,您通常使用编程的JNDI查找,要么使用Spring的JndiTemplate或JndiLocatorDelegate帮助程序,要么直接使用JNDI InitialContext,但不使用JndiObjectFactoryBean变量(这将迫使您将返回类型声明为FactoryBean类型,而不是实际的目标类型,使它在其他打算引用此处提供的资源的@Bean方法中难以使用交叉引用调用)。
In the case of BeanOne
from the example above the preceding note, it would be equally valid to call the init()
method directly during construction, as the following example shows:
@Configuration
public class AppConfig {
@Bean
public BeanOne beanOne() {
BeanOne beanOne = new BeanOne();
beanOne.init();
return beanOne;
}
// ...
}
When you work directly in Java, you can do anything you like with your objects and do not always need to rely on the container lifecycle.
Specifying Bean Scope
Spring includes the @Scope
annotation so that you can specify the scope of a bean.
Using the @Scope
Annotation
You can specify that your beans defined with the @Bean
annotation should have a specific scope. You can use any of the standard scopes specified in the Bean Scopes section.
The default scope is singleton
, but you can override this with the @Scope
annotation, as the following example shows:
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}
@Scope
and scoped-proxy
Spring offers a convenient way of working with scoped dependencies through scoped proxies. The easiest way to create such a proxy when using the XML configuration is the <aop:scoped-proxy/>
element. Configuring your beans in Java with a @Scope
annotation offers equivalent support with the proxyMode
attribute. The default is ScopedProxyMode.DEFAULT
, which typically indicates that no scoped proxy should be created unless a different default has been configured at the component-scan instruction level. You can specify ScopedProxyMode.TARGET_CLASS
, ScopedProxyMode.INTERFACES
or ScopedProxyMode.NO
.
<aop:scope-proxy/>
元素。在Java中使用@Scope注释配置bean提供了与proxyMode属性相同的支持。默认是ScopedProxyMode.DEFAULT,它通常表示不应创建作用域代理,除非在组件扫描指令级别配置了不同的默认值。您可以指定ScopedProxyMode.TARGET_CLASS,ScopedProxyMode.INTERFACES或ScopedProxyMode.NO。
If you port the scoped proxy example from the XML reference documentation (see scoped proxies) to our @Bean
using Java, it resembles the following:
// an HTTP Session-scoped bean exposed as a proxy
@Bean
@SessionScope
public UserPreferences userPreferences() {
return new UserPreferences();
}
@Bean
public Service userService() {
UserService service = new SimpleUserService();
// a reference to the proxied userPreferences bean
service.setUserPreferences(userPreferences());
return service;
}
Customizing Bean Naming
By default, configuration classes use a @Bean
method’s name as the name of the resulting bean. This functionality can be overridden, however, with the name
attribute, as the following example shows:
@Configuration
public class AppConfig {
@Bean("myThing")
public Thing thing() {
return new Thing();
}
}
Bean Aliasing
As discussed in Naming Beans, it is sometimes desirable to give a single bean multiple names, otherwise known as bean aliasing. The name
attribute of the @Bean
annotation accepts a String array for this purpose. The following example shows how to set a number of aliases for a bean:
@Configuration
public class AppConfig {
@Bean({"dataSource", "subsystemA-dataSource", "subsystemB-dataSource"})
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}
Bean Description
Sometimes, it is helpful to provide a more detailed textual description of a bean. This can be particularly useful when beans are exposed (perhaps through JMX) for monitoring purposes.
To add a description to a @Bean
, you can use the @Description
annotation, as the following example shows:
@Configuration
public class AppConfig {
@Bean
@Description("Provides a basic example of a bean")
public Thing thing() {
return new Thing();
}
}
1.12.4. Using the `@Configuration` annotation
@Configuration
is a class-level annotation indicating that an object is a source of bean definitions. @Configuration
classes declare beans through @Bean
-annotated methods. Calls to @Bean
methods on @Configuration
classes can also be used to define inter-bean dependencies. See Basic Concepts: @Bean
and @Configuration
for a general introduction.
Injecting Inter-bean Dependencies
When beans have dependencies on one another, expressing that dependency is as simple as having one bean method call another, as the following example shows:
@Configuration
public class AppConfig {
@Bean
public BeanOne beanOne() {
return new BeanOne(beanTwo());
}
@Bean
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
In the preceding example, beanOne
receives a reference to beanTwo
through constructor injection.
This method of declaring inter-bean dependencies works only when the
@Bean
method is declared within a@Configuration
class. You cannot declare inter-bean dependencies by using plain@Component
classes.
Lookup Method Injection
As noted earlier, lookup method injection is an advanced feature that you should use rarely. It is useful in cases where a singleton-scoped bean has a dependency on a prototype-scoped bean. Using Java for this type of configuration provides a natural means for implementing this pattern. The following example shows how to use lookup method injection:
public abstract class CommandManager {
public Object process(Object commandState) {
// grab a new instance of the appropriate Command interface
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
}
// okay... but where is the implementation of this method?
protected abstract Command createCommand();
}
By using Java configuration, you can create a subclass of CommandManager
where the abstract createCommand()
method is overridden in such a way that it looks up a new (prototype) command object. The following example shows how to do so:
@Bean
@Scope("prototype")
public AsyncCommand asyncCommand() {
AsyncCommand command = new AsyncCommand();
// inject dependencies here as required
return command;
}
@Bean
public CommandManager commandManager() {
// return new anonymous implementation of CommandManager with createCommand()
// overridden to return a new prototype Command object
return new CommandManager() {
protected Command createCommand() {
return asyncCommand();
}
}
}
Further Information About How Java-based Configuration Works Internally
Consider the following example, which shows a @Bean
annotated method being called twice:
@Configuration
public class AppConfig {
@Bean
public ClientService clientService1() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientService clientService2() {
ClientServiceImpl clientService = new ClientServiceImpl();
clientService.setClientDao(clientDao());
return clientService;
}
@Bean
public ClientDao clientDao() {
return new ClientDaoImpl();
}
}
clientDao()
has been called once in clientService1()
and once in clientService2()
. Since this method creates a new instance of ClientDaoImpl
and returns it, you would normally expect to have two instances (one for each service). That definitely would be problematic: In Spring, instantiated beans have a singleton
scope by default. This is where the magic comes in: All @Configuration
classes are subclassed at startup-time with CGLIB
. In the subclass, the child method checks the container first for any cached (scoped) beans before it calls the parent method and creates a new instance.
The behavior could be different according to the scope of your bean. We are talking about singletons here.
As of Spring 3.2, it is no longer necessary to add CGLIB to your classpath because CGLIB classes have been repackaged under
org.springframework.cglib
and included directly within the spring-core JAR.
There are a few restrictions due to the fact that CGLIB dynamically adds features at startup-time. In particular, configuration classes must not be final. However, as of 4.3, any constructors are allowed on configuration classes, including the use of
@Autowired
or a single non-default constructor declaration for default injection.If you prefer to avoid any CGLIB-imposed limitations, consider declaring your
@Bean
methods on non-@Configuration
classes (for example, on plain@Component
classes instead). Cross-method calls between@Bean
methods are not then intercepted, so you have to exclusively rely on dependency injection at the constructor or method level there.
如果您希望避免任何cglib强加的限制,可以考虑在non-@Configuration类上声明@Bean方法(例如,在普通的@Component类上声明)。@Bean方法之间的跨方法调用不会被拦截,因此您必须在构造函数或方法级别上专门依赖依赖注入。
1.12.5. Composing Java-based Configurations
Spring’s Java-based configuration feature lets you compose annotations, which can reduce the complexity of your configuration.
Using the @Import
Annotation
Much as the <import/>
element is used within Spring XML files to aid in modularizing configurations, the @Import
annotation allows for loading @Bean
definitions from another configuration class, as the following example shows:
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
Now, rather than needing to specify both ConfigA.class
and ConfigB.class
when instantiating the context, only ConfigB
needs to be supplied explicitly, as the following example shows:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}
This approach simplifies container instantiation, as only one class needs to be dealt with, rather than requiring you to remember a potentially large number of @Configuration
classes during construction.
As of Spring Framework 4.2,
@Import
also supports references to regular component classes, analogous to theAnnotationConfigApplicationContext.register
method. This is particularly useful if you want to avoid component scanning, by using a few configuration classes as entry points to explicitly define all your components.
Injecting Dependencies on Imported @Bean
Definitions
The preceding example works but is simplistic. In most practical scenarios, beans have dependencies on one another across configuration classes. When using XML, this is not an issue, because no compiler is involved, and you can declare ref="someBean"
and trust Spring to work it out during container initialization. When using @Configuration
classes, the Java compiler places constraints on the configuration model, in that references to other beans must be valid Java syntax.
Fortunately, solving this problem is simple. As we already discussed, a @Bean
method can have an arbitrary number of parameters that describe the bean dependencies. Consider the following more real-world scenario with several @Configuration
classes, each depending on beans declared in the others:
@Configuration
public class ServiceConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
@Configuration
public class RepositoryConfig {
@Bean
public AccountRepository accountRepository(DataSource dataSource) {
return new JdbcAccountRepository(dataSource);
}
}
@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return new DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// everything wires up across configuration classes...
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
There is another way to achieve the same result. Remember that @Configuration
classes are ultimately only another bean in the container: This means that they can take advantage of @Autowired
and @Value
injection and other features the same as any other bean.
Make sure that the dependencies you inject that way are of the simplest kind only.
@Configuration
classes are processed quite early during the initialization of the context, and forcing a dependency to be injected this way may lead to unexpected early initialization. Whenever possible, resort to parameter-based injection, as in the preceding example.Also, be particularly careful with
BeanPostProcessor
andBeanFactoryPostProcessor
definitions through@Bean
. Those should usually be declared asstatic @Bean
methods, not triggering the instantiation of their containing configuration class. Otherwise,@Autowired
and@Value
may not work on the configuration class itself, since it is possible to create it as a bean instance earlier thanAutowiredAnnotationBeanPostProcessor
.
另外,要特别注意通过@Bean定义BeanPostProcessor和BeanFactoryPostProcessor。这些方法通常应该声明为静态@Bean方法,而不是触发包含它们的配置类的实例化。否则,@Autowired和@Value可能无法在配置类本身上工作,因为可以在AutowiredAnnotationBeanPostProcessor之前将其创建为bean实例。
The following example shows how one bean can be autowired to another bean:
@Configuration
public class ServiceConfig {
@Autowired
private AccountRepository accountRepository;
@Bean
public TransferService transferService() {
return new TransferServiceImpl(accountRepository);
}
}
@Configuration
public class RepositoryConfig {
private final DataSource dataSource;
public RepositoryConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
}
@Configuration
@Import({ServiceConfig.class, RepositoryConfig.class})
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return new DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// everything wires up across configuration classes...
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
Constructor injection in
@Configuration
classes is only supported as of Spring Framework 4.3. Note also that there is no need to specify@Autowired
if the target bean defines only one constructor.
Fully-qualifying imported beans for ease of navigation
In the preceding scenario, using @Autowired
works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig
, how do you know exactly where the @Autowired AccountRepository
bean is declared? It is not explicit in the code, and this may be just fine. Remember that the Spring Tools for Eclipse provides tooling that can render graphs showing how everything is wired, which may be all you need. Also, your Java IDE can easily find all declarations and uses of the AccountRepository
type and quickly show you the location of @Bean
methods that return that type.
In cases where this ambiguity is not acceptable and you wish to have direct navigation from within your IDE from one @Configuration
class to another, consider autowiring the configuration classes themselves. The following example shows how to do so:
@Configuration
public class ServiceConfig {
@Autowired
private RepositoryConfig repositoryConfig;
@Bean
public TransferService transferService() {
// navigate 'through' the config class to the @Bean method!
return new TransferServiceImpl(repositoryConfig.accountRepository());
}
}
In the preceding situation, where AccountRepository
is defined is completely explicit. However, ServiceConfig
is now tightly coupled to RepositoryConfig
. That is the tradeoff. This tight coupling can be somewhat mitigated by using interface-based or abstract class-based @Configuration
classes. Consider the following example:
@Configuration
public class ServiceConfig {
@Autowired
private RepositoryConfig repositoryConfig;
@Bean
public TransferService transferService() {
return new TransferServiceImpl(repositoryConfig.accountRepository());
}
}
@Configuration
public interface RepositoryConfig {
@Bean
AccountRepository accountRepository();
}
@Configuration
public class DefaultRepositoryConfig implements RepositoryConfig {
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(...);
}
}
@Configuration
@Import({ServiceConfig.class, DefaultRepositoryConfig.class}) // import the concrete config!
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
Now ServiceConfig
is loosely coupled with respect to the concrete DefaultRepositoryConfig
, and built-in IDE tooling is still useful: You can easily get a type hierarchy of RepositoryConfig
implementations. In this way, navigating @Configuration
classes and their dependencies becomes no different than the usual process of navigating interface-based code.
If you want to influence the startup creation order of certain beans, consider declaring some of them as
@Lazy
(for creation on first access instead of on startup) or as@DependsOn
certain other beans (making sure that specific other beans are created before the current bean, beyond what the latter’s direct dependencies imply).
Conditionally Include @Configuration
Classes or @Bean
Methods
It is often useful to conditionally enable or disable a complete @Configuration
class or even individual @Bean
methods, based on some arbitrary system state. One common example of this is to use the @Profile
annotation to activate beans only when a specific profile has been enabled in the Spring Environment
(see Bean Definition Profiles for details).
The @Profile
annotation is actually implemented by using a much more flexible annotation called @Conditional
. The @Conditional
annotation indicates specific org.springframework.context.annotation.Condition
implementations that should be consulted before a @Bean
is registered.
Implementations of the Condition
interface provide a matches(…)
method that returns true
or false
. For example, the following listing shows the actual Condition
implementation used for @Profile
:
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// Read the @Profile annotation attributes
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
return true;
}
See the @Conditional
javadoc for more detail.
Combining Java and XML Configuration
Spring’s @Configuration
class support does not aim to be a 100% complete replacement for Spring XML. Some facilities, such as Spring XML namespaces, remain an ideal way to configure the container. In cases where XML is convenient or necessary, you have a choice: either instantiate the container in an “XML-centric” way by using, for example, ClassPathXmlApplicationContext
, or instantiate it in a “Java-centric” way by using AnnotationConfigApplicationContext
and the @ImportResource
annotation to import XML as needed.
XML-centric Use of @Configuration
Classes
It may be preferable to bootstrap the Spring container from XML and include @Configuration
classes in an ad-hoc fashion. For example, in a large existing codebase that uses Spring XML, it is easier to create @Configuration
classes on an as-needed basis and include them from the existing XML files. Later in this section, we cover the options for using @Configuration
classes in this kind of “XML-centric” situation.
Declaring @Configuration
classes as plain Spring <bean/>
elements
<bean/>
元素
Remember that @Configuration
classes are ultimately bean definitions in the container. In this series examples, we create a @Configuration
class named AppConfig
and include it within system-test-config.xml
as a <bean/>
definition. Because <context:annotation-config/>
is switched on, the container recognizes the @Configuration
annotation and processes the @Bean
methods declared in AppConfig
properly.
<bean/>
定义包含在system-test-config.xml中。因为<context:annotation-config/>
被打开了,容器识别@Configuration注释并正确地处理AppConfig中声明的@Bean方法。
The following example shows an ordinary configuration class in Java:
@Configuration
public class AppConfig {
@Autowired
private DataSource dataSource;
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
@Bean
public TransferService transferService() {
return new TransferService(accountRepository());
}
}
The following example shows part of a sample system-test-config.xml
file:
<beans>
<!-- enable processing of annotations such as @Autowired and @Configuration -->
<context:annotation-config/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="com.acme.AppConfig"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
The following example shows a possible jdbc.properties
file:
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa
jdbc.password=
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/acme/system-test-config.xml");
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}
In
system-test-config.xml
file, theAppConfig
<bean/>
does not declare anid
element. While it would be acceptable to do so, it is unnecessary, given that no other bean ever refers to it, and it is unlikely to be explicitly fetched from the container by name. Similarly, theDataSource
bean is only ever autowired by type, so an explicit beanid
is not strictly required.
<bean/>
没有声明id元素。虽然这样做是可以接受的,但这是不必要的,因为没有其他bean引用它,而且不太可能根据名称显式地从容器中获取它。类似地,DataSource bean只按类型自动连接,因此并不严格要求显式的bean id。
Using context:component-scan/ to pick up @Configuration
classes
<context:annotation-config/>
拾取@Configuration类
Because @Configuration
is meta-annotated with @Component
, @Configuration
-annotated classes are automatically candidates for component scanning. Using the same scenario as describe in the previous example, we can redefine system-test-config.xml
to take advantage of component-scanning. Note that, in this case, we need not explicitly declare <context:annotation-config/>
, because <context:component-scan/>
enables the same functionality.
<context:annotation-config/>
,因为<context:annotation-scan/>
启用了相同的功能。
The following example shows the modified system-test-config.xml
file:
<beans>
<!-- picks up and registers AppConfig as a bean definition -->
<context:component-scan base-package="com.acme"/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
@Configuration
Class-centric Use of XML with @ImportResource
In applications where @Configuration
classes are the primary mechanism for configuring the container, it is still likely necessary to use at least some XML. In these scenarios, you can use @ImportResource
and define only as much XML as you need. Doing so achieves a “Java-centric” approach to configuring the container and keeps XML to a bare minimum. The following example (which includes a configuration class, an XML file that defines a bean, a properties file, and the main
class) shows how to use the @ImportResource
annotation to achieve “Java-centric” configuration that uses XML as needed:
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}
properties-config.xml
<beans>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
</beans>
jdbc.properties
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa
jdbc.password=
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}