How to call a service from Main application calls

2020-05-26 09:57发布

I'm building a Spring Boot application that will be called from command line. I will pass to application some parameters but I'm having problems to call a service from this class:

@SpringBootApplication
public class App{

    public static void main(String[] args) {

        SpringApplication.run(App.class, args);

        App app = new App();
        app.myMethod();    
    }

    @Autowired
    private MyService myService;

    private void myMethod(){
        myService.save();
    }
}

I'm trying to call a method from inside the main but I'm getting the error:

Exception in thread "main" java.lang.NullPointerException
at com.ef.Parser.App.myMethod(Application.java:26)
at com.ef.Parser.App.main(Application.java:18)

3条回答
够拽才男人
2楼-- · 2020-05-26 10:03

By using the new keyword yourself to create an instance of the App class, Spring cannot know about it.

It's also redundant, as Spring automatically creates a bean instance of this class by a mechanism called component scan.

I like the solution of the CommandLineRunner.

What you also can do, is retrieve the ApplicationContext, lookup the bean and then call the method.

You can inject the ApplicationContext by letting your App class implement ApplicationContextAware, override the setter method and save the context in a static variable which you can access from your main method.

Then, you can use it to retrieve the correct App instance.

App myApp = (App) applicationContext.getBean(App.class);
myApp.myMethod()

Please note that accessing the ApplicationContext directly does kind of violate the whole dependency injection principle, but sometimes you haven't got much choice.

查看更多
smile是对你的礼貌
3楼-- · 2020-05-26 10:11

you can create a class that implements CommandLineRunner and this will be invoked after the app will start

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
    @Autowired
    private MyService myService;

    @Override
    public void run(String...args) throws Exception {
       myService.save();

    }
}

you can get farther information on this here

查看更多
神经病院院长
4楼-- · 2020-05-26 10:27

In SpringBoot 2.x you can simply run the application by SpringApplication.run method and operate on the returned ApplicationContext. Complete example below:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.util.Arrays;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        SomeService service = applicationContext.getBean(SomeService.class);
        service.doSth(args);
    }
}

@Service
class SomeService {

    public void doSth(String[] args){
        System.out.println(Arrays.toString(args));
    }
}
查看更多
登录 后发表回答