带有javaconfig PropertySource的SpringMVC失败了

时间:2017-02-24 02:44:46

标签: java spring

代码如下:

public class RootInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}


@Configuration
@ComponentScan(basePackages = "com.wasu.accounting.service")
@PropertySource(value = "classpath*:/test.properties")
public class RootConfig {
 @Bean
    public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
        return new PropertyPlaceholderConfigurer();
    }
}

项目结构

project structure

当我启动tomcat时,会出现以下错误:

  

class org.springframework.web.context.ContextLoaderListener   org.springframework.beans.factory.BeanDefinitionStoreException:失败   解析配置类[com.test.accounting.config.RootConfig];   嵌套异常是java.io.FileNotFoundException:无法打开   ServletContext资源[/ classpath *:/ test.properties]

1 个答案:

答案 0 :(得分:1)

尝试使用以下

@PropertySource(value = "classpath:/test.properties")

并确保该文件位于类路径上(在资源文件夹下)。