spring mvc中的属性文件

Sri*_*nan 1 java spring spring-mvc

我有一个带键值对的属性文件:

key1=value1
key2=value2
Run Code Online (Sandbox Code Playgroud)

现在,在我的控制器中,我想直接打印属性文件的值(当然在使用web.xml/app-servlet.xml加载属性文件之后),如:

System.out.printl(${key1});
Run Code Online (Sandbox Code Playgroud)

有可能吗?

如果没有,我想创建一个包含所有常量变量的接口,以从属性文件中读取值.我该怎么做??

public interface MyConstants
{
      @Value("${key1}")
      public static final KEY_1="";
}
Run Code Online (Sandbox Code Playgroud)

但正如预期的那样,只分配了空字符串.

我该如何解决这个问题?或者,使用属性文件检索值的最佳方法是什么?提前致谢...

Aru*_*nan 5

为"MyConstants"而不是类创建接口的原因有两个:

1)Spring无法将值注入到没有实现的接口.仅仅因为你无法实例化界面.请记住,Spring只是一个工厂,它只能与可以实例化的"事物"一起玩.

2)另一个原因是拥有一个用于存储常量的接口本身就是一种反模式.这不是为什么设计接口.您可能想要引用Constant接口反模式.

http://en.wikipedia.org/wiki/Constant_interface