Buch
Buch

Reputation: 97

Spring boot: accessing command line args from a bean

I run spring boot application with a single argument - filename. The file contains some properties I need in runtime. When the application starts it checks if args.length==1

But I need this file (properties) in a single point - @Component annotated bean.

Is it a way to get an access to the file's content from it?

Upvotes: 7

Views: 2468

Answers (1)

Vaidas
Vaidas

Reputation: 1503

In Spring, there is a bean called ApplicationArguments that provides access to the arguments used to run an application.

@Component
public MyComponent {
    @Autowired
    private ApplicationArguments  applicationArguments;

    public void method() {
        List<String> filenameArgs = applicationArguments.getOptionValues("filename")
    }
}

Upvotes: 8

Related Questions