JavaConfigApplicationContext
provides direct access to the
beans defined by @Configuration
-annotated classes. For more information on the
ApplicationContext
API in general, please refer to the Core Spring documentation.
Instantiating the JavaConfigApplicationContext
can
be done by supplying @Configuration
class literals to the constructor,
and/or strings representing packages to scan for @Configuration
classes.
Each of the class literals supplied to the constructor will
be processed, and for each @Bean
method encountered, JavaConfig
will create a bean definition and ultimately instantiate and
initialize the bean.
JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class); Service service = context.getBean(Service.class);
Passing multiple @Configuration
classes:
JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class, DataConfig.class); Service service = context.getBean(Service.class);
Base packages will be scanned for the existence of any @Configuration
classes. Any candidate classes will then be processed much as if they had
been supplied directly as class literals to the constructor.
JavaConfigApplicationContext context = new JavaConfigApplicationContext("com.acme.app.configuration"); Service service = context.getBean(Service.class);
Passing multiple base packages:
JavaConfigApplicationContext context = new JavaConfigApplicationContext("com.acme.configuration", "com.acme.other"); Service service = context.getBean(Service.class);
Matching packages and classes by wildcard:
JavaConfigApplicationContext context = new JavaConfigApplicationContext("**/configuration/**/*.class", "**/other/*Config.class"); Service service = context.getBean(Service.class);
Note | |
---|---|
The wildcard syntax for matching packages and classes above is based on Ant Patterns |
When one or more classes/packages are supplied as constructor arguments,
a JavaConfigApplicationContext
instance cannot be further
configured. If post-construction configuration is preferred or required,
use either the no-arg constructor, configure by calling setters, then
manually refresh the context. After the call to refresh()
,
the context will be 'closed for configuration'.
JavaConfigApplicationContext context = new JavaConfigApplicationContext(); context.setParent(otherConfig); context.setConfigClasses(AppConfig.class, DataConfig.class); context.setBasePackages("com.acme.configuration"); context.refresh(); Service service = (Service) context.getBean("serviceA");
Note | |
---|---|
Whenever multiple packages and/or classes are used to
instantiate a |
JavaConfigApplicationContext
provides several
variants of the getBean()
method for accessing beans.
The preferred method for accessing beans is with the type-safe
getBean()
method.
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...); Service service = context.getBean(Service.class);
If more than one bean of type Service
had been
defined in the example above, the call to getBean()
would have thrown an exception indicating an ambiguity
that the container could not resolve. In these cases, the user has a
number of options for disambiguation:
Like Spring's XML configuration, JavaConfig allows for
specifying a given @Bean
as primary
:
@Configuration public class MyConfig { @Bean(primary=Primary.TRUE) public Service myService() { return new Service(); } @Bean public Service backupService() { return new Service(); } }
After this modification, all calls to
getBean(Service.class)
will return the primary
bean
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...); Service service = context.getBean(Service.class); // returns the myService() primary bean
JavaConfig provides a getBean()
variant
that accepts both a class and a bean name for cases just such as
these.
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...); Service service = context.getBean(Service.class, "myService");
Because bean ids must be unique, this call guarantees that the ambiguity cannot occur.
It is also reasonable to call the getBeansOfType()
method in order to return all beans that implement a
given interface:
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...); Map matchingBeans = context.getBeansOfType(Service.class);
Note that this latter approach is actually a feature of the Core
Spring Framework's AbstractApplicationContext
(which JavaConfigApplicationContext
extends) and is not type-safe, in that the returned
Map
is not parameterized.
Beans may be accessed via the traditional string-based getBean()
API as well. Of course this is not type-safe and requires casting,
but avoids any potential ambiguity entirely:
JavaConfigApplicationContext context = new JavaConfigApplicationContext(...); Service service = (Service) context.getBean("myService");