Skip to content
Advertisement

CommandLineRunner overridden run() method not executing

The problem: overridden run() method from CommandLineRunner class is not being executed.

Folder structure:

enter image description here

Code:

Main class:

package com.diplproj.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }

}

CommandLineRunner class:

package com.diplproj.api.config;

import com.diplproj.api.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.sql.*;

@Component
public class DbInitialization implements CommandLineRunner {

    @Autowired
    CropYieldRepository cropYieldRepository;
    @Autowired
    CultureRepository cultureRepository;
    @Autowired
    LocationRepository locationRepository;
    @Autowired
    MicroclimateNameRepository microclimateNameRepository;
    @Autowired
    MicroclimateValueRepository microclimateValueRepository;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("THIS IS NOT PRINTING");
    }

As it says in the println() method, this is not printing anything, only this startup logs:

2021-12-12 17:51:09.371  INFO 17832 --- [  restartedMain] com.diplproj.api.ApiApplication          : Starting ApiApplication using Java 1.8.0_291 on DESKTOP-6IFP1I4 with PID 17832
2021-12-12 17:51:09.373  INFO 17832 --- [  restartedMain] com.diplproj.api.ApiApplication          : No active profile set, falling back to default profiles: default
2021-12-12 17:51:09.452  INFO 17832 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-12-12 17:51:09.452  INFO 17832 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-12-12 17:51:10.706  INFO 17832 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-12-12 17:51:10.835  INFO 17832 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 111 ms. Found 5 JPA repository interfaces.
2021-12-12 17:51:11.819  INFO 17832 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-12-12 17:51:11.830  INFO 17832 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-12-12 17:51:11.831  INFO 17832 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-12-12 17:51:11.971  INFO 17832 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-12-12 17:51:11.971  INFO 17832 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2518 ms
2021-12-12 17:51:12.215  INFO 17832 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-12-12 17:51:12.278  INFO 17832 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-12-12 17:51:12.468  INFO 17832 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-12-12 17:51:12.676  INFO 17832 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

There are couple existing questions on this topic on Stackoverflow, but nothing helped. Does anyone have an idea what might be causing the problem?

Thanks in advance.

Advertisement

Answer

This is weird to be honest. For me it works without problem. Does your Spring project start successfully though?

Started Application in 4.728 seconds (JVM running for 5.885)

This message is missing in your log. CommandLineRunner will be executed after that message.

Another thing to note is you should declare all dependency injection variables as private but that shouldn’t block Commandlinerunner anyways. Also @AutoWired is discouraged to use so try constructor dependency injection instead to see if that is causing problem.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement