无法加载spring PropertyPlaceHolderConfigure中的属性错误

无法加载spring PropertyPlaceHolderConfigure中的属性错误,spring,Spring,我使用PropertyPlaceHolderConfigure,得到了以下代码: public static void main(String[] args) { Scanner sc=null; CustomerDTO dto=null; //create IOC Container ApplicationContext ctx=new FileSystemXmlApplicationContext("src/main/java/com/nt/c

我使用
PropertyPlaceHolderConfigure
,得到了以下代码:

public static void main(String[] args) {        
    Scanner sc=null;
    CustomerDTO dto=null;
    //create IOC Container
    ApplicationContext ctx=new FileSystemXmlApplicationContext("src/main/java/com/nt/cfgs/applicationContext.xml");
    //get PropertyPlaaceHolderConfigurer OBJ
    BeanFactoryPostProcessor bfpp=ctx.getBean("ppc",PropertyPlaceholderConfigurer.class);
    bfpp.postProcessBeanFactory((ConfigurableListableBeanFactory)ctx);

    sc=new Scanner(System.in);
    System.out.println("Enter Customer NO: ");
    int cno=sc.nextInt();
    System.out.println("Enter Customer Name: ");
    String cname=sc.next();
    System.out.println("Enter Principal Amount:: ");
    float pamt=sc.nextFloat();
    System.out.println("Enter Rate of Interest: ");
    float rate=sc.nextFloat();
    System.out.println("Enter Time/Period: ");
    float time=sc.nextFloat();

    //set values to dto
    dto=new CustomerDTO();
    dto.setCno(cno);
    dto.setCname(cname);
    dto.setPamt(pamt);
    dto.setRate(rate);
    dto.setTime(time);

    //getBean
    LoanService service=ctx.getBean("service",LoanService.class);
    System.out.println(service.calcIntrAmt(dto));       
}
上面的代码给出了以下错误:

Aug 04, 2018 8:16:08 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@621be5d1: startup date [Sat Aug 04 20:16:08 IST 2018]; root of context hierarchy
Aug 04, 2018 8:16:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [D:\NTSP710\IOC-Beans28ApplicationContextWithPropertyFileConfigurator\src\main\java\com\nt\cfgs\applicationContext.xml]
Aug 04, 2018 8:16:08 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: src\main\java\com\nt\commons\DBDetails.properties
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: src\main\java\com\nt\commons\DBDetails.properties
    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:164)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:142)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:85)
    at com.nt.test.ClientApp.main(ClientApp.java:21)
Caused by: java.io.FileNotFoundException: src\main\java\com\nt\commons\DBDetails.properties
    at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:129)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:162)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80)
    ... 7 more
2018年8月4日晚上8:16:08 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息:刷新org.springframework.context.support。FileSystemXmlApplicationContext@621be5d1:启动日期[2018年8月4日星期六20:16:08 IST];上下文层次结构的根
2018年8月4日晚上8:16:08 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息:从文件[D:\NTSP710\IOC-Beans28ApplicationContextWithPropertyFileConfigurator\src\main\java\com\nt\cfgs\applicationContext.XML]加载XML bean定义
2018年8月4日晚上8:16:08 org.springframework.context.support.AbstractApplicationContext刷新
警告:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常为java.io.FileNotFoundException:src\main\java\com\nt\commons\DBDetails.properties
线程“main”org.springframework.beans.factory.BeanInitializationException中出现异常:无法加载属性;嵌套异常为java.io.FileNotFoundException:src\main\java\com\nt\commons\DBDetails.properties
位于org.springframework.beans.factory.config.PropertyResourceConfiger.postProcessBeanFactory(PropertyResourceConfiger.java:89)
位于org.springframework.context.support.postprocessorregistrationlegate.invokeBeanFactoryPostProcessors(postprocessorregistrationlegate.java:284)
位于org.springframework.context.support.postprocessorregistrationlegate.invokeBeanFactoryPostProcessors(postprocessorregistrationlegate.java:164)
位于org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
位于org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:142)
位于org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:85)
位于com.nt.test.ClientApp.main(ClientApp.java:21)
原因:java.io.FileNotFoundException:src\main\java\com\nt\commons\DBDetails.properties
位于org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:129)
位于org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
位于org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
位于org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
位于org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:162)
位于org.springframework.beans.factory.config.propertyresourceconfig.postprocessebeanfactory(propertyresourceconfig.java:80)
... 还有7个

如何解决此问题?

您应该将应用程序上下文放在src/main/resources/com/nt/cfgs/applicationContext.xml中,然后尝试使用“com/nt/cfgs/applicationContext.xml”来阅读它。。。您的类不会从项目的顶部执行,而是从目标文件夹中执行,因此它无法看到您相对于“目标”指示的路径


或者更好的是,使用类路径上下文读取器和路径“classpath:com/nt/cfgs/applicationContext.xml”…

异常很简单。上面说

Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: src\main\java\com\nt\commons\DBDetails.properties

这意味着它无法在给定的路径中找到
DBDetails.properties
文件。您需要在
applicationContext.xml
@PropertySource
中提供相同的正确路径,无论您在哪里定义它。

applicationContext.xml实际上在哪里?你能添加项目吗structure@SnigdhaMishra这回答了你的问题吗?