Вы пытаетесь получить доступ к свойству @Autowired
в конструкторе. На этом этапе свойство не может быть подключено автоматически. Чтобы Spring «запек боб», 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;
}
}