无法在Spring PropertyPlaceHolderConfigurer中加载属性错误

时间:2018-08-04 15:07:02

标签: spring

我使用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

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

例外很简单。它说

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中提供相同路径的正确路径。