主方法类中的 Spring bean 注入
我有一个带有 spring 3.0 的网络应用程序。我需要从 cron 运行一个带有 main 方法的类,该类使用 appcontext xml 中定义的 bean(使用组件扫描注释)。我的主类位于同一个 src 目录中。 如何将 Web 上下文中的 Bean 注入到 main 方法中。我尝试使用 AutoWired 来执行此操作
ApplicationContext context = new ClassPathXmlApplicationContext("appservlet.xml");
,它返回一个 null 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);
}
I have a web application with spring 3.0. I need to run a class with main method from a cron that uses beans defined in appcontext xml(using component scan annocations). I have my main class in same src directory.
How can I inject beans from web context into main method. I tried to do it using
ApplicationContext context = new ClassPathXmlApplicationContext("appservlet.xml");
I tried to use AutoWired and it returns a null bean. So I used Application ctx and this is creating a new context (as expected) when I run main method. But is it possible that I can use existing beans from container.
@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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能将 Spring bean 注入任何不是由 spring 创建的对象。另一种说法是:Spring 只能注入到它管理的对象中。
由于您正在创建上下文,因此您需要为 DAO 对象调用 getBean。
查看 Spring Batch 它可能对您有用。
You can not inject a Spring bean into any object that was not created by spring. Another way to say that is: Spring can only inject into objects that it manages.
Since you are creating the context, you will need to call getBean for your DAO object.
Check out Spring Batch it may be useful to you.
您可以在主应用程序中使用 spring 上下文,并重用与 web 应用程序相同的 bean。您甚至可以重用一些 Spring XML 配置文件,前提是它们没有定义仅在 Web 应用程序上下文(请求范围、Web 控制器等)中有意义的 bean。
但您将获得不同的实例,因为您将运行两个 JVM。如果您确实想重用相同的 bean 实例,那么您的主类应该使用 Web 服务或 HttpInvoker 远程调用 Web 应用程序中 bean 的某些方法。
You may use a spring context for your main application, and reuse the same beans as the webapp. You could even reuse some Spring XML configuration files, provided they don't define beans which only make sense in a webapp context (request-scope, web controllers, etc.).
But you'll get different instances, since you'll have two JVMs running. If you really want to reuse the same bean instances, then your main class should remotely call some method of a bean in your webapp, using a web service, or HttpInvoker.
为了解决这个问题,我创建了 https://jira.springsource.org/browse/SPR -9044。如果您喜欢提议的方法,请投票。
In order to address this issue, I have created https://jira.springsource.org/browse/SPR-9044. If you like the proposed approach, please vote for it.
尝试使用这个 Main:
和这个 Bean:
Try with this Main:
And this Bean:
Spring boot官方为此提供了解决方案。 下载框架
从https://start.spring.io/
,并确保 pom.xml 中的打包是设置为罐子。只要您不包含任何 Web 依赖项,该应用程序将仍然是控制台应用程序。
Spring boot provides an official solution for this. Download a skeleton from
https://start.spring.io/
and make sure packaging in the pom.xml is set to jar. As long as you don't include any web dependency the application will remain a console app.