/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
/** * Build a {@link ClassPathBeanDefinitionScanner} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected ClassPathBeanDefinitionScanner getClassPathBeanDefinitionScanner(DefaultListableBeanFactory beanFactory) { return new ClassPathBeanDefinitionScanner(beanFactory, true, getEnvironment()); }
/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
/** * Build a {@link ClassPathBeanDefinitionScanner} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected ClassPathBeanDefinitionScanner getClassPathBeanDefinitionScanner(DefaultListableBeanFactory beanFactory) { return new ClassPathBeanDefinitionScanner(beanFactory, true, getEnvironment()); }
/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
/** * Build a {@link ClassPathBeanDefinitionScanner} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected ClassPathBeanDefinitionScanner getClassPathBeanDefinitionScanner(DefaultListableBeanFactory beanFactory) { return new ClassPathBeanDefinitionScanner(beanFactory, true, getEnvironment()); }
/** * Build a {@link ClassPathBeanDefinitionScanner} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected ClassPathBeanDefinitionScanner getClassPathBeanDefinitionScanner(DefaultListableBeanFactory beanFactory) { return new ClassPathBeanDefinitionScanner(beanFactory, true, getEnvironment()); }
/** * Build an {@link AnnotatedBeanDefinitionReader} for the given bean factory. * <p>This should be pre-configured with the {@code Environment} (if desired) * but not with a {@code BeanNameGenerator} or {@code ScopeMetadataResolver} yet. * @param beanFactory the bean factory to load bean definitions into * @since 4.1.9 * @see #getEnvironment() * @see #getBeanNameGenerator() * @see #getScopeMetadataResolver() */ protected AnnotatedBeanDefinitionReader getAnnotatedBeanDefinitionReader(DefaultListableBeanFactory beanFactory) { return new AnnotatedBeanDefinitionReader(beanFactory, getEnvironment()); }
private void configureStatsCollector(AnnotationConfigWebApplicationContext context, Config config) { if (Boolean.valueOf(config.getRaw().getOrDefault(GA_API_STATS_DISABLE_SETTING_LEGACY, "false"))) { context.getEnvironment().addActiveProfile("stats-null"); } else if (Boolean.valueOf(config.getRaw().getOrDefault(GA_API_STATS_DISABLE_SETTING, "false"))) { context.getEnvironment().addActiveProfile("stats-null"); } else { context.getEnvironment().addActiveProfile("stats-google"); } }
public class WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext sc) throws ServletException { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); // Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE' rootContext.getEnvironment().setDefaultProfiles("prod"); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context sc.addListener(new ContextLoaderListener(rootContext)); } }
class WebAppInitializer extends WebApplicationInitializer { void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.getEnvironment().setActiveProfiles("profileName"); rootContext.register(SpringConfiguration.class); container.addListener(new ContextLoaderListener(rootContext)); } }
private static WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE); return context; }
context.getEnvironment().setDefaultProfiles("dev");
context.getEnvironment().setDefaultProfiles("dev");
@Override public void initialize(final AnnotationConfigWebApplicationContext applicationContext) { /* Set appropriate profile */ final ConfigurableEnvironment environment = applicationContext.getEnvironment(); /* (Should be StandardServletEnvironment) */ environment.setActiveProfiles(QtiWorksProfiles.WEBAPP); /* Extract URI of deployment configuration. */ logger.info("Searching for required paremeter {} within {}", DEPLOYMENT_PROPERTIES_FILE_PARAM, environment.getPropertySources()); final String deploymentPropertiesUri = environment.getProperty(DEPLOYMENT_PROPERTIES_FILE_PARAM); if (deploymentPropertiesUri==null) { throw new QtiWorksDeploymentException("QTIWorks configuration error - required parameter " + DEPLOYMENT_PROPERTIES_FILE_PARAM + " was not found after searching " + environment.getPropertySources()); } /* Register property sources */ QtiWorksApplicationContextHelper.registerConfigPropertySources(applicationContext, deploymentPropertiesUri); /* Then set up ApplicationContext */ logger.info("Initialising QTIWorks webapp ApplicationContext"); applicationContext.register( PropertiesConfiguration.class, JpaProductionConfiguration.class, ServicesConfiguration.class, WebApplicationConfiguration.class ); } }
public class ApplicationContextUtil implements ApplicationContextAware { private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { ctx = appContext; } public static ApplicationContext getApplicationContext() { if (ctx == null) { logger.info(CloudServicesLogUtil .getLogMessage("Could not get Application Context from Web Application. Creating Application Context")); AnnotationConfigWebApplicationContext localCtx = new AnnotationConfigWebApplicationContext(); localCtx.getEnvironment().setActiveProfiles("production"); localCtx.scan("packages to scan"); localCtx.refresh(); ctx = localCtx; } return ctx; } }
@Override protected WebApplicationContext createRootApplicationContext() { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); ConfigurableEnvironment environment = rootContext.getEnvironment(); environment.setDefaultProfiles("production"); PropertyUtil propertyUtil = PropertyUtil.getInstance(environment.getActiveProfiles()); String[] basePackages = propertyUtil.getPropertySplitTrimmed("webapp", "basePackages"); rootContext.scan(basePackages); return rootContext; } @Override protected WebApplicationContext createServletApplicationContext() { return new AnnotationConfigWebApplicationContext(); }
public class WebServiceInitializer implements WebApplicationInitializer { private static final String ACTIVE_PROFILE = "production"; /** * Registers and loads on startup MessageDispatcherServlet for the SOAP messages */ @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); // @EnableWs, @Configuration, @ComponentScan context.setConfigLocation(WebServiceBeans.class.getName()); context.getEnvironment().setActiveProfiles(ACTIVE_PROFILE); // use MessageDispatcherServlet instead of standard DispatcherServlet for SOAP messages MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setContextClass(AnnotationConfigWebApplicationContext.class); servlet.setApplicationContext(context); servlet.setTransformWsdlLocations(true); // register MessageDispatcherServlet as Web Service entry point final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("MessageDispatcherServlet", servlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); }
@Test public void expectedSocialBeansCreated() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.social.facebook.appId:12345").applyTo(this.context); TestPropertyValues.of("spring.social.facebook.appSecret:secret") .applyTo(this.context); ConfigurationPropertySources.attach(this.context.getEnvironment()); this.context.register(FacebookAutoConfiguration.class); this.context.register(SocialWebAutoConfiguration.class); this.context.refresh(); assertConnectionFrameworkBeans(); assertThat(this.context.getBean(Facebook.class)).isNotNull(); }
@Test public void expectedSocialBeansCreated() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.social.twitter.appId:12345").applyTo(this.context); TestPropertyValues.of("spring.social.twitter.appSecret:secret") .applyTo(this.context); ConfigurationPropertySources.attach(this.context.getEnvironment()); this.context.register(TwitterAutoConfiguration.class); this.context.register(SocialWebAutoConfiguration.class); this.context.refresh(); assertConnectionFrameworkBeans(); assertThat(this.context.getBean(Twitter.class)).isNotNull(); }