spring-boot - 属性源没有自动装配
问题描述
由于某种原因,我正在使用外部属性源,其中一个外部属性源没有自动装配,在创建身份验证 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;
}
}
解决方案
您正在尝试访问@Autowired
构造函数中的属性。在此阶段无法自动装配该属性。为了让 Spring “烘焙 bean”,Spring 必须创建您的对象(使用您的构造函数),然后才应用自动装配机制来注入emailPropertyConfig
和cipherCrypt
. 因此,您不能@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;
}
}
推荐阅读
- python-3.x - Python .txt 文件无法正确读取
- django - 从 ManyToManyField 填充 MultipleChoiceField 值
- python - 如何在同一行打印这些字符?
- python - 将单词传递给函数
- c# - `Clicked` 和 `Command` 在同一个 Xamarin 视图上使用时如何工作?
- laravel - 如何将 PHPMailer 连接转换为 Laravel .env?
- excel - PowerShell - Import Excel then Export CSV without using Excel or COM
- machine-learning - 通过了解一些未来事件来增强预测
- javascript - 从 JSON 响应中删除属性
- mysql - 在这种情况下如何对 COUNT() 求和