org.springframework.web.context.support.XmlWebApplicationContext Java Examples
The following examples show how to use
org.springframework.web.context.support.XmlWebApplicationContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextLoaderTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(contextAttr)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }
Example #2
Source File: IbisApplicationContext.java From iaf with Apache License 2.0 | 6 votes |
/** * Loads springContext, springUnmanagedDeployment, springCommon and files specified by the SPRING.CONFIG.LOCATIONS * property in AppConstants.properties * * @param classLoader to use in order to find and validate the Spring Configuration files * @return A String array containing all files to use. */ private String[] getSpringConfigurationFiles(ClassLoader classLoader) { List<String> springConfigurationFiles = new ArrayList<String>(); springConfigurationFiles.add(XmlWebApplicationContext.CLASSPATH_URL_PREFIX + "/springUnmanagedDeployment.xml"); springConfigurationFiles.add(XmlWebApplicationContext.CLASSPATH_URL_PREFIX + "/springCommon.xml"); StringTokenizer locationTokenizer = AppConstants.getInstance().getTokenizedProperty("SPRING.CONFIG.LOCATIONS"); while(locationTokenizer.hasMoreTokens()) { String file = locationTokenizer.nextToken(); if(log.isDebugEnabled()) log.debug("found spring configuration file to load ["+file+"]"); URL fileURL = classLoader.getResource(file); if(fileURL == null) { log.error("unable to locate Spring configuration file ["+file+"]"); } else { if(file.indexOf(":") == -1) { file = XmlWebApplicationContext.CLASSPATH_URL_PREFIX+"/"+file; } springConfigurationFiles.add(file); } } log.info("loading Spring configuration files "+springConfigurationFiles+""); return springConfigurationFiles.toArray(new String[springConfigurationFiles.size()]); }
Example #3
Source File: SpringResourceLoader.java From rice with Educational Community License v2.0 | 6 votes |
@Override public void start() throws Exception { if(!isStarted()){ LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ",")); if (parentSpringResourceLoader != null && parentContext != null) { throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined. Only one can be defined!"); } if (parentSpringResourceLoader != null) { parentContext = parentSpringResourceLoader.getContext(); } if (servletContextcontext != null) { XmlWebApplicationContext lContext = new XmlWebApplicationContext(); lContext.setServletContext(servletContextcontext); lContext.setParent(parentContext); lContext.setConfigLocations(this.fileLocs.toArray(new String[] {})); lContext.refresh(); context = lContext; } else { this.context = new ClassPathXmlApplicationContext(this.fileLocs.toArray(new String[] {}), parentContext); } super.start(); } }
Example #4
Source File: XmlWebApplicationContextTests.java From spring-analysis-note with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
Example #5
Source File: MockTdsContextLoader.java From tds with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public ApplicationContext loadContext(String... locations) throws Exception { final MockServletContext servletContext = new MockTdsServletContext(); final MockServletConfig servletConfig = new MockServletConfig(servletContext); final XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext); webApplicationContext.setServletConfig(servletConfig); webApplicationContext.setConfigLocations(locations); webApplicationContext.refresh(); TdsContext tdsContext = webApplicationContext.getBean(TdsContext.class); checkContentRootPath(webApplicationContext, tdsContext); webApplicationContext.registerShutdownHook(); return webApplicationContext; }
Example #6
Source File: ContextLoaderTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(contextAttr)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }
Example #7
Source File: WebAppInitializer.java From tutorials with MIT License | 6 votes |
public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebMvcConfigure.class); ctx.setServletContext(container); ServletRegistration.Dynamic servletOne = container.addServlet("SpringProgrammaticDispatcherServlet", new DispatcherServlet(ctx)); servletOne.setLoadOnStartup(1); servletOne.addMapping("/"); XmlWebApplicationContext xctx = new XmlWebApplicationContext(); xctx.setConfigLocation("/WEB-INF/context.xml"); xctx.setServletContext(container); ServletRegistration.Dynamic servletTwo = container.addServlet("SpringProgrammaticXMLDispatcherServlet", new DispatcherServlet(xctx)); servletTwo.setLoadOnStartup(1); servletTwo.addMapping("/"); }
Example #8
Source File: IbisApplicationInitializer.java From iaf with Apache License 2.0 | 6 votes |
@Override protected WebApplicationContext createWebApplicationContext(ServletContext servletContext) { System.setProperty(EndpointImpl.CHECK_PUBLISH_ENDPOINT_PERMISSON_PROPERTY_WITH_SECURITY_MANAGER, "false"); servletContext.log("Starting IBIS WebApplicationInitializer"); XmlWebApplicationContext applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocation(XmlWebApplicationContext.CLASSPATH_URL_PREFIX + "/webApplicationContext.xml"); applicationContext.setDisplayName("IbisApplicationInitializer"); MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); propertySources.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); propertySources.remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); propertySources.addFirst(new PropertiesPropertySource("ibis", AppConstants.getInstance())); return applicationContext; }
Example #9
Source File: SimpleUrlHandlerMappingTests.java From java-technology-stack with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example #10
Source File: SimpleUrlHandlerMappingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example #11
Source File: XmlWebApplicationContextTests.java From java-technology-stack with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
Example #12
Source File: InstallWizard.java From openmeetings with Apache License 2.0 | 6 votes |
@Override protected void onSubmit() { try { ConnectionPropertiesPatcher.patch(getModelObject()); XmlWebApplicationContext ctx = (XmlWebApplicationContext)getWebApplicationContext(Application.get().getServletContext()); if (ctx == null) { form.error(new StringResourceModel("install.wizard.db.step.error.patch", InstallWizard.this).setParameters("Web context is NULL").getObject()); log.error("Web context is NULL"); return; } LocalEntityManagerFactoryBean emb = ctx.getBeanFactory().getBean(LocalEntityManagerFactoryBean.class); emb.afterPropertiesSet(); dbType = getModelObject().getDbType(); } catch (Exception e) { form.error(new StringResourceModel("install.wizard.db.step.error.patch", InstallWizard.this).setParameters(e.getMessage()).getObject()); log.error("error while patching", e); } }
Example #13
Source File: WiringTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations( "file:src/main/webapp/WEB-INF/cas-servlet.xml", "file:src/main/webapp/WEB-INF/deployerConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #14
Source File: WiringTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations( "file:src/main/webapp/WEB-INF/cas-management-servlet.xml", "file:src/main/webapp/WEB-INF/managementConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #15
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-servlet.xml", "file:src/main/webapp/WEB-INF/deployerConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #16
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-management-servlet.xml", "file:src/main/webapp/WEB-INF/managementConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example #17
Source File: LayuiAdminStartUp.java From layui-admin with MIT License | 6 votes |
@Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addInitializers(new ServletContextInitializer(){ @Override public void onStartup(ServletContext servletContext) throws ServletException { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(new String[]{"classpath:applicationContext-springmvc.xml"}); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); ServletRegistration.Dynamic dispatcher = servletContext .addServlet("dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }); tomcat.setContextPath("/manager"); tomcat.addTldSkipPatterns("xercesImpl.jar","xml-apis.jar","serializer.jar"); tomcat.setPort(port); return tomcat; }
Example #18
Source File: DispatcherServletChannelInitializer.java From nettyholdspringmvc with Apache License 2.0 | 6 votes |
public DispatcherServletChannelInitializer() throws ServletException { MockServletContext servletContext = new MockServletContext(); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); servletContext.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); //AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); //ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext(); wac.setServletContext(servletContext); wac.setServletConfig(servletConfig); wac.setConfigLocation("classpath:/servlet-context.xml"); //wac.register(WebConfig.class); wac.refresh(); this.dispatcherServlet = new DispatcherServlet(wac); this.dispatcherServlet.init(servletConfig); }
Example #19
Source File: SimpleUrlHandlerMappingTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void handlerBeanNotFound() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"}); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2err.xml"}); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example #20
Source File: XmlWebApplicationContextTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
Example #21
Source File: ContextLoaderTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }
Example #22
Source File: LambdaHandler.java From aws-serverless-java-container with Apache License 2.0 | 6 votes |
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) { if (!isinitialized) { isinitialized = true; try { XmlWebApplicationContext wc = new XmlWebApplicationContext(); wc.setConfigLocation("classpath:/staticAppContext.xml"); handler = SpringLambdaContainerHandler.getAwsProxyHandler(wc); } catch (ContainerInitializationException e) { e.printStackTrace(); return null; } } AwsProxyResponse res = handler.proxy(awsProxyRequest, context); return res; }
Example #23
Source File: StreamingReceiver.java From kylin with Apache License 2.0 | 5 votes |
private void startHttpServer() throws Exception { KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv(); createAndConfigHttpServer(kylinConfig); ContextHandlerCollection contexts = new ContextHandlerCollection(); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/kylin"); XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("classpath:applicationContext.xml"); ctx.refresh(); DispatcherServlet dispatcher = new DispatcherServlet(ctx); context.addServlet(new ServletHolder(dispatcher), "/api/*"); ContextHandler logContext = new ContextHandler("/kylin/logs"); String logDir = getLogDir(kylinConfig); ResourceHandler logHandler = new ResourceHandler(); logHandler.setResourceBase(logDir); logHandler.setDirectoriesListed(true); logContext.setHandler(logHandler); contexts.setHandlers(new Handler[] { context, logContext }); httpServer.setHandler(contexts); httpServer.start(); httpServer.join(); }
Example #24
Source File: XmlConfigServer.java From json-view with GNU General Public License v3.0 | 5 votes |
public synchronized void start(final int port) { if(thread != null) { throw new IllegalStateException("Server is already running"); } thread = new Thread(new Runnable() { @Override public void run() { try { Server server = new Server(port); final XmlWebApplicationContext xmlBasedContext = new XmlWebApplicationContext(); //System.out.println(xmlBasedContext.getEnvironment().getClass()); xmlBasedContext.setConfigLocation("classpath:context.xml"); final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(xmlBasedContext)); final ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); context.addServlet(servletHolder, "/*"); server.setHandler(context); running = true; server.start(); log.info("Server started"); while(running) { Thread.sleep(1); } server.stop(); log.info("Server stopped"); } catch(Exception e) { log.error("Server exception", e); throw new RuntimeException(e); } } }); thread.start(); }
Example #25
Source File: CoreSpringFactory.java From olat with Apache License 2.0 | 5 votes |
/** * @param beanName * The bean name to check for. Be sure the bean does exist, otherwise an NoSuchBeanDefinitionException will be thrown * @return The bean * @throws RuntimeException * when more than one bean of the same type is registered. */ public static <T> T getBean(Class<T> beanType) { // log.info("beanType=" + beanType); // log.info("beanType.getInterfaces()=" + beanType.getInterfaces()); ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext); Map<String, T> m = context.getBeansOfType(beanType); if (m.size() > 1) { // with following code it is possible to configure multiple implementations for an interface (e.g. adapted spring config files for testing) // and to give it preference by setting primary="true" in <bean> definition tag. if (context instanceof XmlWebApplicationContext) { ConfigurableListableBeanFactory clbf = ((XmlWebApplicationContext) context).getBeanFactory(); String[] beanNames = clbf.getBeanNamesForType(beanType); for (String beanName : beanNames) { BeanDefinition bd = clbf.getBeanDefinition(beanName); if (bd.isPrimary()) { return context.getBean(beanName, beanType); } } } // more than one bean found -> exception throw new OLATRuntimeException("found more than one bean for: " + beanType + ". Calling this method should only find one bean!", null); } else if (m.size() == 1) { return m.values().iterator().next(); } // fallback for beans named like the fully qualified path (legacy) Object o = context.getBean(beanType.getName()); beanNamesCalledFromSource.add(beanType.getName()); return (T) o; }
Example #26
Source File: PetclinicInitializer.java From DevOps-for-Web-Development with MIT License | 5 votes |
@Override protected WebApplicationContext createRootApplicationContext() { XmlWebApplicationContext rootAppContext = new XmlWebApplicationContext(); rootAppContext.setConfigLocations("classpath:spring/business-config.xml", "classpath:spring/tools-config.xml"); rootAppContext.getEnvironment().setActiveProfiles(SPRING_PROFILE); return rootAppContext; }
Example #27
Source File: PathMatchingUrlHandlerMappingTests.java From spring-analysis-note with MIT License | 5 votes |
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); hm = (HandlerMapping) wac.getBean("urlMapping"); }
Example #28
Source File: InterAccountSNSPermissionTest.java From spring-integration-aws with MIT License | 5 votes |
private ServletContextHandler getServletContextHandler() throws Exception { context = new XmlWebApplicationContext(); context.setConfigLocation("src/main/webapp/WEB-INF/InterAccountSNSPermissionFlow.xml"); context.registerShutdownHook(); ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setErrorHandler(null); contextHandler.setResourceBase("."); ServletHolder servletHolder = new ServletHolder(new DispatcherServlet( context)); contextHandler.addServlet(servletHolder, "/*"); return contextHandler; }
Example #29
Source File: ExtensionsAdminController.java From olat with Apache License 2.0 | 5 votes |
/** * hmmm, does not work yet, how to get a list with all overwritten beans like the output from to the log.info * http://www.docjar.com/html/api/org/springframework/beans/factory/support/DefaultListableBeanFactory.java.html * * @return */ private List getOverwrittenBeans() { final ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext); final XmlWebApplicationContext context = (XmlWebApplicationContext) applicationContext; final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (int i = 0; i < beanDefinitionNames.length; i++) { final String beanName = beanDefinitionNames[i]; if (!beanName.contains("#")) { final BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); // System.out.println(beanDef.getOriginatingBeanDefinition()); } } return null; }
Example #30
Source File: EnvironmentSystemIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void xmlWebApplicationContext() { AbstractRefreshableWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("classpath:" + XML_PATH); ctx.setEnvironment(prodWebEnv); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }