【发布时间】:2013-01-24 15:37:49
【问题描述】:
使用 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?
【问题讨论】:
-
在制作控制器
@Configuration类之前我会三思而后行 -@Configuration类的处理方式与普通 spring bean 不同,很难预测它如何影响 bean 生命周期和行为。
标签: spring spring-mvc