日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 读取配置文件(二)

發布時間:2023/12/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 读取配置文件(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;@Beanpublic ConsumerService consumerService() {try {ConsumerService consumerService = new ConsumerService(search,monitor,signkey,queueName,exchangeName,routingKey);return consumerService;} catch (Exception e) {// TODO: handle exceptionthrow e;}}@Beanpublic ProducerAgent producerService() {try {ProducerAgent producerAgent = new ProducerAgent();return producerAgent;} catch (Exception e) {// TODO: handle exceptionthrow 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 stubtry {} catch (Exception e) {// TODO Auto-generated catch blocklogger.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 exceptionthrow 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 exceptionthrow e;} }

terminal-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

?

轉載于:https://www.cnblogs.com/rinack/p/6054158.html

總結

以上是生活随笔為你收集整理的Spring 读取配置文件(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。