2015년 12월 10일 목요일

Environment 객체를 이용한 스프링 빈 설정






  1. Object object;  
  2. ConfigurableApplicationContext ctx = new GenericXmlApplicationContext();  
  3. ConfigurableEnvironment env = ctx.getEnvironment();  
  4.   
  5. MutablePropertySources propertySources = env.getPropertySources();  
  6. try {  
  7.     propertySources.addLast(new ResourcePropertySource("classpath:app.properties"));  
  8.       
  9.     System.out.println(env.getProperty("property1"));  
  10.     System.out.println(env.getProperty("property2"));  
  11. catch (IOException e) {}  
  12. GenericXmlApplicationContext gCtx = (GenericXmlApplicationContext) ctx;  
  13. gCtx.load("clathpath:appContext.xml");  
  14. gCtx.refresh();  
  15.   
  16. object = gCtx.getBean("beanID", Object.class);  
  17. // ConfigurableApplicationContext 즉, ctx에는 propertySouce로 app.properties 
  18. // 파일의 내용이 들어가 있다.  
  19. // 이 내용과 더불어 GenericXmlApplicationContext 즉, gCtx에서 appContext.xml 리소스를
  20. // 함께 사용하고 있다.  
  21. // 이 경우 ctx.getBean()으로 부터 주입된 객체는 app.properties의 내용과 appContext.xml
  22. // 모두의 설정을  사용할 수있다.  

  23. /* do something */  
  24.   
  25. ctx.close();  



댓글 없음:

댓글 쓰기