Spring自动连接类为null

时间:2016-03-31 20:27:26

标签: java spring

有一些类似的问题,我检查过但无法找到解决问题的方法。我正在尝试非常基本的春季应用程序,但收到错误。

这是我的appcontext.xml

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.spring.springlegacy"></context:component-scan>

和我的组件

@Component("springService")
public class SpringServiceImpl implements SpringService{
    @Override
    public void giveService() {
        System.err.println("service given");
    }
}

和我的有线变量

public class Printer {
    @Autowired
    private SpringService springService;

    public void print(){
        springService.giveService();
    }
}

和主要方法

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
Printer printer = new Printer();
printer.print();

,错误是

Exception in thread "main" java.lang.NullPointerException
    at com.spring.springlegacy.Printer.print(Printer.java:10)
    at com.spring.springlegacy.Main.main(Main.java:13)

这里缺少什么?

PS:由于我没有在弹簧管理对象中使用new关键字,我相信这个问题与其他问题不同。

0 个答案:

没有答案