List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext register
public void register(Class<?>... annotatedClasses)
From source file:com.netflix.genie.web.security.oauth2.pingfederate.PingFederateConditionsUnitTests.java
private AnnotationConfigApplicationContext load(final Class<?> config, final String... env) { final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, env); context.register(config); context.refresh();//from ww w. j ava2s . c o m return context; }
From source file:org.exoplatform.container.spring.AnnotationConfigApplicationContextProvider.java
/** * {@inheritDoc}/*from ww w .ja va 2s.c om*/ */ public ApplicationContext getApplicationContext(ApplicationContext parent) { try { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setParent(parent); for (String value : params.getValues()) { Class<?> clazz = ClassLoading.forName(value, AnnotationConfigApplicationContextProvider.class); ctx.register(clazz); } ctx.refresh(); return ctx; } catch (Exception e) { throw new RuntimeException("Could not create the ApplicationContext", e); } }
From source file:com.arhs.spring.boot.autoconfigure.UnitTestBase.java
/** * Loads the configuration application context. * * @param configs The configuration class. * @param environment The environment.//from w w w.j av a 2s. com * @return A configuration application context. */ protected AnnotationConfigApplicationContext load(Class<?>[] configs, String... environment) { // Creates a instance of the "AnnotationConfigApplicationContext" class that represents // the application context. AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // Adds environment. EnvironmentTestUtils.addEnvironment(applicationContext, environment); // Registers the configuration class and auto-configuration classes. applicationContext.register(ConfigurationTest.class); applicationContext.register(configs); applicationContext.refresh(); return applicationContext; }
From source file:com.ryantenney.metrics.spring.EnableMetricsTest.java
@Test public void metricsConfigTest() throws Throwable { // Initialize the context AnnotationConfigApplicationContext applicationContext = null; try {/*from www .j ava 2s. c om*/ applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(MetricsConfig.class); applicationContext.refresh(); // Assert that the custom registries were used assertSame(metricRegistry, applicationContext.getBean(MetricRegistry.class)); assertSame(healthCheckRegistry, applicationContext.getBean(HealthCheckRegistry.class)); // Verify that the configureReporters method was invoked assertThat(MetricsConfig.isConfigureReportersInvoked, is(true)); // Assert that the bean has been proxied TestBean testBean = applicationContext.getBean(TestBean.class); assertNotNull(testBean); assertThat(AopUtils.isAopProxy(testBean), is(true)); // Verify that the Gauge field's value is returned Gauge<Integer> fieldGauge = (Gauge<Integer>) forGaugeField(metricRegistry, TestBean.class, "intGaugeField"); assertNotNull(fieldGauge); assertThat(fieldGauge.getValue(), is(5)); // Verify that the Gauge method's value is returned Gauge<Integer> methodGauge = (Gauge<Integer>) forGaugeMethod(metricRegistry, TestBean.class, "intGaugeMethod"); assertNotNull(methodGauge); assertThat(methodGauge.getValue(), is(6)); // Verify that the Timer's counter is incremented on method invocation Timer timedMethodTimer = forTimedMethod(metricRegistry, TestBean.class, "timedMethod"); assertNotNull(timedMethodTimer); assertThat(timedMethodTimer.getCount(), is(0L)); testBean.timedMethod(); assertThat(timedMethodTimer.getCount(), is(1L)); // Verify that the Meter's counter is incremented on method invocation Meter meteredMethodMeter = forMeteredMethod(metricRegistry, TestBean.class, "meteredMethod"); assertNotNull(meteredMethodMeter); assertThat(meteredMethodMeter.getCount(), is(0L)); testBean.meteredMethod(); assertThat(meteredMethodMeter.getCount(), is(1L)); // Verify that the Meter's counter is incremented on method invocation Meter exceptionMeteredMethodMeter = forExceptionMeteredMethod(metricRegistry, TestBean.class, "exceptionMeteredMethod"); assertNotNull(exceptionMeteredMethodMeter); assertThat(exceptionMeteredMethodMeter.getCount(), is(0L)); try { testBean.exceptionMeteredMethod(); } catch (Throwable t) { } assertThat(exceptionMeteredMethodMeter.getCount(), is(1L)); } finally { if (applicationContext != null) { applicationContext.close(); } } }
From source file:com.acme.ModuleConfigurationTest.java
@Test public void test() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); Properties properties = new Properties(); properties.put("prefix", "foo"); properties.put("suffix", "bar"); context.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("options", properties)); context.register(TestConfiguration.class); context.refresh();/* w w w . j av a2 s . com*/ MessageChannel input = context.getBean("input", MessageChannel.class); SubscribableChannel output = context.getBean("output", SubscribableChannel.class); final AtomicBoolean handled = new AtomicBoolean(); output.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { handled.set(true); assertEquals("foohellobar", message.getPayload()); } }); input.send(new GenericMessage<String>("hello")); assertTrue(handled.get()); }
From source file:io.milton.servlet.SpringMiltonFilter.java
@SuppressWarnings("resource") protected void initSpringApplicationContext(FilterConfig fc) { final WebApplicationContext rootContext = WebApplicationContextUtils .getWebApplicationContext(fc.getServletContext()); StaticApplicationContext parent;/*from w w w . ja v a 2 s .c o m*/ if (rootContext != null) { log.info("Found a root spring context, and using it"); parent = new StaticApplicationContext(rootContext); } else { log.info("No root spring context"); parent = new StaticApplicationContext(); } final FilterConfigWrapper configWrapper = new FilterConfigWrapper(fc); parent.getBeanFactory().registerSingleton("config", configWrapper); parent.getBeanFactory().registerSingleton("servletContext", fc.getServletContext()); File webRoot = new File(fc.getServletContext().getRealPath("/")); parent.getBeanFactory().registerSingleton("webRoot", webRoot); log.info("Registered root webapp path in: webroot=" + webRoot.getAbsolutePath()); parent.refresh(); final String configClass = fc.getInitParameter("contextConfigClass"); final String sFiles = fc.getInitParameter("contextConfigLocation"); ConfigurableApplicationContext ctx = null; if (StringUtils.isNotBlank(configClass)) { try { Class<?> clazz = Class.forName(configClass); final AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext(); annotationCtx.setParent(parent); annotationCtx.register(clazz); annotationCtx.refresh(); ctx = annotationCtx; } catch (ClassNotFoundException e) { ctx = null; log.error("Unable to create a child context for Milton", e); } } else { String[] contextFiles; if (sFiles != null && sFiles.trim().length() > 0) { contextFiles = sFiles.split(" "); } else { contextFiles = new String[] { "applicationContext.xml" }; } try { ctx = new ClassPathXmlApplicationContext(contextFiles, parent); } catch (BeansException e) { log.error("Unable to create a child context for Milton", e); } } if (ctx == null) { log.warn("No child context available, only using parent context"); context = parent; } else { context = ctx; } }
From source file:net.ggtools.maven.DDLGeneratorMojo.java
AnnotationConfigApplicationContext createApplicationContext() { final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); final ConfigurableEnvironment environment = applicationContext.getEnvironment(); final MutablePropertySources propertySources = environment.getPropertySources(); final MapPropertySource propertySource = createPropertySource(); propertySources.addFirst(propertySource); applicationContext.register(SpringConfiguration.class); applicationContext.refresh();// w w w . jav a 2s . c o m return applicationContext; }
From source file:com.cloudera.cli.validator.Main.java
/** * From the arguments, run the validation. * * @param args command line arguments/*from w w w . j a v a 2 s . c om*/ * @throws IOException anything goes wrong with streams. * @return exit code */ public int run(String[] args) throws IOException { Writer writer = new OutputStreamWriter(outStream, Constants.CHARSET_UTF_8); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); try { BeanDefinition cmdOptsbeanDefinition = BeanDefinitionBuilder .rootBeanDefinition(CommandLineOptions.class).addConstructorArgValue(appName) .addConstructorArgValue(args).getBeanDefinition(); ctx.registerBeanDefinition(CommandLineOptions.BEAN_NAME, cmdOptsbeanDefinition); ctx.register(ApplicationConfiguration.class); ctx.refresh(); CommandLineOptions cmdOptions = ctx.getBean(CommandLineOptions.BEAN_NAME, CommandLineOptions.class); CommandLineOptions.Mode mode = cmdOptions.getMode(); if (mode == null) { throw new ParseException("No valid command line arguments"); } ValidationRunner runner = ctx.getBean(mode.runnerName, ValidationRunner.class); boolean success = runner.run(cmdOptions.getCommandLineOptionActiveTarget(), writer); if (success) { writer.write("Validation succeeded.\n"); } return success ? 0 : -1; } catch (BeanCreationException e) { String cause = e.getMessage(); if (e.getCause() instanceof BeanInstantiationException) { BeanInstantiationException bie = (BeanInstantiationException) e.getCause(); cause = bie.getMessage(); if (bie.getCause() != null) { cause = bie.getCause().getMessage(); } } IOUtils.write(cause + "\n", errStream); CommandLineOptions.printUsageMessage(appName, errStream); return -2; } catch (ParseException e) { LOG.debug("Exception", e); IOUtils.write(e.getMessage() + "\n", errStream); CommandLineOptions.printUsageMessage(appName, errStream); return -2; } finally { if (ctx != null) { ctx.close(); } writer.close(); } }
From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java
@Test public void withUnresolvablePlaceholderAndDefault() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithUnresolvablePlaceholderAndDefault.class); ctx.refresh();//from w ww. j av a2 s . c o m assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); ctx.close(); }
From source file:pl.bristleback.server.bristle.conf.runner.ServerInstanceResolver.java
public BristlebackServerInstance resolverServerInstance() { InitialConfiguration initialConfiguration = initialConfigurationResolver.resolveConfiguration(); startLogger(initialConfiguration);//from ww w.j av a 2 s. c o m AnnotationConfigApplicationContext frameworkContext = new AnnotationConfigApplicationContext(); BristleSpringIntegration springIntegration = new BristleSpringIntegration(actualApplicationContext, frameworkContext); BristlebackBeanFactoryPostProcessor bristlebackPostProcessor = new BristlebackBeanFactoryPostProcessor( initialConfiguration, springIntegration); frameworkContext.addBeanFactoryPostProcessor(bristlebackPostProcessor); frameworkContext.register(SpringConfigurationResolver.class); frameworkContext.scan(InitialConfiguration.SYSTEM_BASE_PACKAGES); frameworkContext.refresh(); BristlebackConfig configuration = frameworkContext.getBean("bristlebackConfigurationFinal", BristlebackConfig.class); return new BristlebackServerInstance(configuration); }