사용자 도구

사이트 도구


springframework:propertysource

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:propertysource [2013/06/24 23:47]
kwon37xi [@Configuration 클래스에서 XML 프라퍼티를 프라퍼티 소스로 등록하기 예제]
springframework:propertysource [2017/08/11 09:04] (현재)
kwon37xi
줄 4: 줄 4:
   * 시스템 프라퍼티, 환경변수, JNDI 등을 모두 하나의 공간에 넣고 그 값을 읽고 설정할 수 있게 해준다.   * 시스템 프라퍼티, 환경변수, JNDI 등을 모두 하나의 공간에 넣고 그 값을 읽고 설정할 수 있게 해준다.
   * [[http://scottfrederick.blogspot.kr/2012/03/custom-propertysource-in-spring-31.html|Custom PropertySource in Spring 3.1]] : 프라퍼티 소스에 Redis의 데이터를 추가하는 예제   * [[http://scottfrederick.blogspot.kr/2012/03/custom-propertysource-in-spring-31.html|Custom PropertySource in Spring 3.1]] : 프라퍼티 소스에 Redis의 데이터를 추가하는 예제
 +  * [[http://blog.codeleak.pl/2015/09/placeholders-support-in-value.html|${... } placeholders support in @Value annotations in Spring]]
 ===== 프라퍼티 추가 ===== ===== 프라퍼티 추가 =====
   * 일반 프라퍼티 파일의 경우   * 일반 프라퍼티 파일의 경우
     * Java 클래스 설정에서는 [[http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/PropertySource.html|@PropertySource]]로 프라퍼티 파일을 지정하거나,     * Java 클래스 설정에서는 [[http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/PropertySource.html|@PropertySource]]로 프라퍼티 파일을 지정하거나,
     * <del>[[http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-context-pphc|<context:property-placeholder> tag]]를 사용해 넣을 수 있다.</del> - 이거 안 된다. 하단 참조.     * <del>[[http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-context-pphc|<context:property-placeholder> tag]]를 사용해 넣을 수 있다.</del> - 이거 안 된다. 하단 참조.
 +
 ===== 코딩을 통한 임의의 프라퍼티 소스 추가 ===== ===== 코딩을 통한 임의의 프라퍼티 소스 추가 =====
   * ''MyPropertySource''가 [[http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/core/env/PropertySource.html|PropertySource]] 구현체 일 때    * ''MyPropertySource''가 [[http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/core/env/PropertySource.html|PropertySource]] 구현체 일 때 
줄 24: 줄 25:
     <param-value>com.bank.MyInitializer</param-value>     <param-value>com.bank.MyInitializer</param-value>
 </context-param> </context-param>
 +</code>
 +  * TestContext에서 ''web.xml''없이 Java Code로 등록하기<code java>
 +@ContextConfiguration(initializers = MyInitializer.class)
 </code> </code>
   * ''MyInitializer.java''<code java>   * ''MyInitializer.java''<code java>
줄 33: 줄 37:
     }     }
 } }
 +</code>
 +
 +==== ApplicationContextInitializer를 Java 코드 기반 ApplicationContext에서 사용하기 ====
 +  * [[http://stackoverflow.com/questions/13288841/applicationcontextinitializer-in-a-non-web-spring-context|java - ApplicationContextInitializer in a non-web Spring Context?]]<code java>
 +// Create context, but dont initialize with configuration by calling 
 +// the empty constructor. Instead, initialize it with the Context Initializer.
 +AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 +MyAppContextInitializer initializer = new MyAppContextInitializer();
 +
 +// ApplicationContextInitializer가 ApplicationContext를 초기화해주는 형태
 +initializer.initialize( ctx );
 +
 +// @Configuration 클래스를 등록하고 초기화
 +ctx.register( com.my.classpath.StackOverflowConfiguration.class );
 +ctx.refresh()
 </code> </code>
  
줄 111: 줄 130:
   -->   -->
 </code> </code>
 +
 +===== ResourceLoader =====
 +  * ''@Autowired ResourceLoader resourceLoader'' 로 현재 애플리케이션 컨텍스트의 [[http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/ResourceLoader.html|ResourceLoader]]를 획득하여 사용할 수 있다.
 +  * 단, ''classpath:/package/to/**/*.xml'' 처럼 Pattern화된 Resource를 로딩할 때는 [[http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/support/ResourcePatternResolver.html|ResourcePatternResolver]]를 주입받아서 [[http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/support/ResourcePatternResolver.html#getResources-java.lang.String-|ResourcePatternResolver#getResources()]] 메소드로 패턴을 해석한 결과를 획득해야 한다.
 +  * 그냥 속 편하게 [[http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html|ApplicationContext]]를 주입 받으면 이것 자체가 ''ResourcePatternResolver''를 구현하므로 이를 사용하도 된다.
 +
 +===== @Value =====
 +==== @Value로 Date 객체 주입하기 ====
 +  * [[http://stackoverflow.com/questions/18066482/inject-date-using-value-annotation|spring - Inject date using @Value annotation - Stack Overflow]]<code java>
 +@Value("#{new java.text.SimpleDateFormat(\"yyyyMMdd\").parse(\"${PROP_DATE}\")}")
 +private Date date;
 +</code>
 +  * [[http://www.baeldung.com/spring-value-defaults|Using Spring @Value with Defaults]]
springframework/propertysource.1372085238.txt.gz · 마지막으로 수정됨: 2013/06/24 23:47 저자 kwon37xi