Đây là mã của tôi:Sử dụng mùa xuân 3 autowire trong một độc lập Java ứng dụng
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>
Tại sao không làm việc này? Tôi nhận được NullPointerException
. Có thể sử dụng autowiring trong một ứng dụng độc lập không?
Cố gắng làm cho câu hỏi của bạn có thể đọc được. Và cho chúng tôi thấy dấu vết ngăn xếp ngoại lệ. – skaffman
Tôi duy trì một ví dụ hoạt động tại đây: http://tshikatshikaaa.blogspot.com/2012/08/spring-ioc-container-with-annotations.html – JVerstry