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(); } }
@Override public void start(Stage primaryStage) throws Exception { appContext = new AnnotationConfigApplicationContext(SmartCSV.class); String name = appContext.getEnvironment().getProperty("application.name"); String version = appContext.getEnvironment().getProperty("application.version"); Platform.setImplicitExit(false); AboutController aboutController = appContext.getBean(AboutController.class); aboutController.setHostServices(getHostServices()); try { showUI(primaryStage, name, version); } catch (Exception e) { e.printStackTrace(); } }
@Test public void importWithPlaceHolder() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); PropertySource<?> propertySource = new MapPropertySource( "test", Collections.<String, Object>singletonMap("test", "springframework")); ctx.getEnvironment().getPropertySources().addFirst(propertySource); ctx.register(ImportXmlConfig.class); ctx.refresh(); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); }
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment().setActiveProfiles("highschool"); ctx.register(KindergartenConfig.class, HighschoolConfig.class); ctx.refresh(); FoodProviderService foodProviderService = ctx.getBean("foodProviderService", FoodProviderService.class); List<Food> lunchSet = foodProviderService.provideLunchSet(); for (Food food : lunchSet) { System.out.println(" Food: " + food.getName()); } ctx.close(); }
@Bean public Environment getEnvironment() { Environment environment = context.getEnvironment(); // environment.` return environment; }