이것은 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입니다.
XmlBeanFactory가 범인입니다. ClassPathXmlApplicationContext를 대신 사용하십시오. 또한 autowired 종속성에는 기본 생성자가 없습니다. 마침내 일했다. – kakoli