@Override public ConfigurableApplicationContext createSpringApp(Config config, Class... classes) { logger.debug("Configuring Spring"); AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.setAllowCircularReferences(config.isAllowCircularReferences()); rootContext.register(classes); rootContext.scan(config.getBasePackages()); rootContext.refresh(); logger.debug("Configuring Additional Spring Beans"); ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) rootContext).getBeanFactory(); config.getDataSources() .stream() .map(Tuple2::_1) .filter(it -> !new ConfigAccessor().get() .getDefaultDataSourceName() .equals(it)) .forEach(name -> { List<SpringDBConfig> dbConfig = getConfig(config, rootContext, beanFactory); dbConfig.forEach(spring -> spring.createSpringApp(name)); }); logger.debug("Finished Configuring Spring"); return rootContext; }
/** * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely. */ @Test public void annotationConfigWAC() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("does.not.matter"); ContextLoaderListener cll = new ContextLoaderListener(ctx); cll.contextInitialized(new ServletContextEvent(new MockServletContext())); } }
public class Initializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("com.package.name"); //the rest of initialization } }
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.scan("com.doniies.doniiesweb.config");
@Override protected AnnotationConfigWebApplicationContext newApplicationContext() { AnnotationConfigWebApplicationContext context = super.newApplicationContext(); context.scan(ModuleInitializer.this.springPackagesToScan()); return context; }
public class AppInit implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { // Create webapp context AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.scan("my_package"); root.register(SpringSecurityConfiguration.class); ...# }
@Override protected AnnotationConfigWebApplicationContext newApplicationContext() { AnnotationConfigWebApplicationContext context = super.newApplicationContext(); context.scan(ServerInitializer.this.springPackagesToScan()); return context; }
@Override protected AnnotationConfigWebApplicationContext newApplicationContext() { AnnotationConfigWebApplicationContext context = super.newApplicationContext(); context.scan(SingleAppInitializer.this.springPackagesToScan()); return context; }
@Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.setParent(root); appContext.scan(packagesToScan); return appContext; }
@Override protected AnnotationConfigWebApplicationContext newApplicationContext() { AnnotationConfigWebApplicationContext context = super.newApplicationContext(); context.scan(ServerInitializer.this.springPackagesToScan()); return context; }
@Override protected AnnotationConfigWebApplicationContext newApplicationContext() { AnnotationConfigWebApplicationContext context = super.newApplicationContext(); context.scan(SingleAppInitializer.this.springPackagesToScan()); return context; }
private WebApplicationContext createRootContext(ServletContext servletContext) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); //rootContext.register(CoreConfig.class, SecurityConfig.class, OAuth2ServerConfig.class, MethodSecurityConfig.class); //rootContext.refresh(); rootContext.scan(ClassUtils.getPackageName(this.getClass())); servletContext.addListener(new ContextLoaderListener(rootContext)); servletContext.setInitParameter("defaultHtmlEscape", "true"); return rootContext; }
context.register(classes.toArray(new Class[classes.size()])); context.scan(namespace.toArray(new String[namespace.size()])); } catch (Exception ex) { throw new ServletException("Cannot register modules.", ex);
@Override protected void registerConfigClasses(AnnotationConfigWebApplicationContext context, Config config) { configureStatsCollector(context, config); context.scan(addStatsPackage(getPackagesToScan(config))); }
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 public void onStartup(ServletContext servletContext) throws ServletException { // root context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(RootConfig.class); // configuration class for root context rootContext.scan("...service", "...dao"); // scan only some packages servletContext.addListener(new ContextLoaderListener(rootContext)); // dispatcher servlet 1 AnnotationConfigWebApplicationContext webContext1 = new AnnotationConfigWebApplicationContext(); webContext1.setParent(rootContext); webContext1.register(WebConfig1.class); // configuration class for servlet 1 webContext1.scan("...web1"); // scan some other packages ServletRegistration.Dynamic dispatcher1 = servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1)); dispatcher1.setLoadOnStartup(1); dispatcher1.addMapping("/subcontext1"); // dispatcher servlet 2 ... }
@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 WebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { DispatcherServlet dispatcherServlet = new DispatcherServlet(getContext()); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic registration = container.addServlet("dispatcher", dispatcherServlet); registration.setLoadOnStartup(1); registration.addMapping("/"); } private AnnotationConfigWebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.my.config"); context.scan("com.my.controllers"); return context; } }
@Override public ConfigurableApplicationContext createSpringApp(Config config, Class... classes) { logger.debug("Configuring Spring"); AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.setAllowCircularReferences(config.isAllowCircularReferences()); rootContext.register(classes); rootContext.scan(config.getBasePackages()); rootContext.refresh(); logger.debug("Configuring Additional Spring Beans"); ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) rootContext).getBeanFactory(); config.getDataSources() .stream() .map(Tuple2::_1) .filter(it -> !new ConfigAccessor().get() .getDefaultDataSourceName() .equals(it)) .forEach(name -> { List<SpringDBConfig> dbConfig = getConfig(config, rootContext, beanFactory); dbConfig.forEach(spring -> spring.createSpringApp(name)); }); logger.debug("Finished Configuring Spring"); return rootContext; }