SpringBoot 标签之启动
在SpringBoot中入口我们使用:
package com.sankuai.qcs.regulation.traffic; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.reactive.config.EnableWebFlux; @SpringBootApplication
@EnableWebFlux
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
因为是SpringBoot启动,是全注解启动形式,里面的任何注解和XML在启动的时候都可以加载到内存中,XML 和注解可以混合使用,
因为SpringBoot本来就是为了减少XML文件的,所以尽量使用注解
比如:
成都项目:
package com.sankuai.qcs.regulation.traffic.common.config;
import com.dianping.squirrel.client.impl.redis.router.RouterType;
import com.dianping.squirrel.client.impl.redis.spring.RedisClientBeanFactory;
import com.google.common.base.Objects;
import com.meituan.mafka.client.consumer.IMessageListener;
import com.sankuai.qcs.regulation.traffic.common.constant.CommonConstants;
import com.sankuai.qcs.regulation.traffic.common.consumer.MessageConsumer;
import com.sankuai.qcs.regulation.traffic.common.util.HttpUtil; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.reactive.config.EnableWebFlux; /**
* Created by ramon on 2017/11/9.
*/
@Configuration
@ImportResource(locations = {"classpath:rhinoConfig.xml"})
public class SpringBeanConfig { /**
* 动态配置
* @return
*/
@Bean("mccConfig")
public PropertySourcesPlaceholderConfigurer mccConfig() {
MccPropertyPlaceholderConfigurer mccPropertyPlaceholderConfigurer = new MccPropertyPlaceholderConfigurer();
mccPropertyPlaceholderConfigurer.setAppKey(CommonConstants.APP_NAME);
mccPropertyPlaceholderConfigurer.setScanBasePackage("com.sankuai.qcs.regulation");
return mccPropertyPlaceholderConfigurer;
} /**
* 提供netty http server的配置
* @return
*/
@Bean
public NettyReactiveWebServerFactory httpServer() {
NettyReactiveWebServerFactory nettyReactiveWebServerFactory = new NettyReactiveWebServerFactory();
int httpPort = HttpUtil.getHttpPort();
nettyReactiveWebServerFactory.setPort(httpPort);
return nettyReactiveWebServerFactory;
} /**
* 消息监听
* @return
*/
@Bean("messageConsumer")
public MessageConsumer messageConsumer(@Value("${app.name}")String appName, @Value("${topic.name}")String topic, @Value("${group.name}")String groupName, IMessageListener regulationMessageListener) {
MessageConsumer messageConsumer = new MessageConsumer();
messageConsumer.setAppKey(appName);
messageConsumer.setBg("waimai");
messageConsumer.setTopic(topic);
messageConsumer.setConsumerGroup(groupName);
messageConsumer.setConsumerListener(regulationMessageListener);
messageConsumer.start();
return messageConsumer;
} }
在messageConsumer方法中:参数有个:
@Value("${app.name}")String appName
他的意思是:参数appName有个默认值:就是@Value("${app.name}");而 ${app.name}指的是,系统会在项目中遍历所有的properties文件,并找到app.name加载到指定的值;
后面的参数是:
IMessageListener regulationMessageListener
springboot会根据 IMessageListener 的实现类,自动的注册到项目中,因为
IMessageListener 有两个实现类:
package com.meituan.mafka.client.consumer;
public abstract class IDeadLetterListener implements IMessageListener {
private DeadLetterConsumer consumer;
public IDeadLetterListener() {
}
public boolean retry(Object msg, long delayTime) throws Exception {
return this.consumer.retry(msg, delayTime);
}
public void setConsumer(DeadLetterConsumer consumer) {
this.consumer = consumer;
}
}
还有个:
package com.sankuai.qcs.regulation.traffic.mq; import com.dianping.cat.Cat;
import com.meituan.mafka.client.consumer.ConsumeStatus;
import com.meituan.mafka.client.consumer.IMessageListener;
import com.meituan.mafka.client.message.MafkaMessage;
import com.meituan.mafka.client.message.MessagetContext;
import com.sankuai.qcs.regulation.traffic.common.util.JsonUtil;
import com.sankuai.qcs.regulation.traffic.service.MessageManageService; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.io.IOException; /**
* 数据监听
* Created by panyuanyuan02 on 2018/1/30.
*/
@Service
public class RegulationMessageListener implements IMessageListener {
@Resource
private MessageManageService messageManageService;
private Logger logger = LoggerFactory.getLogger(RegulationMessageListener.class);
@Override
public ConsumeStatus recvMessage(MafkaMessage message, MessagetContext context) {
try {
String body = (String) message.getBody();
logger.info("Recv message:{}", body);
MafkaMessageObject mafkaMessageObject = JsonUtil.fromStr(body, MafkaMessageObject.class);
messageManageService.saveMessage(mafkaMessageObject.getMsgType(), mafkaMessageObject.getData());
} catch (Exception e) {
logger.error("process message exception", e);
Cat.logMetricForCount("process_message_exception");
}
return ConsumeStatus.CONSUME_SUCCESS;
} }
请注意第二个实现类上面的@Service注解, springboot 能找到第二个,因为有注解标签@Service,但是找不到第一个,以为第一个就是一个普通的类,并没有注解标签;
里面有标签:@Configuration;
也有引入的配置文件:
@ImportResource(locations = {"classpath:rhinoConfig.xml"})
但是如果是Spring项目的话,
@Configuration有可能不加载;
Spring 启动有两种启动方式,一种是
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application.xml");
}
这种方法不能加载@Configuration,但是可以加载@Service等;
比如:
package com.sankuai.qcs.regulation.shanghai; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer; import java.io.FileNotFoundException; /**
* @author ssc Feb 1, 2018
*/
public class App { private static Logger LOGGER = LoggerFactory.getLogger(App.class); static {
try {
Log4jConfigurer.initLogging("classpath:log4j2.xml", );
} catch (FileNotFoundException e) {
LOGGER.error("App#static initializer init log config error", e);
}
} public static void main(String[] args) throws Throwable { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application.xml");
}
}
这种方式的话,@Configuraton是不会加载的;
需要使用另一种加载方式:
ApplicationContext ac1 = new AnnotationConfigApplicationContext("com.sankuai.qcs.regulation.shanghai");
参数是基础的包名称,这样 @Configuration就可以加载上了;
SpringBoot 标签之启动的更多相关文章
- spring-boot 根据环境启动
spring-boot 根据环境启动: java -jar spring-boot--config--SNAPSHOT.jar --spring.profiles.active=prod
- 3.Springboot之修改启动时的默认图案Banner
一.SpringBoot的默认启动图案 在SpringBoot启动的时候,默认的会展示出一个spring的logo,这个图案我们用户是可以自定义的 二.自定义启动图案 方法一: Application ...
- SpringBoot是如何启动的?
本文是通过查看SpringBoot源码整理出来的SpringBoot大致启动流程,整体大方向是以简单为出发点,不说太多复杂的东西,内部实现细节本文不深扣因为每个人的思路.理解都不一样,我个人看的理解跟 ...
- 【玩转SpringBoot】通过事件机制参与SpringBoot应用的启动过程
生命周期和事件监听 一个应用的启动过程和关闭过程是归属到“生命周期”这个概念的范畴. 典型的设计是在启动和关闭过程中会触发一系列的“事件”,我们只要监听这些事件,就能参与到这个过程中来. 要想监听事件 ...
- SpringBoot项目快速启动停止脚本
SpringBoot项目快速启动停止脚本 1.在jar包同级目录下,创建 app.sh #!/bin/bash appName=`ls|grep .jar$` if [ -z $appName ] t ...
- SpringBoot 应用程序启动过程探秘
概述 说到接触 SpringBoot 伊始,给我第一映像最深的是有两个关键元素: 对照上面的典型代码,这个两个元素分别是: @SpringBootApplication SpringApplicati ...
- 【玩转SpringBoot】SpringBoot应用的启动过程一览表
SpringBoot应用的启动方式很简单,就一行代码,如下图01: 其实这行代码背后主要执行两个方法,一个是构造方法,一个是run方法. 构造方法主要内容就是收集一些数据,和确认一些信息.如下图02: ...
- SpringBoot学习之启动探究
SpringApplication是SpringBoot的启动程序,我们通过它的run方法可以快速启动一个SpringBoot应用.可是这里面到底发生了什么?它是处于什么样的机制简化我们程序启动的?接 ...
- springboot之docker启动参数传递
这几天有网友问,如何在使用docker的情况下传递spring.profiles.active=test,也就是说springboot切换配置文件.以往我们直接通过java启动jar的时候,直接跟上- ...
随机推荐
- 【vue】vue +element 搭建项目,this.$nextTick用法
相关资料:https://www.cnblogs.com/leaf930814/p/7247478.html https://www.cnblogs.com/duanyue/p/7458340.htm ...
- Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
错误信息如下: Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java ...
- 面试 16:栈的压入压出队列(剑指 Offer 第 22 题)
我们今天继续来看看周五留下的习题: 面试题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如:压入序列为{1,2,3,4,5},那{ ...
- .NetCore 资料分享
.NetCore3.0 你还不打算入手么? 这次主要是推荐一些自己学习中遇到的一些好的资料和自己的看法( 我推荐的都是我看过的 Asp.Net Core 不做介绍了,直接分享资料了 资料: .NetC ...
- 一次掌握 React 与 React Native 两个框架
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法. 1. 软件开发语言与框架的学习本质 我 ...
- Django的model form组件
前言 首先对于form组件通过全面的博客介绍,对于form我们应该知道了它的大致用法,这里我们需要明确的一点是,我们定义的form与model其实没有什么关系,只是在逻辑上定义form的时候字段名期的 ...
- python 获取gearbest地址库代码
import requests import json # 用来去掉多余的字符,并格式化 def geshihua(str): s = None if "/**/_get_country(& ...
- CPU-bound(计算密集型) 和I/O bound(I/O密集型)
概念 概念I/O系统,英文全称为“Input output system”,中文全称为“输入输出系统”,由输入输出控制系统和外围设备两部分组成,是计算机系统的重要组成部分.在计算机系统中,通常把处理器 ...
- PS调出甜美艺术外景女生照片
前期思路:拍摄时间大概在下午三四点左右,IOS100 f/1.8 .其实夏天最好的拍摄时间在傍晚五点这样,曝光太强片子会泛白,这张原片首先构图不是很好看,所以我要给它二次构图裁剪一下.下面是裁剪好后的 ...
- NET 使用 RabbitMQ
NET 使用 RabbitMQ 前言 最近项目要使用RabbitMQ,园里里面已经有很多优秀的文章,Rabbitmq官网也有.net实例.这里我尝试下图文并茂之形式记录下使用的过程. 安装 Rabbi ...