0

I am trying to use the value of a bean at the main class but I am getting an error because it can't be used in a static context.

@SpringBootApplication
public class MigrationsApplication implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(MigrationsApplication.class);

    @Autowired
    private UrlInterface url;
    @Autowired
    private UsernameInterface username;
    @Autowired
    private PasswordInterface password;
    @Autowired
    private ChangelogInterface changelog;

    @Bean
    public UrlInterface getUrl(@Value("${spring.datasource.url}") String dbUrl) {

        return () -> dbUrl;
    }

    @Bean
    public UsernameInterface getUsername(@Value("${spring.datasource.username}") String dbUsername) {
        return () -> dbUsername;
    }

    @Bean
    public PasswordInterface getPassword(@Value("${spring.datasource.password}") String dbPassword) {
        return () -> dbPassword;
    }

    @Bean
    public ChangelogInterface getChangelog(@Value("${spring.liquibase.change-log}") String changelogPath) {
        return () -> changelogPath;
    }




    public static void main(String[] args) throws DatabaseException {
        if (args.length < 2) {
            logger.error("Please provide the desired command and the properties file");
            System.exit(-1);
        }

        switch (args[0]) {
            case "run":
                ConfigurableApplicationContext ctx = SpringApplication.run(MigrationsApplication.class, args);
                int exitCode = SpringApplication.exit(ctx, () -> 0);
                logger.info("All migrations were finished with success");
                System.exit(exitCode);
                break;

            case "rollback":
                /*Connection connection = null;
                try {
                    connection = openConnection(url.getUrl(),"","");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
               
            case "release":
                logger.warn("RELEASE");
                break;

            default:
                logger.error("Invalid command");
                break;
        }
    }

    @Override
    public void run(String... args) throws Exception {
    }
}

How can I turn around this? I can't move the code to the run method because even though it's empty it stills updates my database. Why does the method run updates my database even if it is empty? Is it even possible to use properties values in the Main class?

Fabio
  • 343
  • 1
  • 6
  • 17

1 Answers1

2

Only after SpringApplication.run is executed, the ApplicationContext instance is created and then the Beans would be loaded and are available.

To access a Bean in static main method, you could fetch it directly from the ApplicationContext. Maybe something like:

ApplicationContext ctx = SpringApplication.run(MigrationsApplication.class, args);
UrlInterface url = (UrlInterface) ctx.getBean("getUrl");

For using @Autowired with a static method. You need to use static variable and inject it with @Autowired on the constructor. See this answer for example

Mo_-
  • 574
  • 3
  • 6