2016-09-22 2 views
0

다음 Bean XML이 있고 ApplicationConfiguration.java에서 가져오고 Test 클래스에서 자동 작성된 DbManager를 생성했지만 항상 null이 발생합니다. 누구든 도와 줄 수 있습니까?스프링에서 Autowired 객체에 null 값 가져 오기

@Configuration("applicationConfiguration") 
@EnableSpringConfigured 
@ComponentScan 
@EnableCaching(mode = AdviceMode.PROXY, proxyTargetClass = true) 
@EnableTransactionManagement(proxyTargetClass = true, mode = AdviceMode.PROXY) 
@ImportResource({"classpath:META-INF/app-spring-common-config.xml"}) 
public class ApplicationConfiguration extends CachingConfigurerSupport{ 

     //othere beans like datasource, cachemanager 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:cache="http://www.springframework.org/schema/cache" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd 
      http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd"> 

    <bean id="dbManager" class="com.bandu.myfriendsbook.common.services.dbservices.dbmanager.impl.DbManagerImpl"> 

    </bean> 

    <bean id="dbManagers" class="java.util.ArrayList"> 
     <constructor-arg> 
      <list> 
       <ref bean="dbManager"/> 
      </list> 
     </constructor-arg> 
    </bean> 

</beans> 

및 구성 자바 파일은 이제 단지 ApplicationTest.java 콩을 호출하지만, 항상 null지고 있습니다.

@Component 
public class ApplicationTest { 

    @Autowired 
    private DbManagerImpl dbManager; 

    public Integer testQuery(){ 
     return dbManager.testQuery(); 
    } 
} 
+0

@ComponentScan ("my.package")? – degr

답변

2

매개 변수 basePackages 또는 basePackagesClasses와 함께 @ComponentScan을 사용해야합니다. 예 :

@ComponentScan(basePackages = {"com.example"}) 
0

통해 UR XML 파일에 <context:annotation-config/>을 추가 @Autowired 어노테이션을 작동합니다.

관련 문제