我尝试启动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) 我正在将 Java 代码转换为 C#。
Java 代码将应用程序中的 3 个关键数据写入和读取到.properties文件中。这甚至可能持续数百行。
Properties prop = new Properties();
我查看了 .properties 文件的几个 C# 替代品,并提出了 3 种可能性。
.settings
.ini
.config
从我对这 3 种文件类型的不存在经验来看,我认为.settings是我需要的。
如果我错了,请纠正我,并提供一些有关如何使用最佳文件类型的信息。(我已经研究了对 .settings 文件的读取和写入,并且没什么可做的)
谢谢大家!
在@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) 我希望能够在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) 我试过用
@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可以包含属性文件中指定的值或没有空白案例的项目.
如果属性消息是硬编码的,则编写的代码工作正常。相反,我想将动态数据作为参数传递并获取消息。我开发的代码是:
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) 我有 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) 我有一个在此类中创建的类,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创建的?
一些背景信息:
我经常读到 .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) 我正在阅读一个包含欧元符号的属性文件.当它在屏幕上打印时看起来完全不同.我比较了来自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) properties-file ×10
java ×8
spring ×4
spring-boot ×2
c# ×1
encoding ×1
escaping ×1
java-8 ×1
java-ee ×1
javabeans ×1
json ×1
settings ×1
spring-mvc ×1
timex ×1