Loading...
Spring Framework Reference Documentation 7.0.2의 XML Schemas의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
이 부록의 이 부분에서는 core container와 관련된 XML schema를 나열합니다.
util Schema이름에서 알 수 있듯이, util 태그는 collection 설정, constant 참조 등과 같은 일반적인 utility 설정 이슈를 다룹니다.
util schema의 태그를 사용하려면, Spring XML configuration 파일 상단에 다음과 같은 preamble이 필요합니다
(snippet의 텍스트는 util namespace의 태그를 사용할 수 있도록 올바른 schema를 참조합니다):
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:util="http://www.springframework.org/schema/util" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> 8 9 <!-- bean definitions here --> 10 11</beans>
<util:constant/>다음 bean 정의를 고려해 보십시오:
1<bean id="..." class="..."> 2 <property name="isolation"> 3 <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" 4 class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> 5 </property> 6</bean>
앞의 configuration은 Spring FactoryBean 구현(FieldRetrievingFactoryBean)을 사용하여
bean의 isolation property 값을 java.sql.Connection.TRANSACTION_SERIALIZABLE constant의 값으로 설정합니다.
이 설정은 충분히 동작하지만 장황하고, Spring의 내부 plumbing을 최종 사용자에게 (불필요하게) 노출합니다.
다음 XML Schema 기반 버전은 더 간결하고, 개발자의 의도(“이 constant 값을 inject하라”)를 명확하게 표현하며, 더 읽기 쉽습니다:
1<bean id="..." class="..."> 2 <property name="isolation"> 3 <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> 4 </property> 5</bean>
FieldRetrievingFactoryBean
은 static 또는 non-static field 값을 조회하는 FactoryBean입니다.
일반적으로 public``static``final constant를 조회하는 데 사용되며,
그 값은 다른 bean의 property 값이나 constructor argument를 설정하는 데 사용될 수 있습니다.
다음 예제는
staticField
property를 사용하여 static field를 노출하는 방법을 보여줍니다:
1<bean id="myField" 2 class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> 3 <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> 4</bean>
다음 예제에서 보듯이, static field를 bean 이름으로 지정하는 편의 사용 형태도 있습니다:
1<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" 2 class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
이는 더 이상 bean id를 무엇으로 할지 선택할 수 없음을 의미합니다
(따라서 이를 참조하는 다른 bean도 이 더 긴 이름을 사용해야 합니다).
그러나 이 형태는 정의가 매우 간결하고, inner bean으로 사용할 때 매우 편리합니다.
왜냐하면 bean reference에 대해 id를 지정할 필요가 없기 때문입니다. 다음 예제를 보십시오:
1<bean id="..." class="..."> 2 <property name="isolation"> 3 <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" 4 class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> 5 </property> 6</bean>
또한,
FieldRetrievingFactoryBean
class에 대한 API documentation에 설명된 대로, 다른 bean의 non-static (instance) field에도 접근할 수 있습니다.
enumeration 값을 property나 constructor argument로 bean에 inject하는 것은
Spring에서 쉽게 할 수 있습니다. 실제로 Spring 내부 구조(또는 FieldRetrievingFactoryBean과 같은 class)에 대해
아무것도 할 필요도, 알 필요도 없습니다.
다음 예제 enumeration은 enum 값을 inject하는 것이 얼마나 쉬운지를 보여줍니다:
1package jakarta.persistence; 2 3public enum PersistenceContextType { 4 5 TRANSACTION, 6 EXTENDED 7}
1package jakarta.persistence 2 3enum class PersistenceContextType { 4 5 TRANSACTION, 6 EXTENDED 7}
이제 PersistenceContextType 타입의 다음 setter와 이에 대응하는 bean 정의를 고려해 보십시오:
1package example; 2 3public class Client { 4 5 private PersistenceContextType persistenceContextType; 6 7 public void setPersistenceContextType(PersistenceContextType type) { 8 this.persistenceContextType = type; 9 } 10}
1package example 2 3class Client { 4 5 lateinit var persistenceContextType: PersistenceContextType 6}
1<bean class="example.Client"> 2 <property name="persistenceContextType" value="TRANSACTION"/> 3</bean>
<util:property-path/>다음 예제를 고려해 보십시오:
1<!-- target bean to be referenced by name --> 2<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype"> 3 <property name="age" value="10"/> 4 <property name="spouse"> 5 <bean class="org.springframework.beans.TestBean"> 6 <property name="age" value="11"/> 7 </bean> 8 </property> 9</bean> 10 11<!-- results in 10, which is the value of property 'age' of bean 'testBean' --> 12<bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
앞의 configuration은 Spring FactoryBean 구현(PropertyPathFactoryBean)을 사용하여
int 타입의 testBean.age라는 bean을 생성하고,
그 값이 testBean bean의 age property 값과 같도록 합니다.
이제 <util:property-path/> element를 추가한 다음 예제를 고려해 보십시오:
1<!-- target bean to be referenced by name --> 2<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype"> 3 <property name="age" value="10"/> 4 <property name="spouse"> 5 <bean class="org.springframework.beans.TestBean"> 6 <property name="age" value="11"/> 7 </bean> 8 </property> 9</bean> 10 11<!-- results in 10, which is the value of property 'age' of bean 'testBean' --> 12<util:property-path id="name" path="testBean.age"/>
<property-path/> element의 path attribute 값은 beanName.beanProperty 형태를 따릅니다.
이 경우, testBean이라는 이름의 bean의 age property를 가져옵니다.
그 age property의 값은 10입니다.
<util:property-path/> to Set a Bean Property or Constructor ArgumentPropertyPathFactoryBean은 주어진 target object에 대해 property path를 평가하는 FactoryBean입니다.
target object는 직접 지정하거나 bean 이름으로 지정할 수 있습니다.
그런 다음 이 값을 다른 bean 정의에서 property 값이나 constructor argument로 사용할 수 있습니다.
다음 예제는 이름으로 다른 bean에 대해 path를 사용하는 방법을 보여줍니다:
1<!-- target bean to be referenced by name --> 2<bean id="person" class="org.springframework.beans.TestBean" scope="prototype"> 3 <property name="age" value="10"/> 4 <property name="spouse"> 5 <bean class="org.springframework.beans.TestBean"> 6 <property name="age" value="11"/> 7 </bean> 8 </property> 9</bean> 10 11<!-- results in 11, which is the value of property 'spouse.age' of bean 'person' --> 12<bean id="theAge" 13 class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> 14 <property name="targetBeanName" value="person"/> 15 <property name="propertyPath" value="spouse.age"/> 16</bean>
다음 예제에서는 inner bean에 대해 path가 평가됩니다:
1<!-- results in 12, which is the value of property 'age' of the inner bean --> 2<bean id="theAge" 3 class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> 4 <property name="targetObject"> 5 <bean class="org.springframework.beans.TestBean"> 6 <property name="age" value="12"/> 7 </bean> 8 </property> 9 <property name="propertyPath" value="age"/> 10</bean>
bean 이름이 property path인 shortcut 형태도 있습니다. 다음 예제는 shortcut 형태를 보여줍니다:
1<!-- results in 10, which is the value of property 'age' of bean 'person' --> 2<bean id="person.age" 3 class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
이 형태는 bean 이름을 선택할 수 없음을 의미합니다.
이에 대한 모든 reference도 path인 동일한 id를 사용해야 합니다.
inner bean으로 사용하는 경우에는, 다음 예제에서 보듯이 전혀 참조할 필요가 없습니다:
1<bean id="..." class="..."> 2 <property name="age"> 3 <bean id="person.age" 4 class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> 5 </property> 6</bean>
실제 정의에서 result type을 명시적으로 설정할 수도 있습니다. 대부분의 use case에서는 필요하지 않지만, 때때로 유용할 수 있습니다. 이 기능에 대한 자세한 내용은 javadoc을 참조하십시오.
<util:properties/>다음 예제를 고려해 보십시오:
1<!-- creates a java.util.Properties instance with values loaded from the supplied location --> 2<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 3 <property name="location" value="classpath:com/foo/jdbc-production.properties"/> 4</bean>
앞의 configuration은 Spring FactoryBean 구현(PropertiesFactoryBean)을 사용하여
제공된 Resource location에서
값을 로드한 java.util.Properties instance를 생성합니다.
다음 예제는 더 간결한 표현을 위해 util:properties element를 사용합니다:
1<!-- creates a java.util.Properties instance with values loaded from the supplied location --> 2<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>
<util:list/>다음 예제를 고려해 보십시오:
1<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' --> 2<bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean"> 3 <property name="sourceList"> 4 <list> 5 <value>[email protected]</value> 6 <value>[email protected]</value> 7 <value>[email protected]</value> 8 <value>[email protected]</value> 9 </list> 10 </property> 11</bean>
앞의 configuration은 Spring FactoryBean 구현(ListFactoryBean)을 사용하여
제공된 sourceList에서 값을 가져와 초기화된 java.util.List instance를 생성합니다.
다음 예제는 더 간결한 표현을 위해 <util:list/> element를 사용합니다:
1<!-- creates a java.util.List instance with the supplied values --> 2<util:list id="emails"> 3 <value>[email protected]</value> 4 <value>[email protected]</value> 5 <value>[email protected]</value> 6 <value>[email protected]</value> 7</util:list>
또한 <util:list/> element의 list-class attribute를 사용하여
instantiated되고 populated되는 List의 정확한 타입을 명시적으로 제어할 수 있습니다.
예를 들어, 실제로 java.util.LinkedList를 instantiation해야 한다면 다음 configuration을 사용할 수 있습니다:
1<util:list id="emails" list-class="java.util.LinkedList"> 2 <value>[email protected]</value> 3 <value>[email protected]</value> 4 <value>[email protected]</value> 5 <value>d'[email protected]</value> 6</util:list>
list-class attribute가 제공되지 않으면, container가 List 구현을 선택합니다.
<util:map/>다음 예제를 고려해 보십시오:
1<!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' --> 2<bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean"> 3 <property name="sourceMap"> 4 <map> 5 <entry key="pechorin" value="[email protected]"/> 6 <entry key="raskolnikov" value="[email protected]"/> 7 <entry key="stavrogin" value="[email protected]"/> 8 <entry key="porfiry" value="[email protected]"/> 9 </map> 10 </property> 11</bean>
앞의 configuration은 Spring FactoryBean 구현(MapFactoryBean)을 사용하여
제공된 'sourceMap'에서 가져온 key-value pair로 초기화된 java.util.Map instance를 생성합니다.
다음 예제는 더 간결한 표현을 위해 <util:map/> element를 사용합니다:
1<!-- creates a java.util.Map instance with the supplied key-value pairs --> 2<util:map id="emails"> 3 <entry key="pechorin" value="[email protected]"/> 4 <entry key="raskolnikov" value="[email protected]"/> 5 <entry key="stavrogin" value="[email protected]"/> 6 <entry key="porfiry" value="[email protected]"/> 7</util:map>
또한 <util:map/> element의 'map-class' attribute를 사용하여
instantiated되고 populated되는 Map의 정확한 타입을 명시적으로 제어할 수 있습니다.
예를 들어, 실제로 java.util.TreeMap을 instantiation해야 한다면 다음 configuration을 사용할 수 있습니다:
1<util:map id="emails" map-class="java.util.TreeMap"> 2 <entry key="pechorin" value="[email protected]"/> 3 <entry key="raskolnikov" value="[email protected]"/> 4 <entry key="stavrogin" value="[email protected]"/> 5 <entry key="porfiry" value="[email protected]"/> 6</util:map>
'map-class' attribute가 제공되지 않으면, container가 Map 구현을 선택합니다.
<util:set/>다음 예제를 고려해 보십시오:
1<!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' --> 2<bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean"> 3 <property name="sourceSet"> 4 <set> 5 <value>[email protected]</value> 6 <value>[email protected]</value> 7 <value>[email protected]</value> 8 <value>[email protected]</value> 9 </set> 10 </property> 11</bean>
앞의 configuration은 Spring FactoryBean 구현(SetFactoryBean)을 사용하여
제공된 sourceSet에서 값을 가져와 초기화된 java.util.Set instance를 생성합니다.
다음 예제는 더 간결한 표현을 위해 <util:set/> element를 사용합니다:
1<!-- creates a java.util.Set instance with the supplied values --> 2<util:set id="emails"> 3 <value>[email protected]</value> 4 <value>[email protected]</value> 5 <value>[email protected]</value> 6 <value>[email protected]</value> 7</util:set>
또한 <util:set/> element의 set-class attribute를 사용하여
instantiated되고 populated되는 Set의 정확한 타입을 명시적으로 제어할 수 있습니다.
예를 들어, 실제로 java.util.TreeSet을 instantiation해야 한다면 다음 configuration을 사용할 수 있습니다:
1<util:set id="emails" set-class="java.util.TreeSet"> 2 <value>[email protected]</value> 3 <value>[email protected]</value> 4 <value>[email protected]</value> 5 <value>[email protected]</value> 6</util:set>
set-class attribute가 제공되지 않으면, container가 Set 구현을 선택합니다.
aop Schemaaop 태그는 Spring에서의 모든 AOP 설정을 다루며, 여기에는 Spring 자체 proxy 기반 AOP framework와
AspectJ AOP framework와의 Spring integration이 포함됩니다.
이 태그들은 Aspect Oriented Programming with Spring
이라는 제목의 chapter에서 포괄적으로 다루고 있습니다.
완전성을 위해, aop schema의 태그를 사용하려면 Spring XML configuration 파일 상단에
다음과 같은 preamble이 필요합니다
(snippet의 텍스트는 aop namespace의 태그를 사용할 수 있도록 올바른 schema를 참조합니다):
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> 8 9 <!-- bean definitions here --> 10 11</beans>
context Schemacontext 태그는 plumbing과 관련된 ApplicationContext configuration을 다룹니다.
즉, 보통 최종 사용자에게 중요한 bean이 아니라,
BeanfactoryPostProcessors와 같이 Spring에서 많은 “grunt” 작업을 수행하는 bean들입니다.
다음 snippet은 context namespace의 element를 사용할 수 있도록 올바른 schema를 참조합니다:
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> 8 9 <!-- bean definitions here --> 10 11</beans>
<property-placeholder/>이 element는 ${…} placeholder의 치환을 활성화하며,
이 placeholder는 지정된 properties 파일( Spring resource location로서)에 대해 resolve됩니다.
이 element는 PropertySourcesPlaceholderConfigurer를
설정해 주는 편의 메커니즘입니다.
PropertySourcesPlaceholderConfigurer 설정을 보다 세밀하게 제어해야 한다면,
직접 bean으로 명시적으로 정의할 수 있습니다.
하나의 application에 대해서는 필요한 properties를 가진 이러한 element를 하나만 정의해야 합니다.<br>여러 property placeholder를 설정할 수는 있지만, 서로 다른 placeholder syntax (
${…})를 가져야 합니다.<br>치환에 사용되는 properties의 source를 모듈화해야 한다면, 여러 properties placeholder를 생성해서는 안 됩니다.<br>대신 각 module이Environment에PropertySource를 제공해야 합니다.<br>또는 사용할 properties를 수집하는 자신만의PropertySourcesPlaceholderConfigurerbean을 생성할 수도 있습니다.
<annotation-config/>이 element는 bean class에서 annotation을 감지하기 위한 Spring infrastructure를 활성화합니다:
@Configuration model@Autowired/@Inject, @Value, @Lookup@Resource, @PostConstruct, @PreDestroy (사용 가능한 경우)@WebServiceRef 및 EJB 3의 @EJB (사용 가능한 경우)@PersistenceContext, @PersistenceUnit (사용 가능한 경우)@EventListener또는 이러한 annotation을 위한 개별 BeanPostProcessors를 명시적으로 활성화하도록 선택할 수 있습니다.
이 element는 Spring의<br>
@Transactionalannotation 처리를 활성화하지 않습니다.<br>이를 위해서는<tx:annotation-driven/><br>element를 사용할 수 있습니다. 마찬가지로 Spring의<br>caching annotations도<br>명시적으로 enabled해야 합니다.
<component-scan/>이 element는 annotation-based container configuration 섹션에서 자세히 설명합니다.
<load-time-weaver/>이 element는 load-time weaving with AspectJ in the Spring Framework 섹션에서 자세히 설명합니다.
<spring-configured/>이 element는 using AspectJ to dependency inject domain objects with Spring 섹션에서 자세히 설명합니다.
<mbean-export/>이 element는 configuring annotation-based MBean export 섹션에서 자세히 설명합니다.
마지막으로, beans schema의 element들이 있습니다.
이 element들은 framework의 초기부터 Spring에 존재해 왔습니다.
beans schema의 다양한 element 예제는
dependencies and configuration in detail
(그리고 실제로는 해당 chapter 전체)에서 매우 포괄적으로 다루고 있기 때문에 여기에는 제시하지 않습니다.
<bean/> XML 정의에 zero 개 이상의 key-value pair를 추가할 수 있다는 점에 유의하십시오.
이 추가 metadata로 무엇을 할지(또는 무엇을 하지 않을지)는 전적으로 여러분의 custom logic에 달려 있으며,
따라서 일반적으로 XML Schema Authoring이라는 제목의 부록에 설명된 것처럼
자신만의 custom element를 작성하는 경우에만 유용합니다.
다음 예제는 주변 <bean/> context에서 <meta/> element를 보여줍니다
(이를 해석할 logic이 없으면, metadata는 현재 상태로는 사실상 쓸모가 없습니다).
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="foo" class="x.y.Foo"> 8 <meta key="cacheName" value="foo"/> (1) 9 <property name="name" value="Rick"/> 10 </bean> 11 12</beans>
| 1 | 이것은 예제 meta element입니다 |
앞의 예제의 경우, bean 정의를 consume하고 제공된 metadata를 사용하는 caching infrastructure를 설정하는 어떤 logic이 있다고 가정할 수 있습니다.
Appendix
XML Schema Authoring