private void loadBeanDefinitions(String fileName) { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext); ClassPathResource resource = new ClassPathResource(fileName, AnnotationDrivenBeanDefinitionParserTests.class); reader.loadBeanDefinitions(resource); appContext.refresh(); }
/** * Load a Spring {@link WebApplicationContext} from the supplied {@link * MergedContextConfiguration}. * * <p>Implementation details: * * <ul> * <li>Creates a {@link GenericWebApplicationContext} instance. * <li>Delegates to {@link #configureWebResources} to create the {@link MockServletContext} and * set it in the {@code WebApplicationContext}. * <li>Calls {@link #prepareContext} to allow for customizing the context before bean * definitions are loaded. * <li>Calls {@link #customizeBeanFactory} to allow for customizing the context's {@code * DefaultListableBeanFactory}. * <li>Delegates to {@link #loadBeanDefinitions} to populate the context from the locations or * classes in the supplied {@code MergedContextConfiguration}. * <li>Delegates to {@link AnnotationConfigUtils} for {@linkplain * AnnotationConfigUtils#registerAnnotationConfigProcessors registering} annotation * configuration processors. * <li>Calls {@link #customizeContext} to allow for customizing the context before it is * refreshed. * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the context and registers a JVM * shutdown hook for it. * </ul> * * @return a new web application context * @see * org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) * @see GenericWebApplicationContext */ @Override public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { if (!(mergedConfig instanceof WebMergedContextConfiguration)) { throw new IllegalArgumentException( String.format( "Cannot load WebApplicationContext from non-web merged context configuration %s. " + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig)); } WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig; if (logger.isDebugEnabled()) { logger.debug( String.format( "Loading WebApplicationContext for merged context configuration %s.", webMergedConfig)); } GenericWebApplicationContext context = new GenericWebApplicationContext(); configureWebResources(context, webMergedConfig); prepareContext(context, webMergedConfig); customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig); loadBeanDefinitions(context, webMergedConfig); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); customizeContext(context, webMergedConfig); context.refresh(); context.registerShutdownHook(); return context; }
@Override protected WebApplicationContext initApplicationContext() { MockServletContext servletContext = new MockServletContext(); applicationContext = new GenericWebApplicationContext(servletContext); applicationContext.refresh(); applicationContext.getAutowireCapableBeanFactory().initializeBean(validator, "validator"); return applicationContext; }
private WebApplicationContext initApplicationContext(String scope) { MockServletContext sc = new MockServletContext(); GenericWebApplicationContext ac = new GenericWebApplicationContext(sc); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(DerivedTestBean.class); bd.setScope(scope); ac.registerBeanDefinition(NAME, bd); ac.refresh(); return ac; }
private Object getProxyBean(Object handler) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(handler.getClass())); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory() .registerSingleton("advsr", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); wac.refresh(); return wac.getBean("controller"); }
public ApplicationContext loadContext(String... locations) throws Exception { if (logger.isDebugEnabled()) { logger.debug( "Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "]."); } GenericWebApplicationContext context = new GenericWebApplicationContext(); prepareContext(context); customizeBeanFactory(context.getDefaultListableBeanFactory()); createBeanDefinitionReader(context).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); customizeContext(context); context.refresh(); context.registerShutdownHook(); return context; }
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception { GenericWebApplicationContext context = new GenericWebApplicationContext(); // Commented out until SPR-10392 is fixed and we can upgrade to Spring 3.2.3.RELEASE or higher // ApplicationContext parent = config.getParentApplicationContext(); // if(parent != null) { // context.setParent(parent); // } prepareContext(context); prepareContext(context, config); customizeBeanFactory(context.getDefaultListableBeanFactory()); createBeanDefinitionReader(context).loadBeanDefinitions(config.getLocations()); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); customizeContext(context); context.refresh(); context.registerShutdownHook(); return context; }
@SuppressWarnings({"unchecked", "rawtypes"}) @Before public void before() throws Exception { myFhirCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); myFhirCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); if (ourServer == null) { ourPort = RandomServerPortProvider.findFreePort(); ourRestServer = new RestfulServer(myFhirCtx); ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; ourRestServer.setResourceProviders((List) myResourceProviders); ourRestServer .getFhirContext() .setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); myTerminologyUploaderProvider = myAppCtx.getBean(TerminologyUploaderProviderDstu3.class); ourRestServer.setPlainProviders(mySystemProvider, myTerminologyUploaderProvider); JpaConformanceProviderDstu3 confProvider = new JpaConformanceProviderDstu3(ourRestServer, mySystemDao, myDaoConfig); confProvider.setImplementationDescription("THIS IS THE DESC"); ourRestServer.setServerConformanceProvider(confProvider); ourRestServer.setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class)); Server server = new Server(ourPort); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(ourRestServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourWebApplicationContext = new GenericWebApplicationContext(); ourWebApplicationContext.setParent(myAppCtx); ourWebApplicationContext.refresh(); // ContextLoaderListener loaderListener = new ContextLoaderListener(webApplicationContext); // loaderListener.initWebApplicationContext(mock(ServletContext.class)); // proxyHandler .getServletContext() .setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ourWebApplicationContext); DispatcherServlet dispatcherServlet = new DispatcherServlet(); // dispatcherServlet.setApplicationContext(webApplicationContext); dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class); ServletHolder subsServletHolder = new ServletHolder(); subsServletHolder.setServlet(dispatcherServlet); subsServletHolder.setInitParameter( ContextLoader.CONFIG_LOCATION_PARAM, WebsocketDstu3Config.class.getName()); proxyHandler.addServlet(subsServletHolder, "/*"); // Register a CORS filter CorsConfiguration config = new CorsConfiguration(); CorsInterceptor corsInterceptor = new CorsInterceptor(config); config.addAllowedHeader("x-fhir-starter"); config.addAllowedHeader("Origin"); config.addAllowedHeader("Accept"); config.addAllowedHeader("X-Requested-With"); config.addAllowedHeader("Content-Type"); config.addAllowedHeader("Access-Control-Request-Method"); config.addAllowedHeader("Access-Control-Request-Headers"); config.addAllowedOrigin("*"); config.addExposedHeader("Location"); config.addExposedHeader("Content-Location"); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); ourRestServer.registerInterceptor(corsInterceptor); server.setHandler(proxyHandler); server.start(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext( subsServletHolder.getServlet().getServletConfig().getServletContext()); myValidationSupport = wac.getBean(JpaValidationSupportChainDstu3.class); ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase); ourClient.registerInterceptor(new LoggingInterceptor(true)); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setConnectionManager(connectionManager); ourHttpClient = builder.build(); ourServer = server; } }