Java 如何在 Spring MVC 4 中设置全局配置/变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29187828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set global config/variable in Spring MVC 4?
提问by CL So
I am using Spring MVC 4.1.4
我正在使用 Spring MVC 4.1.4
I have some global settings to share in whole application
我有一些全局设置要在整个应用程序中共享
These setting should only be loaded when start the server
这些设置应该只在启动服务器时加载
I know I can use context-param
我知道我可以使用上下文参数
<context-param>
<param-name>configA</param-name>
<param-value>valueA</param-value>
</context-param>
<context-param>
<param-name>configB</param-name>
<param-value>valueB</param-value>
</context-param>
But I want store some complex object, like this
但我想存储一些复杂的对象,像这样
HashMap myConfig = new HashMap();
String[] cfgB={"b1", "b2"};
HashMap<String, String> cfgC=new HashMap<String, String>();
cfgC.put("C1", "1");
cfgC.put("C2", "2");
MyConfigD cfgD = new MyConfigD();
myConfig.put("configA", "A");
myConfig.put("configB",cfgB);
myConfig.put("configC",cfgC);
myConfig.put("configD",cfgD);
context-param is not possible to do that, what else I can use in Java or Spring?
context-param 不可能做到这一点,我还可以在 Java 或 Spring 中使用什么?
采纳答案by tinker
If you are not restricted and have the flexibility to decide how your properties are set, there are two options.
如果您不受限制并且可以灵活决定如何设置属性,则有两种选择。
First is to just define Beans in Java codein an @Configuration class. For most objects, you can have Beans that are @Autowired. All the beans are only loaded at runtime. For Maps (and Lists and such), you define them as beans and then access them with @Resource annotation. Note that you cannot access Maps with @Autowired, Spring uses @Resourcefor these types.
首先是在@Configuration 类中的Java 代码中定义Bean。对于大多数对象,您可以拥有@Autowired 的 Bean。所有 bean 仅在运行时加载。对于 Maps(和 Lists 等),您将它们定义为 bean,然后使用 @Resource 注释访问它们。请注意,您无法使用 @Autowired 访问 Maps,Spring对这些类型使用 @Resource。
Contrary to a comment on the other answer, I argue that settings can also be defined in code, just because it is written in XML doesn't make it any different, they are considered equivalent. By writing your config and settings in Java you get the power of OOP which is fantastic when you have complex configurations.
与对另一个答案的评论相反,我认为设置也可以在代码中定义,仅仅因为它是用 XML 编写的并没有使它有任何不同,它们被认为是等效的。通过用 Java 编写配置和设置,您可以获得 OOP 的强大功能,当您有复杂的配置时,这非常棒。
Example Bean declaration:
示例 Bean 声明:
@Bean
public MyConfig myConfig() {
final MyConfig myConfig = new MyConfig();
myConfig.put("configA", "A");
...
return myConfig;
}
@Bean
public Map<String, String> myMap() {
final Map<String, String> myMap = new HashMap<>();
myMap.put("A", "a");
return myMap;
}
Example usage:
用法示例:
@Autowired
private MyConfig myConfig;
@Resource(name = "myMap")
private Map<String, String> myMap;
Second is to use global system properties, defined in a propertiesfile. You can write properties in YAML or a map type config. Details in the link provided. EditYou can namespace your properties and then reference them using @Value annotation. You can have collectionsin YAML as well.
其次是使用在属性文件中定义的全局系统属性。您可以在 YAML 或地图类型配置中编写属性。提供的链接中的详细信息。 编辑您可以命名您的属性,然后使用 @Value 注释引用它们。您也可以在 YAML 中拥有集合。
application.properties
应用程序属性
myConfig.configA:A
myConfig.configB:B
myConfig.coll.cA:['a','b','c']
myConfig.coll.cB:{a:A,b:B,c:C}
...
In code
在代码中
@Value("${myConfig.configA}")
private String configA;
回答by Stefaan Neyts
You can just declare your maps with <util:map>
in the applicationContext.xml.
您可以<util:map>
在 applicationContext.xml 中声明您的地图。
Scope should be singleton
范围应该是单例
Type/class may be java.util.HashMap
类型/类可能是 java.util.HashMap
You can then @Autowired that bean in your components.
然后,您可以在组件中 @Autowired 那个 bean。
<util:map id="utilmap" map-class="java.util.HashMap">
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</util:map>