Home > Blockchain >  CommandLineRunner Never Executes (Spring Boot 2.6.7)
CommandLineRunner Never Executes (Spring Boot 2.6.7)

Time:05-02

I am trying to learn Spring Boot and restful application from an online tutorial. But the code I wrote for it somehow gives me an error. I have written a CommandLineRunner class like so:

package com.trial.cas.preload;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.trial.cas.employee.repository.EmployeeRepository;
import com.trial.cas.employee.pojo.Employee;


@Configuration
class LoadDatabase implements CommandLineRunner {

    private static final Logger log = java.util.logging.LogManager.getLogManager().getLogger("LoadDatabase");
    
    @Autowired
    EmployeeRepository employeeRepository;

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

        log.info("Preloading "   employeeRepository.save(new Employee("Bilbo Baggins", "burglar")));
        log.info("Preloading "   employeeRepository.save(new Employee("Frodo Baggins", "thief")));      
    }
}

My EmployeeRepository class is like this:

package com.trial.cas.preload;

import org.springframework.data.jpa.repository.JpaRepository;

import com.trial.employee.pojo.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

}

My main class is written like:

package com.trial.cas.logging.example;

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

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

But when I run the Spring Boot application, the lines in the run method never execute. I have a debug point point in the first line of the run method. But that never gets triggered.

Please help! Thanks in advance.

CodePudding user response:

Try using @Component instead of @Configuration when annotating your LoadDatabase class. Also, if I recall correctly, for Spring Boot to run properly it is necessary to annotate your EmployeeRepository with @Repository.

Another last tip, try to avoid field injections like:

@Autowired
EmployeeRepository employeeRepository;

It has several drawbacks compared to constructor injection, which are described in detail in this answer: What exactly is Field Injection and how to avoid it?

Instead use constructor injection, like this:

private EmployeeRepository employeeRepository;

@Autowired
public LoadDatabase(EmployeeRepository employeeRepository){
 this.employeeRepository = employeeRepository;
}

CodePudding user response:

By default Spring Boot will only scan packages below the package of the main application class (in your example it would be the package com.trial.cas.logging.example and all subpackages.

You can either move the application class to a common super package for your application, or specify all other packages, that should be scanned for beans etc. on the annotation:

@SpringBootApplication(scanBasePackages = {"com.trial.cas.logging.example", "com.trial.cas.preload"})
  • Related