Java 配置属性资源占位符配置器/资源文件夹的最佳方式是什么?

Java 配置属性资源占位符配置器/资源文件夹的最佳方式是什么?,java,spring,Java,Spring,我有一个(gradle/java/eclipse)项目,在我看来它是一个相当标准的文件夹结构 ...main java ...controller ...service resources webapp ...resources WEB-INF 我有一个问题,我只是不明白,虽然我已经在一个非常混乱的方式解决它。如果我在WebMVCConfigureAdapter的组件扫描中指定controllers文件夹,则服务类无法使用已配置的PropertySourcesPla

我有一个(gradle/java/eclipse)项目,在我看来它是一个相当标准的文件夹结构

...main
  java
  ...controller
  ...service
  resources
  webapp
  ...resources
  WEB-INF
我有一个问题,我只是不明白,虽然我已经在一个非常混乱的方式解决它。如果我在WebMVCConfigureAdapter的组件扫描中指定controllers文件夹,则服务类无法使用已配置的PropertySourcesPlacePlaceHolderConfigurer bean获取属性。如果我扩展了组件扫描出的jsp文件不拿起css包括

因此,对于这个配置类,一切都很好,但是在服务实现类中没有解析属性

配置类

@EnableWebMvc
@Configuration
@ComponentScan({ "comm.app.controller" })
@PropertySource({ "classpath:app.properties" }) 
public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

//used to handle html put and delete rest calls
@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {
    CommonsMultipartResolver resolver=new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

服务实现类开始

@Service("appService")
@PropertySource({ "classpath:app.properties" })
public class appServiceImpl implements appService {

private RestTemplate restTemplate = new RestTemplate();

@Value("${property.reference}")
private String propref; 

   ...
在本例中,${property.reference}未被选取,但视图页面样式(从…webapp\resources…css选取)是正常的

如果我改变

  @ComponentScan({ "comm.app.controller" }) 

属性被拾取(可能是因为propertyConfig bean进入范围?),但是本地样式文件找不到?对文件webapp\resources的任何链接引用。css失败了

最后我找到了一个糟糕的解决办法

1) 保留@ComponentScan({“comm.app.controller”})的作用域

2) 正在对serviceimplementation类进行黑客攻击,以便

@Configuration
@Service("appService")
@PropertySource({ "classpath:app.properties" })
public class appServiceImpl implements appService {

private RestTemplate restTemplate = new RestTemplate();


@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Value("${property.reference}")
private String propref; 

   ...
有人能告诉我,我是否配置了资源文件的路径不正确,或者是否应该对PropertyConfigBean执行不同的操作?(可能是注入它或在不同的配置文件中声明另一个?)

@Configuration
@Service("appService")
@PropertySource({ "classpath:app.properties" })
public class appServiceImpl implements appService {

private RestTemplate restTemplate = new RestTemplate();


@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Value("${property.reference}")
private String propref; 

   ...