|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.springframework.core.io.DefaultResourceLoader
org.springframework.context.support.AbstractApplicationContext
org.springframework.context.support.AbstractRefreshableApplicationContext
org.springframework.config.java.context.JavaConfigApplicationContext
public class JavaConfigApplicationContext
Application context that looks for classes annotated with the
Configuration annotation
and registers the Beans
they define; is the primary programmatic resource for using Spring
JavaConfig. Note that it is not strictly required that a configuration class
be annotated with @Configuration, but rather that it exposes at least one
non-private method annotated with @Bean.
JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class, DataConfig.class);
AccountService accountService = (AccountService) context.getBean("accountService");
Where AppConfig and DataConfig are defined as
follows:
@Configuration
public abstract class AppConfig {
@Bean
public AccountService accountService() {
return new AccountService(dataSource());
}
@ExternalBean
public abstract DataSource dataSource();
}
@Configuration
public abstract class DataConfig {
@Bean
public DataSource dataSource() {
return new DataSource(...);
}
}
JavaConfigApplicationContext are divided into two categories:
refresh
refresh
JavaConfigApplicationContext(Class...)JavaConfigApplicationContext(String...)JavaConfigApplicationContext(Class[], String[])JavaConfigApplicationContext(ApplicationContext, Class...)JavaConfigApplicationContext(ApplicationContext, String...)JavaConfigApplicationContext(ApplicationContext, Class[], String[])
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext();
ctx.setConfigClasses(Config1.class, Config2.class);
ctx.setBaseClasses("com.foo.myapp.config.*");
ctx.setParent(anotherContext);
ctx.refresh();
Service myService = (Service) ctx.getBean("service");
Note that the caller must manually invoke refresh to
advise the context that configuration is complete. In most cases, users will
not want to be burdened with having to remember to do this, so the latter six
constructors are provided as conveniences. They each internally call
refresh, which means that any subsequent calls to
setConfigClasses,
setBasePackages or
setParent are invalid and will result
in an exception. Simply said, after instantiation with one of the convenience
constructors, the context is 'closed for configuration':
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(Config1.class, Config2.class);
Service myService = (Service) ctx.getBean("service");
JavaConfigApplicationContext's type-safe getBean
methods:
The examples above become more elegant using these methods:
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(Config1.class, Config2.class); Service myService = ctx.getBean(Service.class); // no casting required!Of course, if multiple beans of type
Service exist in the
context, the call above becomes ambiguous. Disambiguation can happen in one
of two ways:
Primary
@Configuration
public class AppConfig {
@Bean(primary=Primary.TRUE)
public Service service() {
return new Service(...);
}
@Bean
public Service testService() {
return new Service(...);
}
}
getBean(Class, String) variant
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(Config1.class, Config2.class); Service testService = ctx.getBean(Service.class, "testService");
Configuration,
Bean| Field Summary |
|---|
| Fields inherited from class org.springframework.context.support.AbstractApplicationContext |
|---|
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME |
| Fields inherited from interface org.springframework.beans.factory.BeanFactory |
|---|
FACTORY_BEAN_PREFIX |
| Fields inherited from interface org.springframework.context.ConfigurableApplicationContext |
|---|
LOAD_TIME_WEAVER_BEAN_NAME |
| Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver |
|---|
CLASSPATH_ALL_URL_PREFIX |
| Fields inherited from interface org.springframework.core.io.ResourceLoader |
|---|
CLASSPATH_URL_PREFIX |
| Constructor Summary | |
|---|---|
JavaConfigApplicationContext()
requires calling refresh() TODO: finish doc |
|
JavaConfigApplicationContext(ApplicationContext parent)
requires calling refresh() TODO: finish doc |
|
JavaConfigApplicationContext(ApplicationContext parent,
Class<?>... classes)
|
|
JavaConfigApplicationContext(ApplicationContext parent,
Class<?>[] classes,
String[] basePackages)
TODO: Document |
|
JavaConfigApplicationContext(ApplicationContext parent,
String... basePackages)
|
|
JavaConfigApplicationContext(Class<?>... classes)
|
|
JavaConfigApplicationContext(Class<?>[] classes,
String[] basePackages)
|
|
JavaConfigApplicationContext(String... basePackages)
|
|
| Method Summary | ||
|---|---|---|
void |
addConfigClass(Class<?> cls)
Allows for incrementally building up the configuration classes to be processed by this context. |
|
protected void |
finishRefresh()
|
|
protected String[] |
getBasePackages()
|
|
|
getBean(Class<T> type)
Return an instance of the given type. |
|
|
getBean(Class<T> type,
String beanName)
Return an instance named beanName and of type type. |
|
protected Class<?>[] |
getConfigClasses()
|
|
protected void |
loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
|
|
protected void |
prepareRefresh()
|
|
protected void |
registerDefaultPostProcessors()
Register the default post processors used for parsing Spring classes. |
|
void |
setBasePackages(String... basePackages)
The base packages for configurations from Strings. |
|
void |
setConfigClasses(Class<?>... classes)
|
|
void |
setParent(ApplicationContext context)
|
|
| Methods inherited from class org.springframework.context.support.AbstractRefreshableApplicationContext |
|---|
closeBeanFactory, createBeanFactory, customizeBeanFactory, getBeanFactory, hasBeanFactory, refreshBeanFactory |
| Methods inherited from class org.springframework.core.io.DefaultResourceLoader |
|---|
getClassLoader, getResource, getResourceByPath, setClassLoader |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface org.springframework.beans.factory.BeanFactory |
|---|
containsBean, getAliases, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch |
| Methods inherited from interface org.springframework.core.io.ResourceLoader |
|---|
getClassLoader, getResource |
| Constructor Detail |
|---|
public JavaConfigApplicationContext()
public JavaConfigApplicationContext(ApplicationContext parent)
parent - public JavaConfigApplicationContext(String... basePackages)
public JavaConfigApplicationContext(Class<?>... classes)
public JavaConfigApplicationContext(ApplicationContext parent,
Class<?>... classes)
public JavaConfigApplicationContext(ApplicationContext parent,
String... basePackages)
public JavaConfigApplicationContext(Class<?>[] classes,
String[] basePackages)
public JavaConfigApplicationContext(ApplicationContext parent,
Class<?>[] classes,
String[] basePackages)
parent - classes - basePackages - prepareRefresh(),
finishRefresh()| Method Detail |
|---|
protected void prepareRefresh()
prepareRefresh in class AbstractApplicationContextpublic void setConfigClasses(Class<?>... classes)
public void addConfigClass(Class<?> cls)
cls - public void setBasePackages(String... basePackages)
public void setParent(ApplicationContext context)
setParent in interface ConfigurableApplicationContextsetParent in class AbstractApplicationContextprotected String[] getBasePackages()
protected Class<?>[] getConfigClasses()
protected void finishRefresh()
finishRefresh in class AbstractApplicationContext
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
throws IOException,
BeansException
loadBeanDefinitions in class AbstractRefreshableApplicationContextIOException
BeansExceptionprotected void registerDefaultPostProcessors()
JavaConfigBeanFactoryPostProcessorRegistrypublic <T> T getBean(Class<T> type)
Primary
type - desired instance type
Primary
public <T> T getBean(Class<T> type,
String beanName)
Primary
This method is similar to its predecessor
BeanFactory.getBean(String, Class), but this variant takes
advantages of generics and removes the casting burden from the caller.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||