在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. [ZJOI2012]网络

    嘟嘟嘟 今天复习lct,趁着还年轻多写点数据结构. 首先不得不吐槽一下,题面好长啊-- 通过观察发现,\(c \leqslant 10\).那么就可以暴力的建10棵lct. 接下来说下具体做法: 1. ...

  2. 路由器安装Openwrt&&***

    路由器安装Openwrt&&*** 前言 对于给路由器刷系统,肯定是有风险的,敢于承担风险的才开始动手. Openwrt其实也是一款嵌入式Linux系统,对于闪存大小也是有一定的要求的 ...

  3. CDB与PDB之间的切换方法

    Oracle 12c 开始支持 PLUGGABLE DATABASE,并且提供了一个方法在CDB和PDB之间切换. 1. 使用 show pdbs 可以确认当前有哪些PDB? SQL> show ...

  4. Oracle报错ORA-16433非归档丢失redo无法启动的恢复过程

    [案例]Oracle报错ORA-16433非归档丢失redo无法启动的恢复过程 转惜纷飞 今天ML的群中女神和travel在纠结一个恢复的问题,11.2.0.3版本,非归档,大概是rm掉current ...

  5. Linux 分卷压缩

    例如,要将大文件夹 PYNQ 分卷压缩成 1G 的单元大小,如下命令(类似的可以指定 tar 的参数为 czf 而生产 .tar.gz 格式的压缩包:可以指定分卷大小例如 500M 等),压缩完成后, ...

  6. python3 迭代器

    ''' 迭代:重复的过程,并且每次迭代的结果都是下次迭代的初始值 可迭代的 Iterable 迭代器 Iterator iter(数据) == 数据.__iter__() 将数据转换为迭代器 next ...

  7. 洛谷 P1451 求细胞数量

    题目链接 https://www.luogu.org/problemnew/show/P1451 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字 ...

  8. PHP之基本操作

    PHP基础操作1.session操作 手动设置session生存期方式一: session_start(); // 设置生存时间 $lifeTime = 24*6000; setcookie(sess ...

  9. Node.js配合jQuery UI autocomplete的应用

    Node.js擅长的领域为: 不需要很多运算 吞吐量要求高 进消息轻并且要求快 出消息轻并且要求快 网上的例子都是socket.io的,我一直在想到底能用在什么地方?根据node.js的优点(擅长领域 ...

  10. Web登录其实没那么简单

    1. 一个简单的HTML例子看看用户信息安全 标准的HTML语法中,支持在form表单中使用<input></input>标签来创建一个HTTP提交的属性,现代的WEB登录中, ...