Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read configuration files dynamically in Spring

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to read the configuration file dynamically in Spring, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Spring dynamic read configuration file requirement background

The requirement we have encountered recently is something like this: we have to carry out secondary development on the basis of an existing project, but we are not willing to touch the code in the original project. So the way of Maven dependency is adopted-- a new Maven project is created as the main development environment, and the original project is introduced as a Maven dependency (war form). So the war packaged in the new extension project will be all the code that merges the two projects.

In the actual construction process, there is a problem that only one Spring configuration file is allowed, and this opportunity has been used by the original project-- this is not rigorous, so here are three solutions:

Option one

The above statement is not rigorous, in fact, multiple additions will not report an error; but only one will take effect (meaning that if spring does not read the attribute to replace the placeholder from the set of configuration files, it will report an error unless ignore-unresolvable is set). If you set it as follows, you can avoid this situation and contact this requirement.

But the disadvantages are:

1. Postponing the discovery of errors to run time is a big taboo when the system is large.

two。 The replacement crisis when the attribute is repeated, this kind of BUG wants to find out, and the time and energy it takes to think about it shudders.

Option 2

The second method is the BeanFactoryPostProcessor interface. Note that the callback time of this interface is earlier than the placeholder replacement operation.

/ / BeanFactoryPostProcessor.postProcessBeanFactory@Overridepublic void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {/ / We read the specified configuration file Properties prop = null; try {prop = PropertiesLoaderUtils.loadAllProperties ("extend/db.properties", Thread.currentThread (). GetContextClassLoader ());} catch (IOException e) {e.printStackTrace ();} if (null = = prop) {return } / / injected into specific attributes of a specific Bean BeanDefinition beanDefinition = beanFactory.getBeanDefinition ("dataSource_extend"); beanDefinition.getPropertyValues () .add ("url", prop.getProperty ("db.sd.url")); beanDefinition.getPropertyValues () .add ("driverClassName", prop.getProperty ("db.sd.driverClassName")); beanDefinition.getPropertyValues () .add ("username", prop.getProperty ("db.sd.username")) BeanDefinition.getPropertyValues (). Add ("password", prop.getProperty ("db.sd.password")); super.postProcessBeanFactory (beanFactory);} scenario three

Another way is to use the parent-child container relationship of the Spring, register this and all the Bean that depends on it with a new container, and then use the container as the Parent of the existing container. This method was tricked in the past, but I didn't actually try it.

Read the information in the configuration file dynamically. 1. First, write a configuration file to facilitate dynamic loading of jedis.properties.

Save in key-value form

1. Use class loaders to read configuration files

1. Read configuration file

InputStream is=JedisPoolUtils.class.getClassLoader () .getResourceAsStream (jedis.properties)

two。 Create a properties object

Properteis pro=new Properties ()

3. Associated file

Pro.load (is)

4. Then key can be loaded dynamically in the project to get the value value.

The above is all the contents of the article "how to read configuration files dynamically in Spring". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report