【问题标题】:SpringMVC with javaconfig PropertySource failed带有 javaconfig PropertySource 的 SpringMVC 失败
【发布时间】:2017-02-24 02:44:45
【问题描述】:

代码如下:

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();
    }
}

项目结构

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

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

【问题讨论】:

    标签: java spring


    【解决方案1】:

    尝试使用以下

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

    并确保文件位于类路径中(在您的资源文件夹下)。

    【讨论】:

    • 是的,当我将 classpath* 更改为 classpath 时,问题就解决了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2014-03-14
    • 2015-10-02
    • 2018-02-18
    • 2014-10-21
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多