Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean
package cn.com.test.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;
}
} @Bean
public ProducerAgent producerService() {
try {
ProducerAgent producerAgent = new ProducerAgent();
return producerAgent;
} 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;
}
}
} package cn.com.test.receive; import java.io.IOException;
import java.io.InputStream; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Spring 注入初始化配置文件资源
* @author
*
*/
public class ConfigurationBase { private static final Logger logger = LoggerFactory.getLogger(ConfigurationBase.class); /***
* Spring 自动注入扫描加载 @Configuration 注解标识的类
* 及调用 ConfigurationBase.class.getResourceAsStream 加载配置文件
* 并载入 @Bean 等相关注解标注的 javaBean
* @param resource
* @param basePackages
* @return
*/
public SuperAnnotationConfigApplicationContext loadSpringContext(String resource,String... basePackages) {
try {
this.loadConfigurationFromResource(resource);
//this.setSystemProperties();
return new SuperAnnotationConfigApplicationContext(basePackages);
} catch (Exception e) {
// TODO: handle exception
throw e;
}
} /**
* 加载应用程序配置文件
* @param resource
*/
private void loadConfigurationFromResource(String resource) {
InputStream is = ConfigurationBase.class.getResourceAsStream(resource);
try {
if (is != null) {
System.getProperties().load(is);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
} @SuppressWarnings("unused")
private void setSystemProperties() {
System.setProperty("redis.keys.group", "cn.com.redis.test.case");
} /**
* 通过应用上下文,初始化类参数
* @author
*
*/
public class SuperAnnotationConfigApplicationContext extends AnnotationConfigApplicationContext { public SuperAnnotationConfigApplicationContext(String... basePackages) {
super();
GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(PropertyPlaceholderConfigurer.class);
this.registerBeanDefinition("propertyPlaceholderConfigurer", gbd);
this.scan(basePackages);
this.refresh();
}
}
}
/**
*
* 实例化调用
*/
private static void runClient() throws Exception{
try {
ConfigurationBase configurationBase = new ConfigurationBase();
SuperAnnotationConfigApplicationContext applicationContext =
configurationBase.loadSpringContext("/terminal-receive.properties", "cn.com.test.receive");
ConsumerService consumerService = (ConsumerService) (applicationContext.getBean("consumerService"));
consumerService.Start();
} catch (Exception e) {
// TODO: handle exception
throw e;
}
}
terminal-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 读取配置文件(一)
注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...
- Spring读取配置文件 @Value
最近在学习Spring如何读取配置文件,记录下方便自己也方便别人: 大致分为两类吧,一种的思路是利用Spring的beanFactoryPostProcessor读取配置文件内容到内存中,也就是应用程 ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- Spring读取配置文件,获取bean的几种方式
BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
- spring 读取配置文件
spring读取dubbo xml文件,在本项目内可以调用正常,一旦把改项目打成jar包,供其他项目调用,就会提示找不到配置文件 ClassPathXmlApplicationContext cont ...
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
随机推荐
- [Contest20180313]灵大会议
为了方便才用lct,没想到最后要加读入优化才能过... 有一个结论就是在一条链上,如果能找到一个点使得这个点划分链左右两边的树节点权值和最相近,那么这个点就是答案 用lct维护,每个splay节点存树 ...
- [CF864F]Cities Excursions
题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...
- 微服务之SpringCloud实战(二):SpringCloud Eureka服务治理
服务治理 SpringCloud Eureka是SpringCloud Netflix微服务套件的一部分,它基于Netflix Eureka做了二次封装,主要完成微服务的服务治理功能,SpringCl ...
- WebForm页面使用Ajax
AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...
- 10个常用的ps命令总结,参数
Linux系统中10个常用的ps命令总结 PS 命令是什么 查看它的man手册可以看到,ps命令能够给出当前系统中进程的快照.它能捕获系统在某一事件的进程状态.如果你想不断更新查看的这个状态,可以使用 ...
- 【DP】UVA 624 CD 记录路径
开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> # ...
- 编程算法 - 和为s的两个数字 代码(C)
和为s的两个数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个递增排序的数组和一个数字s, 在数组中查找两个数, 使得它们的和正好是 ...
- 访问vector元素方法的效率比较(转)
LInux下: gcc 4.47,red hat6 #include<iostream> #include<vector> #include<time.h> usi ...
- CSAPP:异常控制流
在一般的情况下,处理器处理的指令序列是相邻的(顺序执行). 异常控制流提供了指令的跳转,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常处理 在系统启动时,操作系统分配和初始化一张称为异常表的 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...