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的时候,直接跟上- ...
随机推荐
- Jenkins控制台显示乱码
方案: 解决控制台中文乱码问题: 点击左侧“系统管理”——右侧选择“系统设置”——“全局属性”,选择第一项:Environment variables,键值对列表,点击增加: 键:LANG 值:zh. ...
- P2256 一中校运会之百米跑(map+并查集)
思路:首先处理名字,让字符串直接映射唯一一个数字,这就用map<string, int>即可. 然后,直接用并查集 #include<iostream> #include< ...
- [LeetCode]最大系列(最大正方形221,最大加号标志764)
221. 最大正方形 题目描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 ...
- Y7000安装驱动显卡问题
整体 https://blog.csdn.net/la9881275/article/details/86720752 详细 https://blog.csdn.net/luteresa/articl ...
- Python+Pycharm—学习1—封装&导入
一.封装 目的: 写了一个复杂功能的实现,下次有相同的需求时可以直接导入这个包来使用.就可以达到复用的目的了.通俗的来讲就是打包.(以下用四则运算举例) 方案: 1.1.新建python工程,新建一个 ...
- nova系列二:kvm介绍
一 什么是kvm KVM 全称 Kernel-Based Virtual Machine.也就是说 KVM 是基于 Linux 内核实现的,这就使得linux内核本身就相当于一个Hypervisor. ...
- 探讨.NET Core中实现AES加密和解密以及.NET Core为我们提供了什么方便!
前言 对于数据加密和解密每次我都是从网上拷贝一份,无需有太多了解,由于在.net core中对加密和解密目前全部是统一了接口,只是做具体的实现,由于遇到过问题,所以将打算基本了解下其原理,知其然足矣, ...
- 深入浅出Java反射
反射,它就像是一种魔法,引入运行时自省能力,赋予了 Java 语言令人意外的活力,通过运行时操作元数据或对象,Java 可以灵活地操作运行时才能确定的信息 这里笔者就深入浅出总结下Java反射,若有不 ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- Rikka with Subset HDU - 6092 (DP+组合数)
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...