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 get properties File in Java

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

Share

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

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

How to get Properties under spring

For example, the existing commonConfig.properties

Main.db.driverClassName=com.mysql.jdbc.Drivermain.db.url=jdbc:mysql://cloudpkdbrw.xxx.com:3306/huagang?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNullmain.db.username=huagangmain.db.password=xxxHGtest

Reference commonConfig.properties in spring

Type 1: use classpath:/resources/config/commonConfig.properties ${main.db.driverClassName} ${main.db.url} ${main.db directly in spring's xml .username} ${main.db.password} 1 4 1800 1 0 1 60 30 100 false false True select 1 from dual 2: call import org.springframework.beans.factory.annotation.Value in the java startup plus Conifg library or in controller Import org.springframework.stereotype.Component; @ Component public class Config {@ Value ("${main.db.url}") public String jdbcUrl;}

Controller

@ RequestMapping ("/ service/**") @ Controller public class TestController {@ Value ("${main.db.url}") private String jdbcUrl; / / directly reference @ RequestMapping (value= {"/ test"}) public ModelMap test (ModelMap modelMap) {modelMap.put ("jdbcUrl", Config.jdbcUrl); return modelMap in Controller Type 3: do not reference commonConfig.properties in spring.xml, reference it when the class is injected, and then use Environment to get its value import org.apache.commons.lang3.tuple.Pair;import org.redisson.Config;import org.redisson.Redisson;import org.redisson.SentinelServersConfig;import org.redisson.SingleServerConfig;import org.redisson.client.RedisClient;import org.redisson.client.RedisConnection;import org.redisson.client.protocol.RedisCommands;import org.redisson.codec.SerializationCodec Import org.redisson.misc.URIBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;@Configuration@PropertySource ("classpath:resources/config/commonConfig.properties") public class RedissonConfig {@ Autowired private Environment env; @ Bean public SerializationCodec serializationCodec () {return new SerializationCodec () } @ Bean public Config reddissonConfig () throws Exception {String jdbcUrl= env.getProperty ("main.db.url");} / this is the fourth kind of code snippet: do not need to borrow spring, read directly in the class. Note, however: (cannot be found in the redisson.properties configuration file. Full stop), otherwise import java.util.ResourceBundle;public class RedissionParamsUtil {/ * * configuration file address * / private final String configPath = "resources/config/redisson.properties"; private static RedissionParamsUtil paramsUtil; ResourceBundle bundle = null will be reported / * Singleton mode acquisition instance * @ return MenuService * / public static RedissionParamsUtil getInstance () {if (null==paramsUtil) {paramsUtil = new RedissionParamsUtil ();} return paramsUtil;} / * Construction method * / private RedissionParamsUtil () {bundle = ResourceBundle.getBundle (configPath) } public String getValue (String key) {return bundle.getString (key);} public static void main (String [] args) {System.out.println (RedissionParamsUtil.getInstance (). GetValue ("jdbc_url"));}} these are all the contents of the article "how to get properties files in Java". 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