Spring 读取配置文件(一)
注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载
package cn.com.receive;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class ApplicationInitialize { @Value("${tms.terminal.search:200}")
private int search; @Value("${tms.terminal.monitor:15}")
private int monitor; @Value("${tms.terminal.signkey:POI:}")
private String signkey; @Value("${tms.terminal.queueName:gps}")
private String queueName; @Value("${tms.terminal.exchangeName:tms}")
private String exchangeName; @Value("${tms.terminal.routingKey:tms}")
private String routingKey; @Bean
public ConsumerService consumerService() {
try {
ConsumerService consumerService = new ConsumerService(search,monitor,signkey,queueName,exchangeName,routingKey);
return consumerService;
} catch (Exception e) {
// TODO: handle exception
throw e;
}
}
}
具体业务实现类
package cn.com.test.receive; import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import cn.evun.tms.entity.QueueMessages; /***
* 消费者
* @author
*
*/
public class ConsumerService { private static final Logger logger = LoggerFactory.getLogger(ConsumerService.class); protected int SEARCH_COUNT;
protected String SIGN_KEY;
protected long MONITOR_SECONDS;
protected String QUEUE_NAME;
protected String EXCHANGE_NAME;
protected String ROUTING_KEY; /***
* 初始化消费者
* @param search
* @param monitor
* @param signkey
* @param queue_name
*/
public ConsumerService(int search,int monitor,String signkey,String queue_name,String exchangeName,String routingKey) {
SEARCH_COUNT = search;
MONITOR_SECONDS = monitor;
SIGN_KEY = signkey;
QUEUE_NAME = queue_name;
EXCHANGE_NAME = exchangeName;
ROUTING_KEY = routingKey;
} /**
* 启动服务消费者
* @throws Exception
*/
public void Start() throws Exception {
// TODO Auto-generated method stub
try { } catch (Exception e) {
// TODO Auto-generated catch block
logger.error("----------------------------- Start: "+ e.getMessage() +" -----------------------");
throw e;
}
}
}
/***
* Spring 自动注入扫描加载 @Configuration 注解标识的类
* 及实动态实例化一个 bean 加载配置文件
* 并载入 @Bean 等相关注解标注的 javaBean
* @param resource
* @param basePackages
* @return
*/
public abstract class test {
protected static ConsumerService consumerService;/***
* 初始化队列实例
*/
private void init() {
try {
@SuppressWarnings("resource")
MyApplicationContext myCtx = new MyApplicationContext("cn.com.receive");
consumerService = (ConsumerService) myCtx.getBean(ConsumerService.class);
consumerService.Start();
} catch (Exception e) {
// TODO: handle exception
throw e;
}
}
/***
* 加载 .properties 配置文件
* Spring 编码方式获取 PropertyPlaceholderConfigurer 的属性
* @author
*
*/
private static class MyApplicationContext extends AnnotationConfigApplicationContext {
public MyApplicationContext(String basePackages) {
super();
GenericBeanDefinition beanDefination = new GenericBeanDefinition();
beanDefination.setBeanClass(PropertyPlaceholderConfigurer.class);
Map<String, String> map = new HashMap<String, String>();
map.put("locations", "/receive.properties");//根据 ApplicationContext 进行判断
//map.put("locations", "file:D://receive.properties");//作为 URL 从文件系统中加载。
beanDefination.setPropertyValues(new MutablePropertyValues(map));
this.registerBeanDefinition("propertyPlaceholderConfigurer", beanDefination);
super.scan(basePackages);
super.refresh();
}
}
receive.properties 配置文件
# tms redis count
tms.terminal.search=
# tms monitor
tms.terminal.monitor=
# redis signkey
tms.terminal.signkey=POI:
# rabbit mq queueName
tms.terminal.queueName=gps
# rabbit mq exchangeName
tms.terminal.exchangeName=tms
# rabbit mq routingKey
tms.terminal.routingKey=tms
Spring 读取配置文件(一)的更多相关文章
- java web路径和spring读取配置文件
此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...
- Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- Spring读取配置文件 @Value
最近在学习Spring如何读取配置文件,记录下方便自己也方便别人: 大致分为两类吧,一种的思路是利用Spring的beanFactoryPostProcessor读取配置文件内容到内存中,也就是应用程 ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- spring 读取配置文件
spring读取dubbo xml文件,在本项目内可以调用正常,一旦把改项目打成jar包,供其他项目调用,就会提示找不到配置文件 ClassPathXmlApplicationContext cont ...
- Spring读取配置文件,获取bean的几种方式
BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
随机推荐
- [CodeChef-LVGFT]Lovers Gift
题目大意: 给定一个$n(n\le10^5)$个结点的树,初始全为白点.$m(m\le10^5)$次操作,每次将点$x$染成黑色或询问从$x$出发至少经过一个黑点能到达的点中,编号次大的点. 思路: ...
- cocos2d-x引擎优化 修改记录
3.13 一些无聊的bug etc1 在android 平台不能正常显示,包括 cc.Button,setGray,clipeNode 3.13.1优化记录 7月 2.公式计算改为长整型, ...
- 【MySQL笔记】: unable to connect to remote host. catalog download has failed.
安装完MySQL之后,它每天凌晨启动一个Intaller任务,甚是烦人: 这是一个Windows的计划服务,在这里删除即可,开始/附件/系统工具/任务计划程序,把mysql的定时任务计划取消/删除 ...
- SweetAlert和MBProgressHUD冲突的解决办法
解决方法 : UIView *view = [UIApplication sharedApplication].keyWindow; NSLog(@" -- &g ...
- linux内核3.6版本及以下的bug引发的故障--cpu使用率100%
现象: 旗舰店运价库cpu使用率100%,load升高,导致后续的请求失败. 重启服务器,cpu.load恢复正常. 触发条件: (1)linux内核3. ...
- HTML-DEV-ToolLink(常用的在线字符串编解码、代码压缩、美化、JSON格式化、正则表达式、时间转换工具、二维码生成与解码等工具,支持在线搜索和Chrome插件。)
HTML-DEV-ToolLink:https://github.com/easonjim/HTML-DEV-ToolLink 常用的在线字符串编解码.代码压缩.美化.JSON格式化.正则表达式.时间 ...
- CentOS查看主板型号、CPU、显卡、硬盘等信息
系统 uname -a # 查看内核/操作系统/CPU信息 head -n 1 /etc/issue # 查看操作系统版本 cat /proc/cpuinfo # 查看CPU信息 hostname # ...
- 百度地图API的事件处理:覆盖物的如何阻止冒泡
百度地图,为了让事件使用的更方便,进行一层封装 详情可以看官方的文档 http://developer.baidu.com/map/jsdevelop-5.htm 主要的修改点: 1. 使用事件代理. ...
- [转] c++ try catch 问题
windhaunting,原文地址 以前都是用try{} catch(…){}来捕获C++中一些意想不到的异常, 今天看了Winhack的帖子才知道,这种方法在VC中其实是靠不住的.例如下面的代码: ...
- centos系统的时间时区和MySQL的时间时区问题
原文:http://1567045.blog.51cto.com/1557045/1074971 centos系统的时间时区和MySQL的时间时区问题 年轻人做事要细心,特别我们这些搞IT的千万不莽 ...