标签: properties-file

java.io.FileNotFoundException:无法打开类路径资源[timex-servlet.properties],因为它不存在

我尝试启动Spring Web应用程序时收到以下错误消息:

2012-04-12 13:53:20,491 ERROR [org.springframework.web.servlet.DispatcherServlet] - 

Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:172)
Run Code Online (Sandbox Code Playgroud)

我正在通过eclipse运行Tomcat(版本6.x).我尝试将timex-servlet.properties放在以下目录中,但无济于事:

WebContent\WEB-INF
WebContent\WEB-INF\classes
WebContent\
Run Code Online (Sandbox Code Playgroud)

以下是timex-servlet.xml中timex-servlet.properties的引用:

    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="timex-servlet.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)

有几个SO线程处理相同的消息,表示放置类路径:在属性文件引用前面.所以我尝试了以下,但也没有用:

        <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="classpath:timex-servlet.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)

spring filenotfoundexception properties-file timex

2
推荐指数
1
解决办法
5万
查看次数

Java .properties 文件和 C# .settings 文件

我正在将 Java 代码转换为 C#。

Java 代码将应用程序中的 3 个关键数据写入和读取到.properties文件中。这甚至可能持续数百行。

Properties prop = new Properties();

我查看了 .properties 文件的几个 C# 替代品,并提出了 3 种可能性。

.settings .ini .config

从我对这 3 种文件类型的不存在经验来看,我认为.settings是我需要的。

如果我错了,请纠正我,并提供一些有关如何使用最佳文件类型的信息。(我已经研究了对 .settings 文件的读取和写入,并且没什么可做的)

谢谢大家!

c# java settings properties-file

2
推荐指数
1
解决办法
7726
查看次数

Spring没有加载属性

在@Configuration类中引用applicationContext.xml时Spring不加载属性

@Configuration
@ImportResource("classpath:applicationContext.xml")
public class SpringConfig {

  @Autowired
    private MyBean1 myBean1;

  @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();

    }

  @Bean(name = "myBean2")
    public MyBean2 myBean2() {
    MyBean2 bean2 = new MyBean2();
    return bean2;
    }


}
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">           
                <value>classpath:test.properties</value>           
        </property>
    </bean>

  <bean id="myBean1" class="com.foo.bar.MyBean1">
        <property name="name" value="${name}" />  
        <property name="age" value="${age}" />  
    </bean>
Run Code Online (Sandbox Code Playgroud)

Test.properties :(在类路径中)

name=foo
age=10
Run Code Online (Sandbox Code Playgroud)

当我使用以下代码行加载applicationContext.xml文件时,它工作正常:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Run Code Online (Sandbox Code Playgroud)

我看到印有以下几行:

2015-05-30 10:01:08.277 [INFO] org.springframework.beans.factory.config.PropertyPlaceholderConfigurer:172 - Loading properties file from class …
Run Code Online (Sandbox Code Playgroud)

java spring properties-file

2
推荐指数
1
解决办法
1万
查看次数

Scala Play 配置文件中的转义字符

我希望能够在application.conf我的 Web 应用程序中将属性定义为 json 字符串,如下所示:

prop1 = "{key1:value1 , key2:value2}"
Run Code Online (Sandbox Code Playgroud)

但是,在我的情况下,值中有双引号,并且 '\' 不能用作转义字符。如何在 prop1 中声明 json,其中包含双引号,例如

prop1 = "{key1 : \"value1\", key2:\"value2\" }"
Run Code Online (Sandbox Code Playgroud)

escaping properties-file playframework

2
推荐指数
1
解决办法
1990
查看次数

什么是在Spring中将属性文件值转换为字符串集的有效方法

我试过用

@Value("#{'${white.listed.hotel.ids}'.split(',')}")
private Set<String> fareAlertwhiteListedHotelIds;
Run Code Online (Sandbox Code Playgroud)

但是当white.listed.hotel.ids为空时,还设置了一个带空白值的大小.

white.listed.hotel.ids =

有人请帮我一个版本,其中whiteListedHotelIds可以包含属性文件中指定的值或没有空白案例的项目.

java spring properties-file java-8 spring-boot

2
推荐指数
1
解决办法
430
查看次数

如何使用spring消息PropertyPlaceholderConfigurer传递参数?

如果属性消息是硬编码的,则编写的代码工作正常。相反,我想将动态数据作为参数传递并获取消息。我开发的代码是:

