Spring 读取配置文件(一)

注册 @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=200
# tms monitor
tms.terminal.monitor=15
# 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

 

posted @ 2016-11-11 14:00  如.若  阅读(3533)  评论(0编辑  收藏  举报