Loading...
Spring Framework Reference Documentation 7.0.2의 Expressions in Bean Definitions의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
설정 메타데이터와 함께 SpEL expression을 사용하여 bean 인스턴스를 정의할 수 있습니다. 두 경우 모두 expression을 정의하기 위한 구문은 #{ <expression string> } 형태입니다.
애플리케이션 컨텍스트의 모든 bean은 공통 bean name을 가진 미리 정의된 변수로 사용 가능합니다. 여기에는 environment(org.springframework.core.env.Environment 타입)와 같은 표준 컨텍스트 bean뿐만 아니라 런타임 환경에 접근하기 위한 systemProperties 및 systemEnvironment(Map<String, Object> 타입)도 포함됩니다.
기본값을 지정하기 위해 @Value 어노테이션을 필드, 메서드, 그리고 메서드 또는 생성자 매개변수(또는 XML에 상응하는 요소)에 지정할 수 있습니다.
다음 예제는 필드의 기본값을 설정합니다:
1public class FieldValueTestBean { 2 3 @Value("#{ systemProperties['user.region'] }") 4 private String defaultLocale; 5 6 public void setDefaultLocale(String defaultLocale) { 7 this.defaultLocale = defaultLocale; 8 } 9 10 public String getDefaultLocale() { 11 return this.defaultLocale; 12 } 13}
1class FieldValueTestBean { 2 3 @field:Value("#{ systemProperties['user.region'] }") 4 lateinit var defaultLocale: String 5}
여기에서는 미리 정의된 변수 앞에 # 기호를 접두사로 붙일 필요가 없다는 점에 유의하세요.
다음 예제는 프로퍼티 setter 메서드에서의 동등한 예제를 보여줍니다:
1public class PropertyValueTestBean { 2 3 private String defaultLocale; 4 5 @Value("#{ systemProperties['user.region'] }") 6 public void setDefaultLocale(String defaultLocale) { 7 this.defaultLocale = defaultLocale; 8 } 9 10 public String getDefaultLocale() { 11 return this.defaultLocale; 12 } 13}
1class PropertyValueTestBean { 2 3 @set:Value("#{ systemProperties['user.region'] }") 4 lateinit var defaultLocale: String 5}
1<bean id="testBean" class="org.springframework.docs.core.expressions.expressionsbeandef.PropertyValueTestBean"> 2 <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/> 3</bean>
Autowired 메서드와 생성자도 아래 예제에서 보듯이 @Value 어노테이션을 사용할 수 있습니다:
1public class SimpleMovieLister { 2 3 private MovieFinder movieFinder; 4 private String defaultLocale; 5 6 @Autowired 7 public void configure(MovieFinder movieFinder, 8 @Value("#{ systemProperties['user.region'] }") String defaultLocale) { 9 this.movieFinder = movieFinder; 10 this.defaultLocale = defaultLocale; 11 } 12 13 // ... 14}
1class SimpleMovieLister { 2 3 private lateinit var movieFinder: MovieFinder 4 private lateinit var defaultLocale: String 5 6 @Autowired 7 fun configure( 8 movieFinder: MovieFinder, 9 @Value("#{ systemProperties['user.region'] }") defaultLocale: String 10 ) { 11 this.movieFinder = movieFinder 12 this.defaultLocale = defaultLocale 13 } 14 15 // ... 16}
1public class MovieRecommender { 2 3 private String defaultLocale; 4 5 private CustomerPreferenceDao customerPreferenceDao; 6 7 public MovieRecommender(CustomerPreferenceDao customerPreferenceDao, 8 @Value("#{systemProperties['user.country']}") String defaultLocale) { 9 this.customerPreferenceDao = customerPreferenceDao; 10 this.defaultLocale = defaultLocale; 11 } 12 13 // ... 14}
1class MovieRecommender( 2 private val customerPreferenceDao: CustomerPreferenceDao, 3 @Value("#{systemProperties['user.country']}") 4 private val defaultLocale: String 5) { 6 // ... 7}
1<bean id="testBean" class="org.springframework.docs.core.expressions.expressionsbeandef.MovieRecommender"> 2 <constructor-arg ref="customerPreferenceDao"/> 3 <constructor-arg value="#{ systemProperties['user.country'] }"/> 4</bean>
다음 예제에서 보듯이, 다른 bean 프로퍼티를 이름으로 참조할 수도 있습니다:
1public class ShapeGuess { 2 3 private double initialShapeSeed; 4 5 @Value("#{ numberGuess.randomNumber }") 6 public void setInitialShapeSeed(double initialShapeSeed) { 7 this.initialShapeSeed = initialShapeSeed; 8 } 9 10 public double getInitialShapeSeed() { 11 return initialShapeSeed; 12 } 13}
1class ShapeGuess { 2 3 @set:Value("#{ numberGuess.randomNumber }") 4 var initialShapeSeed: Double = 0.0 5}
1<bean id="shapeGuess" class="org.springframework.docs.core.expressions.expressionsbeandef.ShapeGuess"> 2 <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/> 3</bean>
Evaluation
Language Reference