为什么@PostConstruct 在 Spring Boot 应用程序中不起作用?

时间:2021-07-09 07:37:20

标签: java spring spring-boot

我在spring boot应用中有以下代码:

@ApplicationScope
@Component
@Slf4j
public class ApplicationLists {
    
    private List<Country> countryItems = new ArrayList<Country>();

@PostConstruct
    protected void init() {     
        initCountryList();

@PostConstruct 方法只有在我通过邮递员提交请求时才会调用,我需要 此代码运行 om 启动。 谢谢,

1 个答案:

答案 0 :(得分:0)

我正在实现 ApplicationRunner 而不是 @PostConstruct,它有 运行方法。这就是解决问题,我只是希望 这是 Spring Boot 中正确的做法。