/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resources and automatically refreshing the context. * @param resources the resources to load from */ public GenericXmlApplicationContext(Resource... resources) { load(resources); refresh(); }
/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resource locations and automatically refreshing the context. * @param relativeClass class whose package will be used as a prefix when * loading each specified resource name * @param resourceNames relatively-qualified names of resources to load */ public GenericXmlApplicationContext(Class<?> relativeClass, String... resourceNames) { load(relativeClass, resourceNames); refresh(); }
/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resource locations and automatically refreshing the context. * @param resourceLocations the resources to load from */ public GenericXmlApplicationContext(String... resourceLocations) { load(resourceLocations); refresh(); }
/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resource locations and automatically refreshing the context. * @param resourceLocations the resources to load from */ public GenericXmlApplicationContext(String... resourceLocations) { load(resourceLocations); refresh(); }
/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resource locations and automatically refreshing the context. * @param relativeClass class whose package will be used as a prefix when * loading each specified resource name * @param resourceNames relatively-qualified names of resources to load */ public GenericXmlApplicationContext(Class<?> relativeClass, String... resourceNames) { load(relativeClass, resourceNames); refresh(); }
/** * Create a new GenericXmlApplicationContext, loading bean definitions * from the given resources and automatically refreshing the context. * @param resources the resources to load from */ public GenericXmlApplicationContext(Resource... resources) { load(resources); refresh(); }
private static void executeSample1() { final Scanner scanner = new Scanner(System.in); final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load("classpath:META-INF/spring/integration/spring-integration-sample1-context.xml"); context.registerShutdownHook(); context.refresh(); final StringConversionService service = context.getBean(StringConversionService.class); final String message = "\n=========================================================" + "\n " + "\n Please press 'q + Enter' to quit the application. " + "\n " + "\n=========================================================" + "\n\n Please enter a string and press <enter>: "; System.out.print(message); while (!scanner.hasNext("q")) { String input = scanner.nextLine(); System.out.println("Converting String to Uppcase using Stored Procedure..."); String inputUpperCase = service.convertToUpperCase(input); System.out.println("Retrieving Numeric value via Sql Function..."); Integer number = service.getNumber(); System.out.println(String.format("Converted '%s' - End Result: '%s_%s'.", input, inputUpperCase, number)); System.out.print("To try again, please enter a string and press <enter>:"); } scanner.close(); context.close(); System.out.println("Back to main menu."); }
context.load("classpath:META-INF/spring/integration/spring-integration-sample2-context.xml"); context.registerShutdownHook(); context.refresh();
public static GenericXmlApplicationContext setupContext() { final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); if (System.getProperty(AVAILABLE_SERVER_SOCKET) == null) { System.out.print("Detect open server socket..."); int availableServerSocket = SocketUtils.findAvailableTcpPort(5678); final Map<String, Object> sockets = new HashMap<>(); sockets.put(AVAILABLE_SERVER_SOCKET, availableServerSocket); final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); } System.out.println("using port " + context.getEnvironment().getProperty(AVAILABLE_SERVER_SOCKET)); context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml"); context.registerShutdownHook(); context.refresh(); return context; }
/** * * @param path path to the file * @param targetClass the class who's classloader we will use to laod the context file * @param profile a profile name * @return the spring context */ public static AbstractApplicationContext loadProfileContext(String path, Class<?> targetClass, String profile) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.getEnvironment().setActiveProfiles(profile); ctx.setClassLoader(targetClass.getClassLoader()); ctx.load(path); ctx.refresh(); return ctx; }
@Test public void classRelativeResourceLoading_load() { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load(RELATIVE_CLASS, RESOURCE_NAME); ctx.refresh(); assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); }
@Test public void fullyQualifiedResourceLoading_load() { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load(FQ_RESOURCE_PATH); ctx.refresh(); assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); } }
@Test public void propertyPlaceholderEnvironmentProperties() throws Exception { MockEnvironment env = new MockEnvironment().withProperty("foo", "spam"); GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); applicationContext.setEnvironment(env); applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass())); applicationContext.refresh(); assertEquals("spam", applicationContext.getBean("string")); assertEquals("none", applicationContext.getBean("fallback")); }
@Test public void configuredThroughNamespace() { GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(new ClassPathResource("taskNamespaceTests.xml", getClass())); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor")); TestableAsyncUncaughtExceptionHandler exceptionHandler = context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); testBean.failWithVoid(); exceptionHandler.await(3000); Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); context.close(); }
@Test public void genericXmlApplicationContext() { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); assertHasStandardEnvironment(ctx); ctx.setEnvironment(prodEnv); ctx.load(XML_PATH); ctx.refresh(); assertHasEnvironment(ctx, prodEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodEnv); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@Test public void componentScanRespectsProfileAnnotation() { String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; { // should exclude the profile-annotated bean if active profiles remains unset GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); context.close(); } { // should include the profile-annotated bean with active profiles set GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } { // ensure the same works for AbstractRefreshableApplicationContext impls too ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation }, false); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } }
@Test public void methodExpressionHandlerInParentContextLoads() { parent = new GenericXmlApplicationContext( "org/springframework/security/config/method/sec2499/parent.xml"); child = new GenericXmlApplicationContext(); child.load("org/springframework/security/config/method/sec2499/child.xml"); child.setParent(parent); child.refresh(); } }
@Override protected void customizeContext(GenericApplicationContext context) { GenericXmlApplicationContext parent = new GenericXmlApplicationContext(); parent.setEnvironment(TestProfileEnvironment.getEnvironment()); parent.load(parentLocation); parent.refresh(); super.customizeContext(context); context.setParent(parent); } }
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.getEnvironment().setActiveProfiles( "dev" ); ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" ); ctx.refresh();