Web/spring

[Spring Framework core] 1.7. Bean Definition Inheritance

태애니 2023. 3. 6. 20:12
728x90

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-child-bean-definitions

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

틀린 해석이 있다면 알려주세요 🌾

 


 

1.7. Bean Definition Inheritance

 

Bean 정의에는  constructor arguments, property values, and container-specific information, such as the initialization method, a static factory method name, and so on 등 많은 것을 포함한 관련 구성정보가 포함될 수 있다.

하위 Bean 정의에는 상의 정의에서 구성 데이터를 상속한다

child bean definition 는 일부 값을 재정의하거나 필요에 따라 다른 값을 추가할 수 있다. 상위 및 하위 bean definition을 사용하면 입력을 줄일 수 있다. (일종의 템플릿)

 

 

프로그래밍 방식으로 ApplicationContext 인터페이스로 작업하는 경우 하위 빈 정의는 ChildBeanDefinition 클래스로 표시된다.

대부분의 사용자는 이 수준에서 작업하지않는 부분이나 커스텀이 필요하다면...

대신 ClassPathXmlApplicationContext와 같은 클래스에서 빈 정의를 선언적으로 구성할 수 있다.

XML 기반 구성 메타데이터를 사용하는 경우 상위 속성을 사용하여 이 속성의 값으로 상위 Bean을 지정하여 하위 Bean 정의를 표시할 수 있다.

 

<bean id="inheritedTestBean" abstract="true"
        class="org.springframework.beans.TestBean">
    <property name="name" value="parent"/>
    <property name="age" value="1"/>
</bean>

<bean id="inheritsWithDifferentClass"
        class="org.springframework.beans.DerivedTestBean"
        parent="inheritedTestBean" init-method="initialize">   // 여기가 parent attribute
    <property name="name" value="override"/>
    <!-- the age property value of 1 will be inherited from parent -->
</bean>

 

 

Child Bean definition은 아무것도 지정되어있지않은 상태이나 이를 재정의해야할 경우 Parent Bean definition 의 bean class를 사용하면 된다. child bean 클래스는 부모와 호환되어야한다. (parent attribute 도 accept)

 

A child bean definition inherits scope, constructor argument values, property values, and method overrides from the parent, with the option to add new values. Any scope, initialization method, destroy method, or static factory method settings that you specify override the corresponding parent settings.

The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, and lazy init.

The preceding example explicitly marks the parent bean definition as abstract by using the abstract attribute. If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required, as the following example shows:

<bean id="inheritedTestBeanWithoutClass" abstract="true">
    <property name="name" value="parent"/>
    <property name="age" value="1"/>
</bean>

<bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean"
        parent="inheritedTestBeanWithoutClass" init-method="initialize">
    <property name="name" value="override"/>
    <!-- age will inherit the value of 1 from the parent bean definition-->
</bean>

 

 

parent bean은 불완전한 상태이기 때문에 자체적으로 instance하지않고 abstract 명시적으로 표시된다

abstract 이면 child definition에 대한 parent definition 역할을 하는 순수한 템플릿 bean definition 로 사용하는 것이다

다른 bean의 ref 속성으로 참조하거나 부모 bean ID로 명시적인 getBean() 호출을 수행하여 이러한 추상 부모 bean을 자체적으로 사용하려고 하면 오류가 반환된다. 마찬가지로 컨테이너의 내부 preInstantiateSingletons() 메서드는 추상으로 정의된 빈 정의를 무시한다

 

 

ApplicationContext는 기본적으로 모든 싱글톤을 미리 instance화 한다.

따라서 템플릿으로만 사용하려는 (상위) 빈 정의가 있고 이 정의가 클래스를 지정하는 경우이고 특히 Singleton bean일 경우에는 추상 속성을 true로 설정해야 하는 것이 중요하다.  그렇지 않으면 애플리케이션 컨텍스트는 실제로 추상 빈을 사전 인스턴스화(시도)하게 된다.

728x90