CommandLineRunner 从不执行(Spring Boot 2.6.7)
CommandLineRunner Never Executes (Spring Boot 2.6.7)
我正在尝试通过在线教程学习 Spring 引导和 restful 应用程序。但是我为它编写的代码不知何故给了我一个错误。我写了一个 CommandLineRunner
class 像这样:
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")));
}
}
我的EmployeeRepository
class是这样的:
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> {
}
我的主要class是这样写的:
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);
}
}
但是当我 运行 Spring 引导应用程序时,run
方法中的行永远不会执行。我在运行方法的第一行有一个调试点point。但这永远不会被触发。
请帮忙!提前致谢。
在注释您的 LoadDatabase class 时尝试使用 @Component
而不是 @Configuration
。
另外,如果我没记错的话,为了 Spring 正确启动到 运行,有必要用 @Repository
.
注释你的 EmployeeRepository
另一个最后的提示,尽量避免像这样的字段注入:
@Autowired
EmployeeRepository employeeRepository;
与构造函数注入相比,它有几个缺点,在这个答案中有详细描述:
改为使用构造函数注入,像这样:
private EmployeeRepository employeeRepository;
@Autowired
public LoadDatabase(EmployeeRepository employeeRepository){
this.employeeRepository = employeeRepository;
}
默认情况下 Spring Boot 将仅扫描主应用程序包下面的包 class(在您的示例中它将是包 com.trial.cas.logging.example
和所有子包。
您可以将应用程序 class 移动到您的应用程序的通用超级包,或者指定所有其他包,这些包应该在注释上扫描 bean 等:
@SpringBootApplication(scanBasePackages = {"com.trial.cas.logging.example", "com.trial.cas.preload"})
我正在尝试通过在线教程学习 Spring 引导和 restful 应用程序。但是我为它编写的代码不知何故给了我一个错误。我写了一个 CommandLineRunner
class 像这样:
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")));
}
}
我的EmployeeRepository
class是这样的:
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> {
}
我的主要class是这样写的:
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);
}
}
但是当我 运行 Spring 引导应用程序时,run
方法中的行永远不会执行。我在运行方法的第一行有一个调试点point。但这永远不会被触发。
请帮忙!提前致谢。
在注释您的 LoadDatabase class 时尝试使用 @Component
而不是 @Configuration
。
另外,如果我没记错的话,为了 Spring 正确启动到 运行,有必要用 @Repository
.
另一个最后的提示,尽量避免像这样的字段注入:
@Autowired
EmployeeRepository employeeRepository;
与构造函数注入相比,它有几个缺点,在这个答案中有详细描述:
改为使用构造函数注入,像这样:
private EmployeeRepository employeeRepository;
@Autowired
public LoadDatabase(EmployeeRepository employeeRepository){
this.employeeRepository = employeeRepository;
}
默认情况下 Spring Boot 将仅扫描主应用程序包下面的包 class(在您的示例中它将是包 com.trial.cas.logging.example
和所有子包。
您可以将应用程序 class 移动到您的应用程序的通用超级包,或者指定所有其他包,这些包应该在注释上扫描 bean 等:
@SpringBootApplication(scanBasePackages = {"com.trial.cas.logging.example", "com.trial.cas.preload"})