@Test @SuppressWarnings("resource") public void configLocationWithSingleClass() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation(Config.class.getName()); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
@Test @SuppressWarnings("resource") public void withBeanNameGenerator() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return "custom-" + super.generateBeanName(definition, registry); } }); ctx.setConfigLocation(Config.class.getName()); ctx.refresh(); assertThat(ctx.containsBean("custom-myConfig"), is(true)); }
@Test public void annotationConfigWebApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setEnvironment(prodWebEnv); ctx.setConfigLocation(EnvironmentAwareBean.class.getName()); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); }
@Test @SuppressWarnings("resource") public void configLocationWithBasePackage() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation("org.springframework.web.context.support"); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
@Override protected void configureServlets() { AnnotationConfigWebApplicationContext webApp = new AnnotationConfigWebApplicationContext(); webApp.setConfigLocation(FhirTesterConfig.class.getName()); serve("/*").with(new DispatcherServlet(webApp)); bind(CORSFilter.class).in(Singleton.class); filter("/*").through(CORSFilter.class); } });
/** * Creates a Web Application Context that uses Spring annotations for configuration. * * @return A Spring annotations enabled Web Context. */ private static WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("org.locationtech.geogig.spring.config"); return context; }
private static WebApplicationContext getWebApplicationContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION_PACKAGE); return context; }
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("classpath:<your file location>");
private static WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(CONFIG_LOCATION); context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE); return context; }
context.setConfigLocation("org.openrepose.apis"); context.getEnvironment().setDefaultProfiles("dev");
context.setConfigLocation("com.example.config"); context.getEnvironment().setDefaultProfiles("dev");
// You named it WebConfig public class WebAppInitializer implements WebApplicationInitializer { // web.xml replacement @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); // Here you need to change "io.shido.config" to your config location applicationContext.setConfigLocation("io.shido.config"); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext)); // Register and map the dispatcher servlet //servletContext.setInitParameter("spring.profiles.default", "development"); servletContext.addListener(new ContextLoaderListener(applicationContext)); //applicationContext.register(AppConfig.class); // Manage the lifecycle of the root application context dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/web/*"); // ...and here also change the context } }
public void onStartup(final ServletContext servletContext) { servletContext.setInitParameter("contextConfigLocation", "<NONE>"); AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.register(SpringWebConfig.class); root.setConfigLocation("com.jtbdevelopment"); servletContext.addListener(new ContextLoaderListener(root)); Dynamic jersey = servletContext.addServlet("REST", ServletContainer.class); jersey.setLoadOnStartup(0); Map<String, String> map = new HashMap<>(); map.put("jersey.config.server.provider.packages", "com.jtbdevelopment"); map.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.message.DeflateEncoder;org.glassfish.jersey.message.GZipEncoder;org.glassfish.jersey.server.filter.EncodingFilter"); map.put("jersey.config.server.tracing", "ALL"); jersey.setInitParameters(map); jersey.addMapping("/api/*"); }
@Configuration public class AppInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext rootContext = getWebApplicationContext(); servletContext.addListener(new ContextLoaderListener(rootContext)); servletContext.setInitParameter("defaultHtmlEscape", "true"); } private AnnotationConfigWebApplicationContext getWebApplicationContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.app.config"); return context; } @Autowired public void configureDispatcher(DispatcherServlet dispatcherServlet){ dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); } }
context.setConfigLocation(Application.class.getName()); return context;
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; } }
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("/*"); }