【发布时间】:2018-10-12 09:36:05
【问题描述】:
我有一个位于 tomcat 文件夹中的属性文件。我正在尝试如下读取属性文件内容。但我得到的属性值为空值。
文件路径没有任何问题。
@ComponentScan(basePackages = "org.sakaiproject.log.api")
@Configuration
@PropertySource(name = "props", value = {
"file:/home/tomcat-sakai-7.0.55/sakai/local.properties",
"file:/home/tomcat-sakai-7.0.55/sakai/sakai.properties" })
public class SpringCryptoContext {
@Value("${aes.encryption.cipherString}")
private static String cyperString;
public SpringCryptoContext() {
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(
SpringCryptoContext.class);
System.out.println(cyperString);
}
}
编辑:
我创建了单独的类并按如下方式加载属性。
@Service
@PropertySource(name = "locations", value = {
"file:/home/nirmala/projects/iml/tomcat-sakai-7.0.55/sakai/local.properties",
"file:/home/nirmala/projects/iml/tomcat-sakai-7.0.55/sakai/sakai.properties" })
public class CryptoToolImpl implements CryptoTool {
private Cipher cipher = null;
private Key key = null;
@Value("${pii.encryption.cipherString}")
private String cipherString = "";
public static final Logger log = Logger.getLogger(CryptoToolImpl.class);
public IMLCryptoToolImpl() {
try {
fixKeyLength();
cipher = Cipher.getInstance(cipherString);
key = KMSKeyStoreSingleton.getInstance().getPrivateKey();
} catch (Exception e) {
log.error("Error in initializing CryptoToolImpl : "
+ e.getMessage());
}
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
但我收到以下错误
初始化 CryptoToolImpl 时出错:转换格式无效:
【问题讨论】:
-
您确定运行tomcat的用户对文件有读取权限吗?
-
@Evgeni Dimitrov 是的文件具有读取权限
-
您不能在
static字段上使用@Value。
标签: java spring properties