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读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
随机推荐
- 「Baltic2015」Network
题目描述 原文 The government of Byteland has decided that it is time to connect their little country to th ...
- openresty的ngx.timer.at
openresty的ngx.timer.at真是个强大的方法. 例如某些函数不可以在一些NGINX的执行阶段使用时,可以ngx.timer.at API 创建一个零延迟的timer,在timer中去处 ...
- Java高级架构师(一)第07节:远程使用以及冲突解决
- 使用DMV调优性能 --Burgess_Liu
http://blog.csdn.net/burgess_liu/article/details/52813727
- P2P通信标准协议(二)之TURN
上一篇P2P通信标准协议(一)介绍了在NAT上进行端口绑定的通用规则,应用程序可以根据这个协议来设计网络以外的通信. 但是,STUN/RFC5389协议里能处理的也只有市面上大多数的Cone NAT( ...
- Delphi制作软键盘
{ 作者: han 日期: 2006.06.02 } unit softkey; interface uses Windows, Messages, SysUtils, Variants, C ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.3.安装前检查
3.3. 安装前检查 1.检查节点连通性 或:./runcluvfy.sh stage -pre crsinst -n linuxrac1,linuxrac2 -fixup -verbose [gri ...
- csharp 面向对象编程
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shap ...
- 使用unity3d开发app
做过一些项目,参入过一些项目的计划安排.总觉得一些工具用起来很麻烦,要么是要收费,要么很大很重.没有针对小团队的简单易用的任务管理工具,也可能是找了些不能适合自己的习惯. 所有准备开始自己开发一款项目 ...
- 【业务自动化】iTop,全面支持ITIL流程的一款ITSM工具
iTop产品针对的主要应用场景为:内部IT支持.IT外包管理.数据中心运维管理和企业IT资产管理.常青管理从绿象认证产品中选取了iTop作为主要推荐产品,本类别的绿象认证产品还包括:OTRS和RT3等 ...