Java 当我拥有所有参数构造函数时,我是否应该永远不要在Spring Boot中使用@PostConstruct?

Java 当我拥有所有参数构造函数时,我是否应该永远不要在Spring Boot中使用@PostConstruct?,java,spring,spring-boot,constructor-injection,postconstruct,Java,Spring,Spring Boot,Constructor Injection,Postconstruct,在out项目中,我们不使用setter或field注入,我们只使用构造函数注入,我知道这两个选项都是1。二,。可能有用 在这种情况下,在构造函数中使用bean是否不安全 或者spring boot 2+会产生一些效果,我最好使用选项1。而不是2。我无法想象选项1会出问题的情况 @Component @ConfigurationProperties("config") public class ServiceConfigProperties { // .... s

在out项目中,我们不使用setter或field注入,我们只使用构造函数注入,我知道这两个选项都是1。二,。可能有用

  • 在这种情况下,在构造函数中使用bean是否不安全
或者spring boot 2+会产生一些效果,我最好使用选项1。而不是2。我无法想象选项1会出问题的情况

@Component
@ConfigurationProperties("config")
public class ServiceConfigProperties  {
    // .... some code
}
  • 可能不安全吗?-但是看起来好多了
  • 不会不安全吧
  • 使用,在构造函数中与构造函数注入的bean交互是完全安全的

    @Component
    public class Service {
        private boolean skipCheck;
    
        public Service(ServiceConfigProperties configProps) {
            this.skipCheck = configProps.isSkipCheck();
        }
    }
    
    @Component
    public class Service {
        private boolean skipCheck;
        private ServiceConfigProperties configProps;
    
        public Service(ServiceConfigProperties configProps) {
            this.configProps= configProps;
        }
    
        @PostConstruct
        public void initConfig() {
            this.skipCheck= configProps.isSkipCheck();
        }   
    }