首页 > 解决方案 > 属性源没有自动装配

问题描述

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

错误信息

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

引起:java.lang.NullPointerException: null at 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

解决方案


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

如果您需要从中提取一些值,emailPropertyConfig或者cipherCrypt您可以在@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;
    }
}

推荐阅读