【问题标题】:Spring read property file outside the class pathSpring读取类路径外的属性文件
【发布时间】: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


【解决方案1】:

请在您的配置类中添加以下 bean:-

//To resolve values using the @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigDev() {
    return new PropertySourcesPlaceholderConfigurer();
}

更新

静态在这种情况下不起作用,您需要执行以下操作:-

添加以下代码:-

@Value("${aes.encryption.cipherString}")
private String cyperString;

public String getCypherString(){
    return this.cyperString;
}

这只是一个示例,但您使用的是静态的,因为您在 main method(即 static)中访问它,并且为了访问变量,您将其标记为 static

问题是静态变量是一个类变量,当代码到达变量的System.out 行时,它没有属性,此时 - spring 仍在进行内部初始化等。

当您使用 spring 实例化时,您正在访问/使用该变量作为非 spring 框架。它必须是单向的

我建议有一个单独的类来将这些变量加载到上下文中,可能是标有@Component 或更具体地说是@Service 的某个类

【讨论】:

  • 仍然为空。
  • 你能检查一下你的财产钥匙的拼写吗
  • 文件路径和属性键没有问题
  • 我阅读了所有与spring属性文件读取相关的文章。在所有这些文章中,属性文件都定义在项目的类路径中。
  • @kakabali 仍然为空
【解决方案2】:

以下内容应该有所帮助。

@Configuration
@PropertySources(value={
    @PropertySource(value="file:${catalina.home}/sakai/local.properties"),
    @PropertySource(value="file:${catalina.home}/sakai/sakai.properties")
})
public class SpringCryptoContext {

    @Value("${aes.encryption.cipherString}")
    private String cyperString;
}
猜你喜欢
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 2014-05-29
  • 2019-03-04
  • 1970-01-01
  • 2021-01-05
  • 2013-08-30
  • 2013-07-14
相关资源
最近更新 更多