How to set global configuration / variable in Spring MVC 4?

I am using Spring MVC 4.1.4

I have global settings for general use.

These settings should only be loaded at server startup.

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 to 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 impossible to do, what else can I use in Java or Spring?

+6
source share
2 answers

If you are not limited and have the flexibility to determine how your properties are configured, there are two options.

First, you just need to define the Beans in Java code in the @Configuration class. For most objects, you can have Beans that are @Autowired. All Beans are loaded only at runtime. For Maps (and lists, etc.) you define them as Beans and then access them using the @Resource annotation. Note that you cannot access Maps with @Autowired, Spring uses @Resource for these types.

Unlike the comment on the other answer, I argue that settings can also be defined in the code, just because it is written in XML does not make it different, they are considered equivalent. By writing your preferences and settings in Java, you get the power of OOP, which is fantastic when you have a complex configuration.

Bean ad example:

 @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; } 

Usage example:

 @Autowired private MyConfig myConfig; @Resource(name = "myMap") private Map<String, String> myMap; 

Secondly, you should use the global system properties defined in the properties file. You can write properties in YAML or a map type configuration. Details in the provided link. Edit You can proxy your properties and then refer to them using the @Value annotation. You can have collections in 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; 
+7
source

You can simply declare your maps with <util:map> in applicationContext.xml.

The scale must be single

Type / class may be java.util.HashMap

Then you can @Autowired that bean in your components.

 <util:map id="utilmap" map-class="java.util.HashMap"> <entry key="key1" value="value1"/> <entry key="key2" value="value2"/> </util:map> 
0
source

Source: https://habr.com/ru/post/984087/


All Articles