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

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

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

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

    注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...

  3. Spring读取配置文件 @Value

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

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

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

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

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

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

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

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

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

  8. spring 读取配置文件

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

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

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

随机推荐

  1. ba resme

    Resume Name: Zhou Heng Gender: Male Email : jackyzhouheng@gmail.com Self Assessment: I have more tha ...

  2. Awesome图标 | 如何在某些编辑软件中使用Font Awesome字体图标

    文章目录 Font Awesome 字体图标 在某些编辑软件中使用 Font Awesome 字体图标 Font Awesome 为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何可以用 C ...

  3. python函数中的关键字参数

    关键字参数: 就是在形式参数中必须要提供”传递参数名=传递参数值” 位置参数:  仅仅只有参数名 特点:1.位置参数只能出现在关键字参数之前,不管是在行参还是实参中. 2.关键字参数在调用时(实参)中 ...

  4. ethtool 命令输出的注意点--网卡参数

    http://blog.csdn.net/msdnchina/article/details/70339689

  5. golang构造单链表

    原文地址:http://www.niu12.com/article/47package main import "fmt" type ListNode struct { Value ...

  6. mac上虚拟机:VMWare Fusion, VirtualBox, Parallels Desktop, CrossOver, Veertu

    作者:Louis Tong链接:https://www.zhihu.com/question/35731328/answer/66127970来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  7. [转载]Delphi Tokyo 10.2.3发布了

    转载:http://blog.sina.com.cn/s/blog_44fa172f0102wwwg.html 今早最好的消息,Delphi 10.2.3如期发布,下载地址:http://altd.e ...

  8. nmcli日常用法

    一.nmcli日常用法nmcli dev status //查看系统现有网络设备的连接状态nmcli conn show //查看已有连接nmcli conn delete UUID1 UUID2 U ...

  9. 【Hadoop】HDFS客户端开发示例

    1.原理.步骤 2.HDFS客户端示例代码 package com.ares.hadoop.hdfs; import java.io.FileInputStream; import java.io.F ...

  10. Java定时任务的三种实现方法

    译者注:个人觉得用定时任务来跑垃圾回收不是很好的例子,从译者接触到的项目来看,比较常见的是用定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法:1.普通thre ...