SpringPropertyPlaceHolderConfigurer中出现无法加载属性错误

4sup72z8  于 2023-02-18  发布在  Spring
关注(0)|答案(3)|浏览(133)

我有下面的代码,使用一个PropertyPlaceHolderConfigurer

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

如何解决此问题?

yc0p9oo0

yc0p9oo01#

您应该将应用程序上下文放在src/main/resources/com/nt/cfgs/applicationContext.xml中,然后尝试仅使用“com/nt/cfgs/applicationContext.xml”阅读它...您的类不会从项目的顶部执行,而是从目标文件夹中执行-因此它无法看到您指定的相对于“target”的路径。
或者更好的方法是使用类路径上下文读取器和路径“classpath:com/nt/cfgs/applicationContext.xml”...

uttx8gqw

uttx8gqw2#

例外很简单。它说

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中提供相同文件的正确路径,无论您在何处定义它。

6ioyuze2

6ioyuze23#

我面临着同样的问题。xml文件,它浪费了我很多时间,最终我按照这些步骤来解决它:
1.删除您的项目(或多个相关项目)及其子项目(如果有的话)从工作区(不要删除它从您的计算机磁盘)。
1.再次导入项目,我的是从git,所以我做了“从GIT导入”
1.导入完成后,从pom.xml进行全新安装
1.然后尝试运行主类。
可能有更好的解决方案,但这解决了我的问题,可能也会帮助其他人。

相关问题