Spring @Value 字段为空
Spring @Value field is null
我在 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 中,我想从这个文件中注入字段。
我的 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("{general.key'}")
应该是:
@Value("{general.key}")
这个:
<value>classpath:config/local/*.properties</value>
应该是:
<value>classpath:*.properties</value>
尝试以下操作:
@Value("#{general['general.key']}")
EntityEncryptionConverter bean 是否与 PropertyPlaceholderConfigurer 在相同的 Spring 上下文中? Post 处理器只在同一上下文中修饰 bean。
编辑
正如我们所讨论的,您在 JPA 中获得的 EntityEncryptionConverter 实例不是由 Spring 的组件扫描创建的 Spring 托管实例。因此,keyCode 上没有设置任何值。
我想你在这里有几个选择,none 我觉得非常 "clean"。
不要使用 Spring 在转换器中获取 keyCode 值。在 Converter 中,通过 Java Properties.
从老式属性文件中读取 keyCode 值
您想使用Spring,好的...然后创建一个Application Context aware Converter。您可以按照 here 所述创建 AppContext 提供程序。使用该提供程序,您可以在 Spring 中查找 keyCode(可能必须将其公开为 String bean),或者您可以查找 Spring 管理的 EntityEncryptionConverter 实例,然后将转换器方法委托给那个实例。
我在 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 中,我想从这个文件中注入字段。 我的 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("{general.key'}")
应该是:
@Value("{general.key}")
这个:
<value>classpath:config/local/*.properties</value>
应该是:
<value>classpath:*.properties</value>
尝试以下操作:
@Value("#{general['general.key']}")
EntityEncryptionConverter bean 是否与 PropertyPlaceholderConfigurer 在相同的 Spring 上下文中? Post 处理器只在同一上下文中修饰 bean。
编辑
正如我们所讨论的,您在 JPA 中获得的 EntityEncryptionConverter 实例不是由 Spring 的组件扫描创建的 Spring 托管实例。因此,keyCode 上没有设置任何值。
我想你在这里有几个选择,none 我觉得非常 "clean"。
不要使用 Spring 在转换器中获取 keyCode 值。在 Converter 中,通过 Java Properties.
从老式属性文件中读取 keyCode 值
您想使用Spring,好的...然后创建一个Application Context aware Converter。您可以按照 here 所述创建 AppContext 提供程序。使用该提供程序,您可以在 Spring 中查找 keyCode(可能必须将其公开为 String bean),或者您可以查找 Spring 管理的 EntityEncryptionConverter 实例,然后将转换器方法委托给那个实例。