spring 主方法类中的Spring bean注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8313070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:46:49  来源:igfitidea点击:

Spring bean injection in a main method class

springdependency-injectioninversion-of-controlmainautowired

提问by riamob

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

我有一个带有 spring 3.0 的 Web 应用程序。我需要从使用 appcontext xml 中定义的 bean 的 cron 运行一个带有 main 方法的类(使用组件扫描注释)。我在同一个 src 目录中有我的主类。如何将 Web 上下文中的 bean 注入 main 方法。我试着用

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,它返回一个空 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);

}

回答by DwB

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.

您不能将 Spring bean 注入任何不是由 spring 创建的对象。另一种说法是:Spring 只能注入它管理的对象。

Since you are creating the context, you will need to call getBean for your DAO object.

由于您正在创建上下文,因此您需要为您的 DAO 对象调用 getBean。

Check out Spring Batchit may be useful to you.

查看Spring Batch它可能对您有用。

回答by btiernay

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.

为了解决这个问题,我创建了https://jira.springsource.org/browse/SPR-9044。如果您喜欢建议的方法,请投票支持。

回答by madx

Try with this Main:

试试这个主要:

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("classpath*:/META-INF/spring/applicationContext*.xml");
        System.out.println("The method of my Bean: " + myBean.getStr());
    }
}

And this Bean:

还有这个豆子:

@Service 
public class MyBean {
    public String getStr() {
        return "mybean!";
    }
}

回答by Peter Szanto

Spring boot provides an official solution for this. Download a skeleton from

Spring Boot 为此提供了官方解决方案。从下载骨架

https://start.spring.io/

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.

并确保 pom.xml 中的包装设置为 jar。只要您不包含任何 Web 依赖项,该应用程序将保持为控制台应用程序。

回答by JB Nizet

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.).

您可以为主应用程序使用 spring 上下文,并重用与 web 应用程序相同的 bean。您甚至可以重用一些 Spring XML 配置文件,前提是它们没有定义仅在 webapp 上下文(请求范围、web 控制器等)中有意义的 bean。

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.

但是您将获得不同的实例,因为您将有两个 JVM 正在运行。如果您真的想重用相同的 bean 实例,那么您的主类应该使用 web 服务或 HttpInvoker 远程调用 web 应用程序中 bean 的某些方法。