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

本文共 4475 字,大约阅读时间需要 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

 

转载于:https://www.cnblogs.com/rinack/p/6054032.html

你可能感兴趣的文章
js 过滤敏感词
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
软件开发和软件测试,我该如何选择?(蜗牛学院)
查看>>
基本封装方法
查看>>
[Typescript] Specify Exact Values with TypeScript’s Literal Types
查看>>
Illustrated C#学习笔记(一)
查看>>
理解oracle中连接和会话
查看>>
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
在centos上开关tomcat
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>