[SPRING] 스프링 빈 정의의 우선 순위는 무엇입니까?
SPRING스프링 빈 정의의 우선 순위는 무엇입니까?
여러 개의 Spring bean이 같은 이름으로 정의 될 때, 어느 것이 다른 것을 숨길까요?
org.example 패키지에 @Component ( "bean")로 주석을 추가 한 여러 클래스와 함께 다음을 포함하는 applicationContext.xml이 있다고 가정 해 보겠습니다.
<context:component-scan base-package="org.example"/>
<alias name="aliasedBean" alias="bean"/>
<bean id="aliasedBean" class="org.example.AliasedBean"/>
<bean id="bean" class="org.example.XmlBean"/>
<import resource="otherApplicationContext.xml"/>
ApplicationContext.getBean ( "bean")을 수행하면 해당 bean이 검색됩니다.
Spring 문서에 따르면 :
그러나 이것이 완료되면 Spring이 불평하지 않는다는 것을 알았습니다 (테스트했기 때문에). 하나의 정의는 다른 것을 숨길 것입니다. 그러나 나는 규칙이 무엇인지 알 수 없었다.
나는 이것을 테스트 목적으로하고 싶다. 실제 (프로덕션) 빈을 정의하기 위해 주석 기반 구성을 사용합니다. 그런 다음 테스트 관련 XML 구성 파일을 사용하여 이러한 정의를 무시하고 mock bean을 주입하려고합니다.
편집 : 여러 번 로그를 요청 했으므로 시간을 좀 들여 만들어 보았습니다. 여기 있습니다 :
0 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3934f69a: startup date [Wed Mar 06 23:04:35 CET 2013]; root of context hierarchy
45 INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
223 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'bean': replacing [Generic bean: class [org.example.AnnotatedBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/etienne/Documents/Développement/Registre/workspace/SpringPrecedence/target/classes/org/example/AnnotatedBean.class]] with [Generic bean: class [org.example.XmlBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]]
223 INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [otherApplicationContext.xml]
246 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'bean': replacing [Generic bean: class [org.example.XmlBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]] with [Generic bean: class [org.example.ImportedXmlBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [otherApplicationContext.xml]]
290 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6aba4211: defining beans [bean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,aliasedBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
290 INFO org.example.AliasedBean - Construction of AliasedBean.
302 INFO org.example.Main - Application context loaded.
302 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@3934f69a: startup date [Wed Mar 06 23:04:35 CET 2013]; root of context hierarchy
302 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6aba4211: defining beans [bean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,aliasedBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
몇 가지 테스트를 한 후, 다음과 같은 경우 컨텍스트 생성 중에 예외가 발생한다는 것을 알게되었습니다.
해결법
-
==============================
1.그래서 XML이 이깁니다.
그래서 XML이 이깁니다.
먼저 component-scan 태그를 넣으면 xml beans가 스캔 한 태그를 덮어 씁니다. 마지막에 넣으면 스캔 된 빈은 무시됩니다.
편집하다
별명은 bean 정의에서 name 속성에 선언 된 것과 다른 별명을 가지거나 별명 태그를 사용하여 선언됩니다.
예 :
<bean id="foo" name="bar" class="Foo" /> <bean id="bar" class="Bar" /> -- throw Exception (name bar is in use)
그러나
<bean id="foo" class="Foo" /> <alias name="foo" alias="bar" /> <bean id="bar" class="Bar" /> -- Hidden by alias no exception thrown
차이점은 BeanDefinitionParserDelegate는 Bean 요소 중첩의 동일한 빈 레벨에서 사용중인 이름 및 별명 목록을 보유하고 Bean 정의를 구문 분석 할 때 이름 고유성을 확인합니다.
별명 태그는 DefaultBeanDefinitionDocumentReader.processAliasRegistration ()에 의해 직접 처리되며 파서 대리자는이 이름을 인식하지 못합니다.
버그인지 의도적인지는 모르지만 참조는 아무 것도 말하지 않고 별칭의 내부 및 외부 선언이 동일한 동작을 할 것으로 예상됩니다.
from https://stackoverflow.com/questions/15233766/what-is-the-precedence-of-spring-beans-definitions by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Weblogic에 스프링 부트 응용 프로그램 배포 (0) | 2019.01.11 |
---|---|
[SPRING] 왜 트랜잭션은 RuntimeException에서 롤백하지만 SQLException은 그렇지 않다. (0) | 2019.01.11 |
[SPRING] "잠금을 얻으려고 할 때 교착 상태가 발견되었습니다. 트랜잭션을 다시 시작하십시오. " (0) | 2019.01.11 |
[SPRING] Spring MVC : 컨트롤러 메소드 매개 변수에 요청 속성을 바인드한다. (0) | 2019.01.11 |
[SPRING] 서비스 계층이 정말로 필요합니까? (0) | 2019.01.11 |