Java 类路径资源 [pointsconfig.properties] 无法打开,因为它不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37544835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
class path resource [pointsconfig.properties] cannot be opened because it does not exist
提问by Mr Asker
I am trying to learn the basic of the Spring framwork. I am using the org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
in the sping.xml to print the properties of the PointA but I am getting the error below.
我正在尝试学习 Spring 框架的基础知识。我org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
在 sping.xml 中使用 来打印 PointA 的属性,但出现以下错误。
Triagle class
三角形类
package org.stack;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Triangle {
private Point PointA;
private Point PointB;
private Point PointC;
private ApplicationContext context = null;
public Point getPointA() {
return PointA;
}
public void setPointA(Point pointA) {
PointA = pointA;
}
public Point getPointB() {
return PointB;
}
public void setPointB(Point pointB) {
PointB = pointB;
}
public Point getPointC() {
return PointC;
}
public void setPointC(Point pointC) {
PointC = pointC;
}
public void draw() {
System.out.println("Point A = (" + getPointA().getX() + ", "
+ getPointA().getY() + ")");
System.out.println("Point B = (" + getPointB().getX() + ", "
+ getPointB().getY() + ")");
System.out.println("Point C = (" + getPointC().getX() + ", "
+ getPointC().getY() + ")");
}
}
DrawingApp
绘图应用程序
package org.stack;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.draw();
}
}
spring.xml
弹簧文件
</bean>
<!-- We intialize here 3 Points objects -->
<bean id="pointA" class="org.stack.Point">
<property name="x" value="${PointA.pointY}" />
<property name="y" value="${PointA.pointY}" />
</bean>
<bean id="pointB" class="org.stack.Point">
<property name="x" value="-20" />
<property name="y" value="0" />
</bean>
<bean id="pointC" class="org.stack.Point">
<property name="x" value="20" />
<property name="y" value="0" />
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="pointsconfig.properties"></property>
</bean>
pointsconfig.properties file
pointsconfig.properties 文件
PointA.pointX=0
PointA.pointY=0
error
错误
Mai 31, 2016 1:25:24 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFORMATION: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@619a5dff: startup date [Tue May 31 13:25:24 CEST 2016]; root of context hierarchy
Mai 31, 2016 1:25:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFORMATION: Loading XML bean definitions from class path resource [spring.xml]
Mai 31, 2016 1:25:25 PM org.springframework.beans.factory.config.PropertyPlaceholderConfigurer loadProperties
INFORMATION: Loading properties file from class path resource [pointsconfig.properties]
Mai 31, 2016 1:25:25 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNUNG: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [pointsconfig.properties] cannot be opened because it does not exist
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [pointsconfig.properties] cannot be opened because it does not exist
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:89)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.stack.DrawingApp.main(DrawingApp.java:14)
Caused by: java.io.FileNotFoundException: class path resource [pointsconfig.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80)
... 7 more
采纳答案by TheBook
Move the pointsconfig.properties
file to the resources directory.
将pointsconfig.properties
文件移动到资源目录。
回答by Nicolas Filotto
Your problem is that you try to access to a file in the package org.stack
so you need to give the full path to Spring
as next:
您的问题是您尝试访问包中的文件,org.stack
因此您需要提供完整路径,Spring
如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:/org/stack/pointsconfig.properties"/>
</bean>
回答by Dilip Balu
Try moving the pointsconfig.properties file to src directory. It should fix the issue.
尝试将 pointsconfig.properties 文件移动到 src 目录。它应该解决这个问题。
回答by Dilip Kumar
<context:property-placeholder location="classpath:deep/com/sport.properties" />
<bean id ="myTeam" class ="deep.com.CricketTeam">
<property name="emailAddr" value="${foo.email}"/>
<property name="teamName" value="${foo.team}"/>
</bean>