【问题标题】:Property Source not getting autowired属性源没有自动装配
【发布时间】:2020-03-15 06:54:02
【问题描述】:

由于某种原因,我正在使用外部属性源,其中一个外部属性源没有自动装配,在创建身份验证 bean 时接收到空指针

错误信息

原因:org.springframework.beans.BeanInstantiationException:无法实例化[com.filechecker.check.Authenticator]:构造函数抛出异常;嵌套异常是 java.lang.NullPointerException

原因:java.lang.NullPointerException: null 在 com.filechecker.check.Authenticator.(Authenticator.java:30) ~[classes!/:0.0.1-SNAPSHOT]

第 30 行:

    String username = emailPropertyConfig.getEmailConfig().getUsername();

不工作一个

@Component
@PropertySource(value="${email.app.properties}",ignoreResourceNotFound = false)
@ConfigurationProperties
public class PropertyEmailConfiguration {

    private EmailConfig emailConfig =  new EmailConfig();

    public EmailConfig getEmailConfig() {
        return emailConfig;
    }

    public void setEmailConfig(EmailConfig emailConfig) {
        this.emailConfig = emailConfig;
    }
}


@Component
public class Authenticator extends javax.mail.Authenticator {

    @Autowired
    PropertyEmailConfiguration emailPropertyConfig;

    @Autowired
    CipherCrypt cipherCrypt;

    private PasswordAuthentication authentication;

    public Authenticator() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {

        String username = emailPropertyConfig.getEmailConfig().getUsername();
        String password = cipherCrypt.decrypt(emailPropertyConfig.getEmailConfig().getEncryptPassword());
        authentication = new PasswordAuthentication(username, password);
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return authentication;
    }
}

工作一个

@Component
@PropertySource(value="${external.app.properties}", ignoreResourceNotFound = true)
@ConfigurationProperties
public class PropertyConfiguration {

    private List<FileStructureConfig> fileStructureConfig = new ArrayList();

    private List<EmailSendingProperties> emailSendingProperties  = new ArrayList();

    public List<FileStructureConfig> getFileStructureConfig() {
        return fileStructureConfig;
    }

    public void setFileStructureConfig(List<FileStructureConfig> fileStructureConfig) {
        this.fileStructureConfig = fileStructureConfig;
    }

    public List<EmailSendingProperties> getEmailSendingProperties() {
        return emailSendingProperties;
    }

    public void setEmailSendingProperties(List<EmailSendingProperties> emailSendingProperties) {
        this.emailSendingProperties = emailSendingProperties;
    }


}

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    您正在尝试访问构造函数中的@Autowired 属性。在此阶段无法自动装配该属性。 为了让 Spring “烘焙 bean”,Spring 必须创建您的对象(使用您的构造函数),然后才应用自动装配机制注入 emailPropertyConfigcipherCrypt。因此,您无法访问构造函数中的两个 @Autowired 属性。

    如果您需要从emailPropertyConfigcipherCrypt 中提取一些值,您可以在@PostConstruct 中进行操作

    @Component
    public class Authenticator {
    
        @Autowired
        PropertyEmailConfiguration emailPropertyConfig;
    
        @Autowired
        CipherCrypt cipherCrypt;
    
        private PasswordAuthentication authentication;
    
        @PostConstruct
        void init() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
    
          String username = emailPropertyConfig.getEmailConfig().getUsername();
          String password = cipherCrypt.decrypt(emailPropertyConfig.getEmailConfig().getEncryptPassword());
          authentication = new PasswordAuthentication(username, password);
        }
    
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return authentication;
        }
    }
    

    或使用构造函数注入:

    @Component
    public class Authenticator {
    
        PropertyEmailConfiguration emailPropertyConfig;
    
        CipherCrypt cipherCrypt;
    
        private PasswordAuthentication authentication;
    
        public Authenticator(PropertyEmailConfiguration emailPropertyConfig, CipherCrypt cipherCrypt) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
    
          String username = emailPropertyConfig.getEmailConfig().getUsername();
          String password = cipherCrypt.decrypt(emailPropertyConfig.getEmailConfig().getEncryptPassword());
          authentication = new PasswordAuthentication(username, password);
        }
    
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return authentication;
        }
    }
    

    【讨论】:

    • 感谢我使用 post 结构解决了这个问题,抱歉我忘记了基础知识。
    • 不客气。请将我的回答标记为“已接受”。这样可以帮助其他人在遇到类似问题时快速找到正确答案。
    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2020-08-27
    • 2020-01-08
    相关资源
    最近更新 更多