public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer {

private static Map<String, String> propertiesMap;
// Default as in PropertyPlaceholderConfigurer
private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; //Check system properties if not resolvable in the specified properties.

static final Logger logger = LogManager.getLogger(SpringPropertiesUtil.class);

@Override
public void setSystemPropertiesMode(int systemPropertiesMode) {
    super.setSystemPropertiesMode(systemPropertiesMode);
    springSystemPropertiesMode = systemPropertiesMode;
}

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
    super.processProperties(beanFactory, props);

    propertiesMap = new HashMap<String, String>();
    for (Object key : props.keySet()) {
        String keyStr = key.toString();
        String valueStr = resolvePlaceholder(keyStr, …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc properties-file

2
推荐指数
1
解决办法
3655
查看次数

如何在java中将JSON转换为属性文件?

我有 JSON 字符串,想要转换为 java 属性文件。注意:JSON 可以是 JSON 字符串、对象或文件。示例 JSON:

{
    "simianarmy": {
        "chaos": {
            "enabled": "true",
            "leashed": "false",
            "ASG": {
                "enabled": "false",
                "probability": "6.0",
                "maxTerminationsPerDay": "10.0",
                "IS": {
                    "enabled": "true",
                    "probability": "6",
                    "maxTerminationsPerDay": "100.0"
                }
            }
        }
    }
}

 **OUTPUT SHOULD BE:-**
simianarmy.chaos.enabled=true
simianarmy.chaos.leashed=false
simianarmy.chaos.ASG.enabled=false
simianarmy.chaos.ASG.probability=6.0
simianarmy.chaos.ASG.maxTerminationsPerDay=10.0
simianarmy.chaos.ASG.IS.enabled=true
simianarmy.chaos.ASG.IS.probability=6
simianarmy.chaos.ASG.IS.maxTerminationsPerDay=100.0
Run Code Online (Sandbox Code Playgroud)

java json properties-file

2
推荐指数
1
解决办法
1万
查看次数

在非 spring 注入类中使用 application.properties

我有一个在此类中创建的类,new B(this);BI 希望使用 application.properties 中的值。但是因为(据我所知)因为它不是用 Spring 创建的,所以不会有任何注入(我使用注释@Value

这就是类的创建方式:

@Component
public class A {

    public B getB(){return new B(this);}

    /**
        a lot more stuff
    **/
}
Run Code Online (Sandbox Code Playgroud)

有问题的班级:

public class B {

    private A a;

    public B(A a){
        this.a = a;
    }

    @Value("${threshold}")
    private String threshold;  //this is null because it's not created with Spring

    public String getThreshold(){
        return threshold;
    }
    /**
        a lot more stuff
    **/

}
Run Code Online (Sandbox Code Playgroud)

所以我的问题如下:

1)如何使用 application.properties 文件中的值或

2)B怎么写,它是用Spring创建的?

一些背景信息:

  • 最初的代码不是我写的,所以不想改变太多,但想修改一下,以便将来可以更好地维护
  • 我没有那么多 Spring 知识,只是开始越来越熟悉它。 …

java javabeans properties-file spring-boot

2
推荐指数
1
解决办法
9096
查看次数

在 java 9 之前,.properties 文件必须在 ISO-8859-1 中编码真的是真的吗?

我经常读到 .properties 文件应该/必须在 Java 9 之前以 ISO-8859-1 编码。我从来没有遇到过以 UTF-8 加载和存储 .properties 的问题。还是建议 Resource Bundle 文件只使用 8859-1 ?

public static void utf8_test()
{
    Path pfile    = Paths.get("C:\\temp_\\properties_utf8.properties");
    Path pfileOut = Paths.get("C:\\temp_\\properties_utf-8_out.properties");
    Properties props = new Properties();

    try ( BufferedReader br = Files.newBufferedReader(pfile,StandardCharsets.UTF_8);
          BufferedWriter bw = Files.newBufferedWriter(pfileOut,StandardCharsets.UTF_8)
        )
    {
        props.load(br);
        props.setProperty("test","test prop;??????? ?? ????????;öüµäß@;Euro sign;€");
        props.list(System.out);

        props.store(bw,"comment");

    } catch (Exception e) { e.printStackTrace(); }
} //--- end

The input file is:
foo = aaa
utf_test = ???????
bar = bbb

The output …
Run Code Online (Sandbox Code Playgroud)

java encoding properties-file

2
推荐指数
1
解决办法
499
查看次数

欧元符号未在适当的文件中显示正确

我正在阅读一个包含欧元符号的属性文件.当它在屏幕上打印时看起来完全不同.我比较了来自props文件的字符串,并使用equals方法声明另一个带有相同文本的字符串,这是假的.Pls可以有人帮助我.

properities file
your purchase order is €

string text="your purchase order is €";

on comparing the above strings it fails.

************************
public String getProperty(String arg0) {
        Properties prop = new Properties();
        InputStream input = null;


            try {
                input = new FileInputStream("C:/text.properties");

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {

                prop.load(input);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return prop.getProperty(arg0);

    }
Run Code Online (Sandbox Code Playgroud)

java character-encoding java-ee properties-file

1
推荐指数
1
解决办法
1159
查看次数