這裏是我的代碼:在獨立使用Spring 3自動裝配的Java應用程序
public class Main {
public static void main(String[] args) {
Main p = new Main();
p.start(args);
}
@Autowired
private MyBean myBean;
private void start(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("META-INF/config.xml");
System.out.println("my beans method: " + myBean.getStr());
}
}
@Service
public class MyBean {
public String getStr() {
return "string";
}
}
<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 />
<context:component-scan base-package="mypackage"/>
</beans>
爲什麼不這項工作?我得到NullPointerException
。是否可以在獨立應用程序中使用自動裝配?
盡一切努力使您的問題可讀。並向我們展示異常堆棧跟蹤。 – skaffman 2010-09-07 14:45:47
我在這裏維護一個操作示例:http://tshikatshikaaa.blogspot.com/2012/08/spring-ioc-container-with-annotations.html – JVerstry 2012-11-27 16:22:49