SpringBoot 标签之启动

时间:2022-09-16 08:11:19

在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 标签之启动的更多相关文章

  1. spring-boot 根据环境启动

    spring-boot 根据环境启动: java -jar spring-boot--config--SNAPSHOT.jar --spring.profiles.active=prod

  2. 3.Springboot之修改启动时的默认图案Banner

    一.SpringBoot的默认启动图案 在SpringBoot启动的时候,默认的会展示出一个spring的logo,这个图案我们用户是可以自定义的 二.自定义启动图案 方法一: Application ...

  3. SpringBoot是如何启动的?

    本文是通过查看SpringBoot源码整理出来的SpringBoot大致启动流程,整体大方向是以简单为出发点,不说太多复杂的东西,内部实现细节本文不深扣因为每个人的思路.理解都不一样,我个人看的理解跟 ...

  4. 【玩转SpringBoot】通过事件机制参与SpringBoot应用的启动过程

    生命周期和事件监听 一个应用的启动过程和关闭过程是归属到“生命周期”这个概念的范畴. 典型的设计是在启动和关闭过程中会触发一系列的“事件”,我们只要监听这些事件,就能参与到这个过程中来. 要想监听事件 ...

  5. SpringBoot项目快速启动停止脚本

    SpringBoot项目快速启动停止脚本 1.在jar包同级目录下,创建 app.sh #!/bin/bash appName=`ls|grep .jar$` if [ -z $appName ] t ...

  6. SpringBoot 应用程序启动过程探秘

    概述 说到接触 SpringBoot 伊始,给我第一映像最深的是有两个关键元素: 对照上面的典型代码,这个两个元素分别是: @SpringBootApplication SpringApplicati ...

  7. 【玩转SpringBoot】SpringBoot应用的启动过程一览表

    SpringBoot应用的启动方式很简单,就一行代码,如下图01: 其实这行代码背后主要执行两个方法,一个是构造方法,一个是run方法. 构造方法主要内容就是收集一些数据,和确认一些信息.如下图02: ...

  8. SpringBoot学习之启动探究

    SpringApplication是SpringBoot的启动程序,我们通过它的run方法可以快速启动一个SpringBoot应用.可是这里面到底发生了什么?它是处于什么样的机制简化我们程序启动的?接 ...

  9. springboot之docker启动参数传递

    这几天有网友问,如何在使用docker的情况下传递spring.profiles.active=test,也就是说springboot切换配置文件.以往我们直接通过java启动jar的时候,直接跟上- ...

随机推荐

  1. STM32F10xxx 之 System tick Timer(SYSTICK Timer)

    背景 研究STM32F10xxx定时器的时候,无意间看到了System tick Timer,于是比较深入的了解下,在此做个记录. 正文 System tick Timer是Cotex-M内核的24位 ...

  2. java 15-10 List的三个子类的特点

    List:(面试题List的子类特点) ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低. Li ...

  3. ubuntu14 eclipse luna 无法显示菜单 , 解决方案

    使用命令行 , 输入 Exec=env UBUNTU_MENUPROXY=0 <eclipse的安装路径>/eclipse 就可以了 或者建立一个Eclipse的快捷方式,eclipse. ...

  4. 如何解决vector 析构函数的异常 opencv Assert &lowbar;CrtIsValidHeapPointer

    一气呵成代码,但是,当发生执行_CrtIsValidHeapPointer例外,去搭调了一上午Bug.最终获得 跟踪定位到 _CrtIsValidHeapPointer ,注意到 g 8h&quo ...

  5. Oracle 锁

    select  for update对某行加锁之后: select语句可以执行: select  for update 这行不可以: 会一直等待锁释放 select for update wait 3 ...

  6. python环境jieba分词的安装

    我的python环境是Anaconda3安装的,由于项目需要用到分词,使用jieba分词库,在此总结一下安装方法. 安装说明======= 代码对 Python 2/3 均兼容 * 全自动安装:`ea ...

  7. P1417 烹调方案 &lpar;0&sol;1背包&plus;贪心&rpar;

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  8. FFmpeg 结构体学习&lpar;六&rpar;: AVCodecContext 分析

    在上文FFmpeg 结构体学习(五): AVCodec 分析我们学习了AVCodec结构体的相关内容.本文,我们将讲述一下AVCodecContext. AVCodecContext是包含变量较多的结 ...

  9. Docker学习笔记之编写 Docker Compose 项目

    0x00 概述 通过阅读之前的小节,相信大家对 Docker 在开发中的应用已经有了一定的了解.作为一款实用的软件,我们必须回归到实践中来,这样才能更好地理解 Docker 的实用逻辑和背后的原理.在 ...

  10. java web开发环境配置系列(二)安装tomcat

    在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<java web开发环境配置系列>来祭奠那逝去的…… 1.下载tomcat压缩包,进入官网http: ...