Java 获取properties的几种方式

网友投稿 236 2023-01-21

Java 获取properties的几种方式

spring下获取Properties方式

比如已有的commonConfig.properties

main.db.driverClassName=com.mysql.jdbc.Driver

main.db.url=jdbc\:mysql\://cloudpkdbrw.xxx.com\:3306/huagang?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull

main.db.username=huagang

main.db.password=xxxHGtest

在spring中引用commonConfig.properties

第1种:直接在spring的xml中使用

classpath:/resources/config/commonConfig.properties

  

 

      

${main.db.driverClassName}

${main.db.url}

${main.db.username}

${main.db.password}

1

4

1800

1

0

1

60

30

100

false

false

true

select 1 from dual

第2种:在java 启动加Conifg库中或者在controller中调用

import org.springframework.beans.factory.annotation.Value;

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; //直接在Controller引用

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("jdbcUrl", Config.jdbcUrl);

return modelMap;

}

}

第3种:不要在spring.xml中引用commonConfig.properties,在类注入时引用,然后使用Environment获取它的值

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");

}

//此为代码片段

第4种:不需要借用spring,直接在类中读取.但要注意:(redisson.properties配置文件中不能有.句号),否则将报错

import java.util.ResourceBundle;

public class RedissionParamsUtil {

/** 配置文件地址 */

private final String configPath = "resources/config/redisson.properties";

private static RedissionParamsUtil paramsUtil;

ResourceBundle bundle = null;

/**

* 单例模式获取实例

* @return MenuService

eYdHgREXF */

public static RedissionParamsUtil getInstance(){

if(null==paramsUtil){

paramsUtil = new RedissionParamsUtil();

}

return paramsUtil;

}

/**

* 构造方法

*/

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"));

}

}

以上就是Java 获取properties的几种方式的详细内容,更多关于Java 获取properties的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java基础之字符串编码知识点总结
下一篇:搜索开放api接口(查询api接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~