#java #spring-boot
Вопрос:
Когда я вызываю метод llaveCom.getName (), я всегда получаю значение null, я не знаю, почему
Код компонента»
@Component
@ConfigurationProperties("app")
public class LlaveCompo {
private String name;
private String llave;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLlave() {
return llave;
}
public void setLlave(String llave) {
this.llave = llave;
}
}
Значения, которые я хочу получить
application.properties
app.name=hola
app.llave=fE3tTFvoRd#PYDLoXdIq4ytJJP#ym%Mw
Сервис от я внедряю компонент:
@Autowired
private LlaveCompo llaveCom;
private static final String AES = "AES";
@Value("app.llave")
private String secret = "fE3tTFvoRd#PYDLoXdIq4ytJJP#ym%Mw" ;
private final Key key;
private final Cipher cipher;
public CryptoService() throws Exception{
System.out.println(llaveCom.getName());
System.out.println("llave: " llaveC);
key = new SecretKeySpec(secret.getBytes(), AES);
cipher = Cipher.getInstance(AES);
}
журналы, описывающие ошибки:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServic': Unsatisfied dependency expressed through field 'cryptoService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoService' defined in file [C:UsersECE-LDDocumentsNetBeansProjectsPruebaCripto2targetclassescomexamplePruebaCriptoservicesCryptoService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.PruebaCripto.services.CryptoService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServic': Unsatisfied dependency expressed through field 'cryptoService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoService' defined in file [C:UsersECE-LDDocumentsNetBeansProjectsPruebaCripto2targetclassescomexamplePruebaCriptoservicesCryptoService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.PruebaCripto.services.CryptoService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoService' defined in file [C:UsersECE-LDDocumentsNetBeansProjectsPruebaCripto2targetclassescomexamplePruebaCriptoservicesCryptoService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.PruebaCripto.services.CryptoService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.PruebaCripto.services.CryptoService]: Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
Комментарии:
1. возможно, @ConfigurationProperties(префикс = «приложение») и @EnableConfigurationProperties(LlaveCompo.class) в основном классе?
Ответ №1:
Вы должны использовать инъекцию конструктора. И поскольку у вас уже есть Llaveompo, вам не нужно иметь @Value для секрета.
private final LlaveCompo llaveCom;
private static final String AES = "AES";
private final Key key;
private final Cipher cipher;
public CryptoService(LlaveCompo llaveCom) throws Exception{
this.llaveCom = llaveCom;
System.out.println(llaveCom.getName());
System.out.println("llave: " llaveC);
key = new SecretKeySpec(llaveCom.getLlave().getBytes(), AES);
cipher = Cipher.getInstance(AES);
}