복붙노트

[SPRING] Spring @Autowired 같은 유형의 2 개의 콩

SPRING

Spring @Autowired 같은 유형의 2 개의 콩

나는 다음을 정의했다.

@Autowired
DaoType1<object1> someDao;

@Autowired
DaoType1<object1> someListDao;

내 bean 정의에 나는 같은 타입의 두 콩을 가지고있다.

<bean id="someDao" class="com.example.DaoType1" />
<bean id="someListDao" class="com.example.DaoType1" />

두 번째 빈은 다른 XML 파일에서 가져온 것이고 차이가 나는 경우입니다. 그것들은 다른 속성도 설정되어 있습니다. 같은 종류의 2 개의 빈이 정의 되었기 때문에 스프링이 오류를 던지지 않는 이유는 무엇입니까? 변수 이름이 bean id와 일치하기 때문에 변수 이름을 사용합니까? 두 개의 다른 bean에 대해 @Qualifiers를 사용했다면 dao가 다르며 기능은 예상대로 작동합니다.

다음은 좀 더 간결한 버전입니다. 나는 그들이 관련이 없기 때문에 다른 콩을 버렸다.

applicationContext.xml

<import resource="classpath:dm-services-crud.xml"/>
<bean id="ruleListCrudService" class="com.idna.dm.service.crud.impl.RuleCrudServiceImpl"> 
    <property name="crudDao" ref="ruleListCrudDao" />
</bean>

dm-services-crud.xml

    <bean id="ruleCrudService" class="com.idna.dm.service.crud.impl.RuleCrudServiceImpl">
        <property name="crudDao" ref="ruleCrudDao" />
        <property name="ruleNetworkOfNodesCrudService" ref="ruleNetworkOfNodesCrudService" />
        <property name="elementMappingsCrudService" ref="elementMappingsCrudService" />
        <property name="ruleCrudDao" ref="newRuleCrudDao"/>
   </bean>

default-autowire는 내 XML 파일에 전혀 존재하지 않습니다.

해결법

  1. ==============================

    1.이것은 예상되는 동작 인 것으로 보입니다. 설명서에 다음과 같이 적혀 있습니다.

    이것은 예상되는 동작 인 것으로 보입니다. 설명서에 다음과 같이 적혀 있습니다.

    이것은 applicationContext.xml에 default-autowire = "byName"을 지정했다는 의미입니다.

    그러나 리팩토링은 예측할 수없는 방식으로이 작업에 영향을 줄 수 있습니다. 그래서 (유형에 따라) autowiring으로 전환하는 것이 바람직하며, 빈을 사용하여 빈을 명확히하는 것이 좋습니다.

  2. ==============================

    2.@Autowired 어노테이션은 xml 기반 bean 정의에 대한 "autowire by type"스펙과 약간 다르게 동작한다.

    @Autowired 어노테이션은 xml 기반 bean 정의에 대한 "autowire by type"스펙과 약간 다르게 동작한다.

    특수 효과를 사용하는 경우 기술적으로 자동 와이어를 사용하지 않고 있습니다 ... 특수 효과를 기반으로 값을 설정하고 있습니다. autowire 주석은 xml 속성 요소와 동일한 기능을합니다.

  3. from https://stackoverflow.com/questions/3925681/spring-autowired-with-2-beans-of-the-same-type by cc-by-sa and MIT license