, , , . , context.getParentBeanFactory() context.getParent() ApplicationContextAware ApplicationContext.
- , . , Tomcat:
- next,
DispatcherServlet .
, beans : singleton beans .
, , :
- (). IMHO, , WebApplicationContextes . :
- beans beans: beans
- (, , DAO ..) , .
- ApplicationContext , beans, , .
- beans . , , beans beans. bean bean , , ( ), ... beans,
- .
, Spring, . , (1- ).
beans , , , - :
@Configuration
public class FeatureConfig implements ApplicationContextAware {
static boolean needInit = true;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
AnnotationConfigWebApplicationContext parent =
(AnnotationConfigWebApplicationContext) ((AnnotationConfigWebApplicationContext) ac).getParent();
if (needInit) {
needInit = false;
parent.register(RootConfig.class);
parent.refresh();
((AnnotationConfigWebApplicationContext) ac).refresh();
}
}
@Configuration
@Conditional(NoParentContext.class)
public static class RootConfig {
}
public static class NoParentContext implements Condition {
@Override
public boolean matches(ConditionContext cc, AnnotatedTypeMetadata atm) {
logger.debug(" {} parent {}", cc.getBeanFactory(), cc.getBeanFactory().getParentBeanFactory());
return (cc.getBeanFactory().getParentBeanFactory() == null);
}
}
}
@Configuration @import(FeatureConfig.class) DispatcherServlet.
. , bean @Autowired(required=false), , , .