spring - 在 Spring 中使用 @PropertyResource 访问多个属性文件

使用 Spring 3.1 中新增的 @PropertySource 注解,如何通过 Environment 访问多个属性文件?

目前我有:

@Controller
@Configuration 
@PropertySource(
    name = "props",
    value = { "classpath:File1.properties", "classpath:File2.properties" })
public class TestDetailsController {


@Autowired
private Environment env;
/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {

    String file1Name = env.getProperty("file1.name","file1.name not found");
            String file2Name = env.getProperty("file2.name","file2.name not found");

            System.out.println("file 1: " + file1Name);
            System.out.println("file 2: " + file2Name);

    return "home";
}


结果是 File1.properties 中的正确文件名,但未找到 file2.name。如何访问File2.properties

最佳答案

如果您可以迁移到 Spring 4.x,那么新的 @PropertySources 已经解决了问题。注释:

@PropertySources({
        @PropertySource("/file1.properties"),
        @PropertySource("/file2.properties")
})

https://stackoverflow.com/questions/14505078/

相关文章:

java - @PostConstruct 注解和spring生命周期

java - 返回一个列表,我已经有一个rowmapper实现

spring - 注释驱动配置spring时如何为bean设置init-method?

hibernate - java.lang.VerifyError : class net. sf.

java - 自定义 validator 消息 : Throwing exception in im

spring - 如何在 Spring 中通过 XML 定义 MySql 数据源 bean

java - 如何使用modelAttribute在ajax(jquery)中提交spring表单

java - 如何使用 Spring Data Pagination 在一页中获取所有结果

java - 如何访问 Thymeleaf 模板中的系统属性?

json - 406 Spring MVC Json,根据请求 "accept"headers No