在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. PHP 3种方法实现采集网站数据

    什么叫采集? 就是使用PHP程序,把其他网站中的信息抓取到我们自己的数据库中.网站中. PHP制作采集的技术: 从底层的socket到高层的文件操作函数,一共有3种方法可以实现采集. 1. 使用soc ...

  2. P1843 奶牛晒衣服(二分)

    思路:就是一个模板,只是找最小化而已.在判断函数里面:当湿度<=x*A不判断, 反之sum+=(a[i]-x*A)/B+(a[i]-x*A)%B?1:0; #include<iostrea ...

  3. 四、Oracle 序列、常用函数、多表连接

    一.序列定义:是oracle数据库专门用来产生连续且自动增长的数字的对象创建语法:create sequence 序列名(sq_表名) nocache(无缓存) create sequence sq_ ...

  4. ESP8266串口和MQTT服务器消息互传(版本一) 单纯透传+保存WIFI账号信息

    目标 制作一个ESP8266串口和MQTT相互透传的小WIFI,可用手机修改其连接的路由器,由此该模块可以任意加载到各种串口传输的单片机上,完成硬件到云端的传输. 1 实物图 2 MQTT网页测试客户 ...

  5. ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  6. from __future__ import包的作用

    __future__是python2的概念,其实是为了使用python2时能够去调用一些在python3中实现的特性 1.absolute_import from __future__ import ...

  7. XtraDB引擎

    https://mariadb.com/kb/en/library/xtradb-and-innodb/ https://mariadb.com/kb/en/library/about-xtradb/ ...

  8. i春秋-百度杯十月场-fuzzing

    1. 打开链接,提示 show me key,抓包,传值key=1,GET请求没有用,而POST请求有返回. 2.将md5值直接拿去解密,得到key=ichunqiu105    OK,进入下一步. ...

  9. Java入门篇(五)——字符串/String类

    前面在举例时有出现过String的例子,当时肯定有一部分朋友不知道这个是做什么用的.其实String类是Java中一个比较特殊的类,字符串即String类,它不是Java的基本数据类型之一,但可以像基 ...

  10. JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数

    JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数 示例如下 至少需要九个元素:"A","B","C",&q ...