gpt4 book ai didi

java - Spring @Value 字段为空

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:28 26 4
gpt4 key购买 nike

我在 Spring 中遇到 @Value 注释问题,我正在尝试从属性文件中设置字段。我的配置applicationContext.xml是

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:config/local/*.properties</value>
</list>
</property>
</bean>

属性文件位于 src/main/resources/config/local/general.properties

一般属性

general.key="EDC183ADVARTT"

在我的类(class)中,我想从该文件注入(inject)字段。我的类(class)

import java.security.Key;

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec;
import javax.persistence.AttributeConverter;

import org.postgresql.util.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;

@javax.persistence.Converter
@Component
public class EntityEncryptionConverter implements AttributeConverter<String, String> {

@Value("${general.key}")
private String keyCode;

private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static final byte[] KEY = "395DEADE4D23DD92".getBytes();

public String convertToDatabaseColumn(String ccNumber) {
System.out.print(keyCode);
// do some encryption
Key key = new SecretKeySpec(KEY, "AES");
try {
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeBytes(c.doFinal(ccNumber.getBytes()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public String convertToEntityAttribute(String dbData) {
// do some decryption
Key key = new SecretKeySpec(KEY, "AES");
try {
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
return new String(c.doFinal(Base64.decode(dbData)));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

为什么我的 KeyCode 值为空?

最佳答案

这个:

<value>classpath:config/local/*.properties</value>

应该是:

 <value>classpath:*.properties</value>

关于java - Spring @Value 字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28504150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com