注册 @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 读取配置文件(一)的更多相关文章

  1. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  2. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...

  3. Spring读取配置文件的几种方式

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...

  4. Spring读取配置文件 @Value

    最近在学习Spring如何读取配置文件,记录下方便自己也方便别人: 大致分为两类吧,一种的思路是利用Spring的beanFactoryPostProcessor读取配置文件内容到内存中,也就是应用程 ...

  5. Java中spring读取配置文件的几种方法

    Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...

  6. spring 读取配置文件

    spring读取dubbo xml文件,在本项目内可以调用正常,一旦把改项目打成jar包,供其他项目调用,就会提示找不到配置文件 ClassPathXmlApplicationContext cont ...

  7. Spring读取配置文件,获取bean的几种方式

    BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...

  8. spring读取配置文件PropertyPlaceholderConfigurer类的使用

    这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...

  9. 关于spring读取配置文件的两种方式

    很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...

随机推荐

  1. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  2. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

  3. 【kruscal】【最小生成树】【块状树】bzoj3732 Network

    跟去年NOIP某题基本一样. 最小生成树之后,就变成了询问连接两点的路径上的权值最大的边. 倍增LCA.链剖什么的随便搞. 块状树其实也是很简单的,只不过每个点的点权要记录成“连接其与其父节点的边的权 ...

  4. 【floyd】CODEVS 1077 多源最短路

    floyd模板 #include<cstdio> #include<algorithm> using namespace std; ][],m,x,y,n; int main( ...

  5. centos7下解决python3和python2同时存在但是无法使用pip3的问题

    一.首先,官网下载python3的所需版本. wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz 想下载到那个文件夹下就先进入到 ...

  6. IO流--字符流缓冲技术

    缓冲技术是为了提高数据的读写效率而提出的. (1)字符流的缓冲读 在字符流的缓冲技术中提供了一个newLine()方法,这个方法是跨平台的 在读数据的时候采用读完直接刷新的方式可以保证断电后数据不会丢 ...

  7. cocoods 出现下面的问题:ERROR: While executing gem ... (Errno::EPERM)

    今天安装cocoods 出现下面的问题:ERROR:  While executing gem ... (Errno::EPERM)     Operation not permitted - /us ...

  8. iOS开发-设置在使用NavigateController时View的顶部位置

      最近我在开发中遇到了一个问题,在使用NavigationController时内部的ViewController的View总是与屏幕顶部对齐,而我们有时候不需要这种效果: 在开发过程中,我们可能会 ...

  9. linux-统计文件中相同行的数量

    cat sorttest | sort | uniq -c sorttest文件内容如下

  10. 数据结构(Java语言)——BinaryHeap简单实现

    优先队列priority queue是同意至少下列两种操作的数据结构:insert插入以及deleteMin(删除最小者),它的工作是找出,返回并删除优先队列中最小的元素.insert操作等价于enq ...