J'essaie d'apprendre les bases du cadre de printemps. J'utilise le org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
dans le sping.xml pour imprimer les propriétés du PointA mais je reçois l'erreur ci-dessous.
Classe Triagle
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>
fichier pointsconfig.properties
PointA.pointX=0
PointA.pointY=0
Erreur
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
Déplacez le fichier pointsconfig.properties
dans le répertoire des ressources.
Votre problème est que vous essayez d'accéder à un fichier du package org.stack
. Vous devez donc donner le chemin complet à Spring
comme suit:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:/org/stack/pointsconfig.properties"/>
</bean>
Essayez de déplacer le fichier pointsconfig.properties vers le répertoire src. Cela devrait régler le problème.