我有一个使用spring 3.0的web应用程序。我需要从使用appcontext xml中定义的bean的cron(使用组件扫描注释)中运行main方法。我有我的主要类在同一个src目录。 我怎样才能从Web上下文注入到主要方法的bean。我试图用在主要方法类中的春豆注入
ApplicationContext context = new ClassPathXmlApplicationContext("appservlet.xml");
我试过使用AutoWired,它返回一个空的bean。所以我使用了Application ctx,并且在运行main方法时创建了一个新的上下文(如预期的那样)。但是,我可以使用容器中的现有bean吗?
@Autowired
static DAO dao;
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("xman- servlet.xml");
TableClient client = context.getBean(TableClient.class);
client.start(context);
}
对于未来的读者,一个密切相关的问题:http://stackoverflow.com/questions/3659720/spring-3-autowire-in-standalone-application – Jonik