2014-04-21 6 views
0

이것은 complexMsg 필드 중 하나가 자동으로 실행되는 단순한 스프링 빈입니다. 그러나 실행 중에 빈은 null입니다. Spring 버전은 3.2입니다. 유사한 문제가 이전에 게시되었으며, 그 해결책은 이미 내 코드에 있습니다. 콩 코드 :기본값 @Autowired not injecting

public class GreetingServiceImpl implements IGreetingService { 
private String simpleMsg = null;  
//Autowired annotation is equivalent to autowire="byType" 
@Autowired 
private ComplexMessage complexMsg ; 

public String getSimpleMsg() { 
    return simpleMsg; 
} 
    ... 

봄 구성 XML :

<?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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
<context:annotation-config /> 
<bean id="greetingBean" class="simple.GreetingServiceImpl"> 
    <property name="simpleMsg" value="HelloWorld!!" /> 
</bean> 
<bean id="complexMsg" class="simple.ComplexMessage"> 
    <property name="receiver" value="Raka" /> 
    <property name="content" value="MerryChristmas" /> 
</bean> 
</beans> 

Main 클래스 :

public static void main(String[] args) { 
    IGreetingService greetingSrvc = null;  
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("greetingConfig.xml")); 
    greetingSrvc =(IGreetingService) beanFactory.getBean("greetingBean"); 

greetingSrvc.complexMsg은 null입니다.

+0

XmlBeanFactory가 범인입니다. ClassPathXmlApplicationContext를 대신 사용하십시오. 또한 autowired 종속성에는 기본 생성자가 없습니다. 마침내 일했다. – kakoli

답변

0

난 정말 당신이해야한다고 생각 내 전체 코드

beans.xml 환경은 아래를 참조 알려 .. 그것을 변경 적절하지 않았다 ApplicationContext 기타 tan BeanFacotry을 사용하십시오. 그 이유는 ApplicationContext가 BeanPostProcessor이라고 생각하는 1 개의 인터페이스를 구현하고 BeanFacotry가 그렇지 않기 때문입니다. beanfactory는 @Transational와 같은 기능이 부족합니다.

0

아마도 구성 요소 검사를 놓치셨습니까? <context:component-scan base-package="simple"/>

그것을

이의 .class 파일이 .jar 파일에 아마 당신은

에 추가하여

@Component에게

주석을 추가 할 수 있습니다

@ 서비스

annotation.

는 예 :

@Service @Component 공용 클래스 ABC는 {}

0

나는 그것을 시도하고 그것은 나를 위해 일하고, 나는 여기에 애플리케이션 컨텍스트를 사용했다. 당신의 beans.xml 환경의 파일 내용을 내가 내가 다른 거 .. 이

<?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<context:annotation-config /> 
<bean id="greetingBean" class="com.kb.autowiring2.GreetingServiceImpl"> 

</bean> 
<bean id="complexMsg" class="com.kb.autowiring2.ComplexMessage"> 

</bean> 
</beans> 

package com.kb.autowiring2; 

public interface IGreetingService { 
    public ComplexMessage getComplexMsg(); 

} 


package com.kb.autowiring2; 

import org.springframework.beans.factory.annotation.Autowired; 

public class GreetingServiceImpl implements IGreetingService { 
    @Autowired 
    private ComplexMessage complexMsg ; 

    public ComplexMessage getComplexMsg() { 
     return complexMsg; 
    } 

} 


package com.kb.autowiring2; 

public class ComplexMessage { 

} 


package com.kb.autowiring2; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.kb.autowiring1.Person; 

public class Client { 

    public static void main(String[] args) { 
     IGreetingService greetingSrvc = null;  
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/autowiring2/beans.xml"); 
      IGreetingService p = applicationContext.getBean("greetingBean",IGreetingService.class); 
      System.out.println(p.getComplexMsg()); 


    } 

}