注册 @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. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  2. python3开发进阶-Django框架中的ORM的常用操作的补充(F查询和Q查询,事务)

    阅读目录 F查询和Q查询 事务 一.F查询和Q查询 1.F查询 查询前的准备 class Product(models.Model): name = models.CharField(max_leng ...

  3. 再谈EditText只能输入金额

    上次写了一篇EditText只能输入金额的博客,后来发现一个bug,当还未输入数字的情况下输入小数点程序就崩了,我去测了一下支付宝,看看会怎么样,我先输入小数点,程序正常,我再输入数字,可以正常输入, ...

  4. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

  5. java源码阅读StringBuilder

    1类签名与注释 public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializab ...

  6. Docker解析及轻量级PaaS平台演练(三)--Dockerfile编写

    在本篇中将介绍Dockerfile的编写 除了通过修改Image,创建Container,在打包成Image来创建我们需要的Image之外 我们还可以编写Dockerfile文件,通过build来创建 ...

  7. angular directive 深入理解

    由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...

  8. Guice 学习(五)多接口的实现( Many Interface Implementation)

    1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...

  9. 恼人的The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved...错误,无奈用Struts的bean:write替代了JSTL的C:out

    一个应用中有两个页面使用了JSTL的c:out输出,就类似这么简单三句 <c:if test="${!empty error}">       <h2>&l ...

  10. 使用Unity3D的50个技巧:Unity3D最佳实践

    转自:http://www.tuicool.com/articles/buMz63I  刚开始学习unity3d时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇 ...