Qui è il mio codice:Utilizzando Spring 3 autowire in un alone un'applicazione 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>
Perché non fa questo lavoro? Ottengo NullPointerException
. È possibile utilizzare l'autowiring in un'applicazione standalone?
Fare uno sforzo per rendere la vostra domanda leggibile. E mostraci la traccia dello stack delle eccezioni. – skaffman
Mantengo un esempio operativo qui: http://tshikatshikaaa.blogspot.com/2012/08/spring-ioc-container-with-annotations.html – JVerstry