博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 读取配置文件(一)
阅读量:7044 次
发布时间:2019-06-28

本文共 4425 字,大约阅读时间需要 14 分钟。

注册 @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;@Configurationpublic 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
map = new HashMap
(); 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 counttms.terminal.search=200# tms monitortms.terminal.monitor=15# redis signkeytms.terminal.signkey=POI:# rabbit mq queueNametms.terminal.queueName=gps# rabbit mq exchangeNametms.terminal.exchangeName=tms# rabbit mq routingKeytms.terminal.routingKey=tms

 

转载地址:http://blqal.baihongyu.com/

你可能感兴趣的文章
[书目20150309]成功的企业级软件项目管理:优化绩效完美交付的最佳实践
查看>>
iOS 通过(lame)将录制音频转换成Mp3
查看>>
JDK7中的新特性 The try-with-resources Statement
查看>>
linux 清空文件内容命令
查看>>
Android——通知 Notification
查看>>
java.lang.InstantiationException:
查看>>
转:jmf編譯問題的解決
查看>>
Hosts文件的位置
查看>>
java设计模式演示样例
查看>>
Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
查看>>
suggest
查看>>
Java集合框架实现自定义排序
查看>>
数组添加:如何往数组的"null"位置插入数据呢?
查看>>
Number of Parallelograms(求平行四边形个数)
查看>>
转:基于TLS1.3的微信安全通信协议mmtls介绍
查看>>
ImageNet && 医学图像的识别
查看>>
HBase编程 API入门系列之delete(客户端而言)(3)
查看>>
Cocos2dx使用wxsqlite开源加密SQLite3数据库
查看>>
JMeter学习-内存溢出解决方法
查看>>
磨刀不误砍柴工——VS生成事件
查看>>