# 4.2 빈 정의에 있는 표현식 by sh

## 4.2. 빈 정의에 있는 표현식

**`BeanDefinition`인스턴스를 정의하기 위해 XML 기반 또는 Annotation 기반 구성 메타 데이터와 함께 SpEL 표현식을 사용할 수 있습니다. 두 경우 모두 표현식을 정의하는 구문은 `#{ <expression string> }` 형식입니다.**&#x20;

### **4.2.1. XML 구성**

다음 예제와 같이 표현식을 사용하여 속성 또는 생성 자 인수 값을 설정할 수 있습니다.

```markup
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

    <!-- other properties -->
</bean>
```

`systemProperties`변수는 미리 정의되어 있으므로 다음 예제와 같이 표현식에 사용할 수 있습니다.

```markup
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
    <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>

    <!-- other properties -->
</bean>
```

이 컨텍스트에서 미리 정의된 변수 앞에 `#` 기호를 붙일 필요는 없습니다.

다음 예제와 같이 다른 빈 속성을 이름으로 참조할 수도 있습니다.

```markup
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

    <!-- other properties -->
</bean>

<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
    <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>

    <!-- other properties -->
</bean>
```

### **4.2.2. Annotation 구성**

기본 값을 지정하기 위해 `@Value` Annotation을 필드, 메서드 및 생성 자 매개 변수에 배치할 수도 있습니다.

다음 예제에서는 필드 변수의 기본 값을 설정합니다.

```java
public static class FieldValueTestBean

    @Value("#{ systemProperties['user.region'] }")
    private String defaultLocale;

    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    public String getDefaultLocale() {
        return this.defaultLocale;
    }

}
```

다음 예제는 동등하지만 속성 설정 메서드에 대한 예제입니다.

```java
public static class PropertyValueTestBean

    private String defaultLocale;

    @Value("#{ systemProperties['user.region'] }")
    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    public String getDefaultLocale() {
        return this.defaultLocale;
    }

}
```

Autowired 메서드와 생성 자는 다음 예제와 같이 `@Value` Annotation을 사용할 수도 있습니다.&#x20;

```java
public class SimpleMovieLister {

    private MovieFinder movieFinder;
    private String defaultLocale;

    @Autowired
    public void configure(MovieFinder movieFinder,
            @Value("#{ systemProperties['user.region'] }") String defaultLocale) {
        this.movieFinder = movieFinder;
        this.defaultLocale = defaultLocale;
    }

    // ...
}
```

```java
public class MovieRecommender {

    private String defaultLocale;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
            @Value("#{systemProperties['user.country']}") String defaultLocale) {
        this.customerPreferenceDao = customerPreferenceDao;
        this.defaultLocale = defaultLocale;
    }

    // ...
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wannaqueen.gitbook.io/spring5/spring-5.0/4-spel/4.2-by-sh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
