SpringBoot项目引入SpringClouds时run方法被执行的两次解析

SpringBoot项目引入SpringClouds时run方法被执行的两次解析

在没用引入SpringCloud之前,可以看到environment资源实例是:StandardServletEnvironment

在这里插入图片描述

引入SpringClound之后,会发现第一次environment资源实例是:StandardEnvironment,第二次资源实例才是StandardServletEnvironment

在这里插入图片描述

疑惑: 为什么会出现这种情况呢?

让我们来一步步分析:

1.首先将断点定格在 
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
2.查看prepareEnvironment方法中
listeners.environmentPrepared(environment);
3.此刻会调用SpringApplicationRunListeners类中environmentPrepared方法
void environmentPrepared(ConfigurableEnvironment environment) {
//this.listeners只存在一个值,那就是EventPublishingRunListener
		for (SpringApplicationRunListener listener : this.listeners) {
			listener.environmentPrepared(environment);
		}
	}
4.从上得知,最终实际是调用了EventPublishingRunListener中的environmentPrepared
public void environmentPrepared(ConfigurableEnvironment environment) {
//initialMulticaster为SimpleApplicationEventMulticaster实例
 this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));}
5.那我们接着进入SimpleApplicationEventMulticaster的multicastEvent方法
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		Executor executor = getTaskExecutor();
//此方法getApplicationListeners会获取到创建SpringApplication实例时设置的listener属性,参照setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
			//首先会调用BootstrapApplicationListener中的onApplicationEvent方法
				invokeListener(listener, event);
			}
		}
	}
6.此刻进入到BootstrapApplicationListener类中的onApplicationEvent方法
context = bootstrapServiceContext(environment, event.getSpringApplication(),
					configName);
7.经过一顿操作后执行bootstrapServiceContext方法,关键是下面一段代码
final ConfigurableApplicationContext context = builder.run();
8.执行了builder.run()方法,即重新执行了SpringApplication中的run方法(类似递归调用,但实际是不同对象)
9.在启动参数中添加-Dspring.cloud.bootstrap.enabled = false来关闭引导类启动(操作如下图),该参数不能放在配置文件中,因为在启动bootstrap时,还未对配置文件进行加载

关闭bootstrap引导启动
至此分享到这。。。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
可以通过以下步骤在Spring Boot项目引入Netty: 1. 在项目的pom.xml文件中添加Netty的依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.63.Final</version> </dependency> ``` 2. 创建一个Netty服务器类,例如 `NettyServer`: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; public class NettyServer { private final int port; public NettyServer(int port) { this.port = port; } public void start() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new YourNettyHandler()); } }); ChannelFuture future = bootstrap.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } } ``` 3. 创建一个Netty处理器类,例如 `YourNettyHandler`,用于处理接收到的消息: ```java import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class YourNettyHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理接收到的消息 ByteBuf byteBuf = (ByteBuf) msg; // TODO: 处理消息逻辑 byteBuf.release(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 发生异常的处理逻辑 cause.printStackTrace(); ctx.close(); } } ``` 4. 在Spring Boot应用的入口类中启动Netty服务器,例如 `Application`: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); // 启动Netty服务器 NettyServer server = new NettyServer(8080); server.start(); } } ``` 这样,你就可以在Spring Boot项目中成功引入并使用Netty了。请根据自己的需求进行相应的配置和修改。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值