@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(WebConfig.class); wac.refresh(); return wac; }
public static AnnotationConfigApplicationContext inititializeApplicationContext() { final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PersistenceJPAConfig.class); ctx.refresh(); return ctx; }
public static void main(String[] args) { try { LOGGER.info("Starting application"); String resourceName = "spring.profiles.active"; ClassLoader loader = Thread.currentThread().getContextClassLoader(); Properties props = new Properties(); try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) { props.load(resourceStream); } System.setProperty("spring.profiles.active", props.getProperty("spring.profiles.active")); context = new AnnotationConfigApplicationContext(); context.register(ApplicationConfiguration.class); context.refresh(); CategoryDAO dao = context.getBean(CategoryDAO.class); dao.getAllCategories(); ProducerDAO prdao = context.getBean(ProducerDAO.class); prdao.getAllProducers(); ProductDAO pdao = context.getBean(ProductDAO.class); pdao.getAllProducts(); MainView view = context.getBean(MainView.class); view.show(); } catch (Exception e) { LOGGER.error(" ERROR " + e.getMessage()); } }
@Test public void testEndpointMBeanExporterInParentChild() throws IntrospectionException, InstanceNotFoundException, MalformedObjectNameException, ReflectionException { this.context = new AnnotationConfigApplicationContext(); this.context.register( JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); parent.register( JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); this.context.setParent(parent); parent.refresh(); this.context.refresh(); parent.close(); System.out.println("parent " + ObjectUtils.getIdentityHexString(parent)); System.out.println("child " + ObjectUtils.getIdentityHexString(this.context)); }
@Override public void configure(AtmosphereConfig config) { try { String s = config.getInitParameter(ATMOSPHERE_SPRING_EXCLUDE_CLASSES); if (s != null) { String[] list = s.split(","); for (String clazz : list) { excludedFromInjection.add(IOUtils.loadClass(getClass(), clazz)); } if (list.length > 0) { preventSpringInjection = true; } } context = new AnnotationConfigApplicationContext(); context.setParent( WebApplicationContextUtils.getWebApplicationContext( config.framework().getServletContext())); context.refresh(); // Hack to make it injectable context.register(AtmosphereConfig.class); ((AtmosphereConfig) context.getBean(AtmosphereConfig.class.getCanonicalName(), config.framework())) .populate(config); } catch (Exception ex) { logger.warn("Unable to configure injection", ex); } }
private AnnotationConfigApplicationContext load(Class<?> config, String... env) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, env); context.register(config); context.refresh(); return context; }
@Test public void basicBinding() { EnvironmentTestUtils.addEnvironment(context, "eureka.instance.appGroupName=mygroup"); context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class); context.refresh(); assertEquals("mygroup", context.getBean(EurekaInstanceConfigBean.class).getAppGroupName()); }
public void performAction() { /* Choose appropriate Spring profile */ final String profileName = action.getSpringProfileName(); /* Let action do any pre-setup work */ action.beforeApplicationContextInit(); /* Initialise ApplicationConetext */ logger.debug("Setting up Spring ApplicationContext using profile '{}'", profileName); final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.getEnvironment().setActiveProfiles(profileName); QtiWorksApplicationContextHelper.registerConfigPropertySources( applicationContext, deploymentPropertiesResource); applicationContext.register( PropertiesConfiguration.class, JpaProductionConfiguration.class, JpaBootstrapConfiguration.class, ServicesConfiguration.class, ManagerConfiguration.class); applicationContext.refresh(); /* Now let action class do its work*/ try { action.run(applicationContext, actionParameters); } catch (final Exception e) { logger.warn("Unexpected Exception performing action", e); } finally { applicationContext.close(); } }
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.vamsi"); ctx.refresh(); for (String beanname : ctx.getBeanDefinitionNames()) { System.out.println(beanname); } HollywoodServiceJSR330 hollywoodservice = ctx.getBean("hollywoodServiceJSR330", HollywoodServiceJSR330.class); hollywoodservice.getFriendlyAgents(); Box<Integer> abox = ctx.getBean("intBox", Box.class); abox.setA(100); System.out.println("first bean " + abox.getA()); Box<Integer> bbox = ctx.getBean("intBox", Box.class); System.out.println("second bean " + abox.getA()); ctx.close(); }
@Test public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig() throws IOException { String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment( applicationContext, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config=" + cacheConfig, "spring.hazelcast.config=" + mainConfig); applicationContext.register(DefaultCacheConfiguration.class); applicationContext.register(HazelcastAndCacheConfiguration.class); applicationContext.refresh(); this.context = applicationContext; HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class); HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class); HazelcastInstance cacheHazelcastInstance = getHazelcastInstance(cacheManager); assertThat(cacheHazelcastInstance, not(hazelcastInstance)); // Our custom assertThat( hazelcastInstance.getConfig().getConfigurationFile(), equalTo(new ClassPathResource(mainConfig).getFile())); assertThat( cacheHazelcastInstance.getConfig().getConfigurationFile(), equalTo(new ClassPathResource(cacheConfig).getFile())); }
public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); annotationConfigApplicationContext.register(CustomizeScanTest.class); annotationConfigApplicationContext.refresh(); ScanClass1 injectClass = annotationConfigApplicationContext.getBean(ScanClass1.class); injectClass.print(); }
@Test public void annotatedService_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); assertTrue("expected a jdk proxy", AopUtils.isJdkDynamicProxy(s)); assertThat(s, not(instanceOf(NonAnnotatedServiceImpl.class))); }
@Test public void annotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(NonAnnotatedServiceImpl.class)); }
@Test public void orderAttributeIsPropagated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(OrderedAsyncConfig.class); ctx.refresh(); AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE)); }
@Override protected ConfigurableApplicationContext getApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getBeanFactory().registerSingleton("cachingProvider", getCachingProvider()); context.register(EnableCachingConfig.class); context.refresh(); jCacheManager = context.getBean("jCacheManager", CacheManager.class); return context; }
private void load(Class<?> config, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(applicationContext, environment); applicationContext.register(config); applicationContext.register(CacheAutoConfiguration.class); applicationContext.refresh(); this.context = applicationContext; }
public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); annotationConfigApplicationContext.register(ApplicationListenerTest.class); annotationConfigApplicationContext.refresh(); PrintSomething printSomething = annotationConfigApplicationContext.getBean(PrintSomething.class); printSomething.print(); }
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(configs); applicationContext.register(RabbitAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(applicationContext, environment); applicationContext.refresh(); return applicationContext; }
@Test public void asyncProcessorIsOrderedLowestPrecedenceByDefault() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfig.class); ctx.refresh(); AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE)); }
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ApplicationConfig.class); ctx.refresh(); System.out.println("version: " + SpringVersion.getVersion()); SpringUI ui = ctx.getBean(SpringUI.class); ui.init(); }
@Test public void skipsFilterIfPropertyDisabled() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.enabled:false"); context.register(Config.class, MetricFilterAutoConfiguration.class); context.refresh(); assertThat(context.getBeansOfType(Filter.class).size()).isEqualTo(0); context.close(); }
public void testWithConfigurationClass() throws Exception { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); parent.register(TransactionManagerConfiguration.class); parent.refresh(); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent); doTestWithMultipleTransactionManagers(context); }
@Test public void strictTraceId_defaultsToTrue() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.storage.type:mysql"); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class); context.refresh(); assertThat(context.getBean(MySQLStorage.class).strictTraceId).isTrue(); }
@Test public void transactionProxyIsCreated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableTxConfig.class, TxManagerConfig.class); ctx.refresh(); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); assertThat("testBean is not a proxy", AopUtils.isAopProxy(bean), is(true)); Map<?, ?> services = ctx.getBeansWithAnnotation(Service.class); assertThat("Stereotype annotation not visible", services.containsKey("testBean"), is(true)); }
@Test public void txManagerIsResolvedOnInvocationOfTransactionalMethod() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableTxConfig.class, TxManagerConfig.class); ctx.refresh(); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); }
@Test public void providesStorageComponent_whenStorageTypeMySQL() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.storage.type:mysql"); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class); context.refresh(); assertThat(context.getBean(MySQLStorage.class)).isNotNull(); }
@Test public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableTxConfig.class, MultiTxManagerConfig.class); ctx.refresh(); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); }
@Test public void proxyingOccurs() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfig.class); ctx.refresh(); AsyncBean asyncBean = ctx.getBean(AsyncBean.class); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); }
@Test public void canOverridesProperty_username() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.storage.type:mysql", "zipkin.storage.mysql.username:robot"); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class); context.refresh(); assertThat(context.getBean(ZipkinMySQLStorageProperties.class).getUsername()) .isEqualTo("robot"); }
@Test public void doesntProvidesStorageComponent_whenStorageTypeNotMySQL() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.storage.type:cassandra"); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinMySQLStorageAutoConfiguration.class); context.refresh(); thrown.expect(NoSuchBeanDefinitionException.class); context.getBean(MySQLStorage.class); }