/** * Apply any relevant post processing the {@link ApplicationContext}. Subclasses can * apply additional processing as required. * @param context the application context */ protected void postProcessApplicationContext(ConfigurableApplicationContext context) { if (this.beanNameGenerator != null) { context.getBeanFactory().registerSingleton( AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, this.beanNameGenerator); } if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { ((GenericApplicationContext) context) .setResourceLoader(this.resourceLoader); } if (context instanceof DefaultResourceLoader) { ((DefaultResourceLoader) context) .setClassLoader(this.resourceLoader.getClassLoader()); } } if (this.addConversionService) { context.getBeanFactory().setConversionService( ApplicationConversionService.getSharedInstance()); } }
/** * Sets the ClassLoader used to load bean definition classes on the Spring ApplicationContext. * * @param applicationContext the Spring ApplicationContext in which to configure the ClassLoader. * @return the given Spring ApplicationContext. * @see org.springframework.core.io.DefaultResourceLoader#setClassLoader(ClassLoader) * @see java.lang.ClassLoader */ ConfigurableApplicationContext setClassLoader(ConfigurableApplicationContext applicationContext) { ClassLoader beanClassLoader = beanClassLoaderReference.get(); if (applicationContext instanceof DefaultResourceLoader && beanClassLoader != null) { ((DefaultResourceLoader) applicationContext).setClassLoader(beanClassLoader); } return applicationContext; }
/** * Sets the ClassLoader used to load bean definition classes on the Spring ApplicationContext. * * @param applicationContext the Spring ApplicationContext in which to configure the ClassLoader. * @return the given Spring ApplicationContext. * @see org.springframework.core.io.DefaultResourceLoader#setClassLoader(ClassLoader) * @see java.lang.ClassLoader */ ConfigurableApplicationContext setClassLoader(ConfigurableApplicationContext applicationContext) { ClassLoader beanClassLoader = beanClassLoaderReference.get(); if (applicationContext instanceof DefaultResourceLoader && beanClassLoader != null) { ((DefaultResourceLoader) applicationContext).setClassLoader(beanClassLoader); } return applicationContext; }
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) { if (child instanceof DefaultResourceLoader) { ((DefaultResourceLoader) child) .setClassLoader(this.applicationContext.getClassLoader()); } }
public AbstractApplicationContext getApplicationContext() { if (applicationContext != null) { return applicationContext; } else { final String config = getConfig(); if (config == null) { applicationContext = new ClassPathXmlApplicationContext(DEFAULT_CONFIG_LOCATION); } else { applicationContext = new FileSystemXmlApplicationContext("file:" + config); } applicationContext.setClassLoader(getClass().getClassLoader()); applicationContext.refresh(); } return applicationContext; }
public void setConfig(String config) { this.config = config; logger.debug("Loading configuration from [" + config + "]."); try { applicationContext = new FileSystemXmlApplicationContext( new String[] { "file:" + config }, false); applicationContext.setClassLoader(getClass().getClassLoader()); applicationContext.refresh(); this.applicationContext.getBeanFactory().autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, isDependencyCheck()); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } }
@POST @Path(":restart") public void restartApplication() throws Exception { codeBase.clear(); OgnlUtils.clearCache(); if(applicationContext instanceof ConfigurableApplicationContext) { //Spring enhances @Configuration classes. To do so it loads them by name from its classloader. //Thus, replacing the classloader with a fresh one has also the side-effect of making Spring reload the user //SpringConfiguration class, provided it already existed and was annotated with @Configuration. //Note that Spring won't pick up new @Bean methods. It will also barf badly on removed @Bean methods, //effectively crashing the application. Changing the return value and even the return type is fine. ((DefaultResourceLoader) applicationContext).setClassLoader(codeBase.asClassLoader()); ((ConfigurableApplicationContext) applicationContext).refresh(); } }
private void configureContext(C context) { if (this.parent != null) { context.setParent(this.parent); } if (this.classLoader != null) { Assert.isInstanceOf(DefaultResourceLoader.class, context); ((DefaultResourceLoader) context).setClassLoader(this.classLoader); } this.environmentProperties.applyTo(context); Class<?>[] classes = Configurations.getClasses(this.configurations); if (classes.length > 0) { ((AnnotationConfigRegistry) context).register(classes); } this.initializers.forEach((initializer) -> initializer.initialize(context)); context.refresh(); }
protected ApplicationContextInitializer<?>[] getRootApplicationContextInitializers() { return new ApplicationContextInitializer[] { applicationContext -> { ElementsThreadLocals.setupDefaultElementsContext(); MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources(); Configuration configuration = (Configuration) servletContext.getAttribute(BaseModule.PORTOFINO_CONFIGURATION); sources.addFirst( new ConfigurationPropertySource("portofino.properties", configuration)); AnnotationConfigRegistry annotationConfig = (AnnotationConfigRegistry) applicationContext; for(Class<?> moduleClass : moduleClasses) { annotationConfig.register(moduleClass); } annotationConfig.register(PortofinoSpringConfiguration.class); CodeBase codeBase = (CodeBase) servletContext.getAttribute(PortofinoListener.CODE_BASE_ATTRIBUTE); ((DefaultResourceLoader) applicationContext).setClassLoader(codeBase.asClassLoader()); try { annotationConfig.register(codeBase.loadClass("SpringConfiguration")); } catch (Exception e) { logger.info("User-defined Spring configuration not found", e); } } }; } }