riamob
riamob

Reputation: 299

Spring bean injection in a main method class

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

}

Upvotes: 16

Views: 51041

Answers (5)

Peter Szanto
Peter Szanto

Reputation: 7722

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.

Upvotes: 1

madx
madx

Reputation: 7203

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!";
    }
}

Upvotes: 1

btiernay
btiernay

Reputation: 8129

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.

Upvotes: 1

DwB
DwB

Reputation: 38300

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.

Upvotes: 7

JB Nizet
JB Nizet

Reputation: 691785

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.

Upvotes: 2

Related Questions