默认@Autowired没有注入

时间:2014-04-21 03:02:39

标签: java spring autowired

这是一个简单的Spring bean,其中一个字段complexMsg是自动装配的。但是在运行时,bean是null。春季版是3.2。 之前已发布过类似问题,其解决方案已存在于我的代码中。 Bean代码:

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;
}
     ...

Spring config 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>

主要课程:

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

greetingSrvc.complexMsg为空。

3 个答案:

答案 0 :(得分:0)

可能你错过了组件扫描?

  

<context:component-scan base-package="simple"/>

这个.class文件在.jar文件中,可能你可以添加

  

@Component

除了

之外的

注释

  

@服务

注释

例如:

  

@Service @Component public class Abc {}

答案 1 :(得分:0)

我已经尝试过,它对我有用,我在这里使用了应用程序上下文。 你的beans.xml文件内容不合适我改了.. 让我知道其他任何事情.. 请参阅下面的完整代码

的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());


    }

}

答案 2 :(得分:0)

我真的认为你应该使用ApplicationContext其他tan BeanFacotry。原因是ApplicationContext实现了1个接口,我认为它是BeanPostProcessor而BeanFacotry没有。注意beanfactory缺少功能,例如@Transational。