List of usage examples for org.springframework.context.support GenericXmlApplicationContext refresh
@Override public void refresh() throws BeansException, IllegalStateException
From source file:com.bank.config.xml.IntegrationTests.java
@Test public void transferTenDollars() throws InsufficientFundsException { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.getEnvironment().setActiveProfiles("dev"); ctx.load("classpath:/com/bank/config/xml/transfer-service-config.xml"); ctx.refresh(); TransferService transferService = ctx.getBean(TransferService.class); AccountRepository accountRepository = ctx.getBean(AccountRepository.class); assertThat(accountRepository.findById("A123").getBalance(), equalTo(100.00)); assertThat(accountRepository.findById("C456").getBalance(), equalTo(0.00)); transferService.transfer(10.00, "A123", "C456"); assertThat(accountRepository.findById("A123").getBalance(), equalTo(90.00)); assertThat(accountRepository.findById("C456").getBalance(), equalTo(10.00)); }
From source file:com.company.contactbook.dao.JdbcContactDaoTest.java
@Before public void setUp() throws Exception { GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles("jdbc"); context.load("classpath:/applicationContext.xml"); context.refresh(); contactDao = context.getBean(ContactDao.class); }
From source file:ReproTests.java
/** * Work around the lifecycle issues above by introducing a PropertySource containing * the value for the "resourceDirPlaceHolder" *//*from ww w . jav a2 s. c o m*/ @Test public void workaround() { Map<String, Object> localProps = new HashMap<String, Object>(); localProps.put("resourceDirPlaceHolder", "myResourceDir"); GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.getEnvironment().getPropertySources().addFirst(new MapPropertySource("localProps", localProps)); ctx.load("ReproTests-workaround.xml"); ctx.refresh(); }
From source file:org.cloudfoundry.reconfiguration.spring.AbstractCloudServiceBeanFactoryPostProcessorTest.java
protected final ApplicationContext getApplicationContext(String contextLocation, BeanFactoryPostProcessor beanFactoryPostProcessor) { GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); applicationContext.load(contextLocation); applicationContext.addBeanFactoryPostProcessor(beanFactoryPostProcessor); applicationContext.refresh(); return applicationContext; }
From source file:org.apache.cxf.fediz.service.idp.service.jpa.DBLoaderSpring.java
@Override public void load() { GenericXmlApplicationContext ctx = null; try {/* ww w . j av a 2s . c o m*/ if (resource == null) { LOG.warn("Resource null for DBLoaderSpring"); } ctx = new GenericXmlApplicationContext(); ctx.load(resource); ctx.refresh(); ctx.start(); Collection<EntitlementEntity> entitlements = ctx.getBeansOfType(EntitlementEntity.class, true, true) .values(); for (EntitlementEntity e : entitlements) { em.persist(e); } LOG.info(entitlements.size() + " EntitlementEntity added"); Collection<RoleEntity> roles = ctx.getBeansOfType(RoleEntity.class, true, true).values(); for (RoleEntity r : roles) { em.persist(r); } LOG.info(roles.size() + " RoleEntity added"); Collection<ClaimEntity> claims = ctx.getBeansOfType(ClaimEntity.class, true, true).values(); for (ClaimEntity c : claims) { em.persist(c); } LOG.info(claims.size() + " ClaimEntity added"); Collection<TrustedIdpEntity> trustedIdps = ctx.getBeansOfType(TrustedIdpEntity.class).values(); for (TrustedIdpEntity t : trustedIdps) { em.persist(t); } LOG.info(trustedIdps.size() + " TrustedIdpEntity added"); Collection<ApplicationEntity> applications = ctx.getBeansOfType(ApplicationEntity.class).values(); for (ApplicationEntity a : applications) { em.persist(a); } LOG.info(applications.size() + " ApplicationEntity added"); Collection<IdpEntity> idps = ctx.getBeansOfType(IdpEntity.class).values(); for (IdpEntity i : idps) { em.persist(i); } LOG.info(idps.size() + " IdpEntity added"); Collection<ApplicationClaimEntity> applicationClaims = ctx.getBeansOfType(ApplicationClaimEntity.class) .values(); for (ApplicationClaimEntity ac : applicationClaims) { em.persist(ac); } LOG.info(applicationClaims.size() + " ApplicationClaimEntity added"); em.flush(); } catch (Exception ex) { LOG.warn("Failed to initialize DB with data", ex); } finally { if (ctx != null) { ctx.close(); } } }
From source file:org.objectrepository.MessageConsumerDaemon.java
public void init() { log.info("Startup service..."); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false);//from ww w .jav a 2 s .com context.load("/META-INF/spring/application-context.xml", "META-INF/spring/dispatcher-servlet.xml"); context.refresh(); setContext(context); context.registerShutdownHook(); final RejectedExecutionHandler rejectedExecutionHandler = context.getBean(RejectedExecutionHandler.class); for (Queue taskExecutor : taskExecutors) { taskExecutor.setRejectedExecutionHandler(rejectedExecutionHandler); taskExecutor.initialize(); log.info("Initialized " + taskExecutor.getQueueName()); } }
From source file:it.tidalwave.northernwind.util.test.TestHelper.java
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull/*ww w. ja va 2 s .co m*/ private ApplicationContext createSpringContext(final @Nonnull Map<String, Object> properties, final @Nonnull Collection<String> configurationFiles) { configurationFiles.add(test.getClass().getSimpleName() + "/TestBeans.xml"); if (properties.isEmpty()) { return new ClassPathXmlApplicationContext(configurationFiles.toArray(new String[0])); } else { final StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addFirst(new MapPropertySource("test", properties)); final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setEnvironment(environment); context.load(configurationFiles.toArray(new String[0])); context.refresh(); return context; } }
From source file:com.frdna.config.context.spring.SpringContext.java
public SpringContext(Object environment, String name) { String fileName = name;/*from w ww.j a v a 2 s . co m*/ if (fileName == null) { fileName = "config.xml"; } GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(); Log.info(this, "Loading SpringContext from [classpath:%s]", fileName); applicationContext.load(new ClassPathResource(fileName)); if (environment != null) { Log.info(this, "Loading SpringContext from [classpath:%s/%s]", environment, fileName); ClassPathResource resource = new ClassPathResource(String.valueOf(environment) + "/" + fileName); if (resource.exists()) { applicationContext.load(resource); } else { Log.warn(this, "Unable to find environment resource [%s]", resource); } } applicationContext.refresh(); this.applicationContext = applicationContext; }
From source file:org.cloudfoundry.identity.varz.BootstrapTests.java
private GenericXmlApplicationContext getServletContext(String... resources) { String profiles = null;//from w ww . ja v a2 s .c om String[] resourcesToLoad = resources; if (!resources[0].endsWith(".xml")) { profiles = resources[0]; resourcesToLoad = new String[resources.length - 1]; System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length); } GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(resourcesToLoad); if (profiles != null) { context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles)); } // Simulate what happens in the webapp when the YamlServletProfileInitializer kicks in YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources( new Resource[] { new FileSystemResource("./src/test/resources/test/config/varz.yml") }); context.getEnvironment().getPropertySources() .addLast(new PropertiesPropertySource("servletProperties", factory.getObject())); context.refresh(); return context; }
From source file:org.cloudfoundry.identity.login.BootstrapTests.java
private GenericXmlApplicationContext getServletContext(String... resources) { String profiles = null;// w w w. ja v a 2 s . co m String[] resourcesToLoad = resources; if (!resources[0].endsWith(".xml")) { profiles = resources[0]; resourcesToLoad = new String[resources.length - 1]; System.arraycopy(resources, 1, resourcesToLoad, 0, resourcesToLoad.length); } GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(resourcesToLoad); if (profiles != null) { context.getEnvironment().setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles)); } // Simulate what happens in the webapp when the YamlServletProfileInitializer kicks in YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources( new Resource[] { new FileSystemResource("./src/test/resources/test/config/login.yml") }); context.getEnvironment().getPropertySources() .addLast(new PropertiesPropertySource("servletProperties", factory.getObject())); context.refresh(); return context; }