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读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
随机推荐
- BZOJ 1852 [MexicoOI06]最长不下降序列(贪心+DP+线段树+离散化)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1852 [题目大意] 给你N对数A1,B1……An,Bn.要求你从中找出最多的对, 把它 ...
- 1.7(java学习笔记)package和import
package package主要用于管理类,在java中同一个包下不能有相同的类名,可有时项目总会出现很多同名的类,这时就需要通过包来管理类.不同的包下可以有相同的类名. 包就有点类似于文件夹,不同 ...
- 将Java程序打jar包并运行
1)接着上篇博客继续说手动编译之后,将代码打成jar包,然后直接“java -jar lz.jar"运行不成功的问题.还是先上代码: 这个是Demo类: package org.lz.dem ...
- linux的打包与解压
zip: 打包 :zip something.zip something (目录请加 -r 参数) 解包:unzip something 指定路径:-d 参数 创建加密 zip 包 使用 -e 参数可 ...
- CentOS 6.9使用Setup配置网络(解决dhcp模式插入网线不自动获取IP的问题)
说明:dhcp模式插入网线不自动获取IP是因为网卡没有激活,造成这种原因的,应该是安装系统时没有插入网线造成的. 解决方法: 修改网卡配置文件 vim /etc/sysconfig/network-s ...
- Inno Setup 注册表启动项 修改注册表
//注册表启动项 [Registry] Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ...
- SpringMVC——redirect重定向跳转传值
原文:https://my.oschina.net/u/2273085/blog/398374 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url ...
- ActiveMQ实战-集群
原文:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- 【json】前台ajax序列化的多个属性拼接在一起的字符串,转化为JSONObject对象
1.首先看一下前台序列化了哪些东西: 部分js代码 //查询按钮 $(".questButton").click(function(){ $.ajax({url:"/qu ...
- WebLogic Server 关键优化指标
昨天给客户做巡检,又将整个WebLogic Server的优化过程走了一遍,记录下来给大家参考. 1.JVM优化 查看 $ps –ef | grep java /opt/java1.5/bin/jav ...