Skip to main content
 首页 » 编程设计

java之带有 javaconfig PropertySource 的 SpringMVC 失败

2023年09月11日36三少

代码如下:

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 时,出现以下错误:

class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.test.accounting.config.RootConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/classpath*:/test.properties]

请您参考如下方法:

尝试使用以下

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

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