实践!在Spring-Boot启动时添加方法运行,查漏补缺


这将产生以下日志输出:

In CommandLineRunnerImpl
status=running


如我们所见,该参数未解析,而是解释为单个值“ status = running”。

要访问已解析格式的命令行参数,我们需要使用ApplicationRunner接口。我们待会儿再看。

Spring Boot将CommandLineRunner接口添加到启动过程中。因此,在commandliner Runner中引发异常将迫使Spring启动中止启动。

我们可以在一个应用程序中创建多个CommandLineRunner。使用Ordered接口或@Order批注,我们可以配置它们的运行顺序。较低的值表示较高的优先级。默认情况下,所有组件均以最低优先级创建。这就是为什么没有订单配置的组件将被最后调用的原因。

我们可以使用订单注释,如下所示

@Component
@Order(1)
public class CommandLineRunnerImpl implements CommandLineRunner {

}


# 2.具有ApplicationRunner界面

如前所述,要访问已解析的参数,我们需要使用ApplicationRunner接口。ApplicationRunner接口为运行方法提供ApplicationArguments而不是原始字符串数组。

ApplicationArguments是一个接口,可从org.springframework.boot软件包下的boot 1.3 srping中获得。

它提供了以下几种访问参数的方法

![image.png](https://upload-images.jianshu.io/upload_images/24195226-f7a6e029033582f0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

方法getOptionValues返回值列表,因为参数值可以是数组,因为我们可以在命令行中多次使用同一键。 例如**–name** =“ stacktrace” —端口= 8080 **–name** =“ guru”

# 作为接口实现的应用程序运行器示例

让我们使用“ status = running –mood = happy 10 –20”参数运行以下程序,并了解输出

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) throws Exception {

  System.out.println("ApplicationRunnerImpl Called");

//print all arguemnts: arg: status=running, arg: --mood=happy, 10, --20
for (String arg : args.getSourceArgs()) {
System.out.println("arg: "+arg);
}
System.out.println("NonOptionArgs: "+args.getNonOptionArgs()); //[status=running,10]
System.out.println("OptionNames: "+args.getOptionNames()); //[mood, 20]

 System.out.println("Printing key and value in loop:");
  for (String key : args.getOptionNames()) {
     System.out.println("key: "+key);     //key: mood  //key: 20
     System.out.println("val: "+args.getOptionValues(key)); //val:[happy] //val:[]
  }

}
}


输出:

ApplicationRunnerImpl Called
arg: status=running
arg: --mood=happ
arg: 10
arg: --20
NonOptionArgs: [status=running , 10]
OptionNames: [mood, 20]
Printing key and value in loop:
key: mood
val: [happy]
key: 20
val: []


CommandLineRunner和ApplicationRunner具有类似的功能,例如

*   run()方法中的异常将中止应用程序启动
*   可以使用Ordered接口或@Order批注来订购多个ApplicationRunner

需要注意的最重要一点是,命令在CommandLineRunners和ApplicationRunners之间共享。这意味着可以在commandlinerRunner和applicationRunner之间混合执行顺序。

# 3\. Spring Boot中的应用程序事件

Spring框架在不同情况下触发不同事件。它还会在启动过程中触发许多事件。我们可以使用这些事件来执行代码,例如,在Spring Boot应用程序启动后,可以使用ApplicationReadyEvent执行代码。

如果我们不需要命令行参数,这是在应用程序启动后执行代码的最佳方法。

@Component
public class RunAfterStartup{

@EventListener(ApplicationReadyEvent.class)
public void runAfterStartup() {
System.out.println(“Yaaah, I am running…”);
}


输出:

Yaaah, I am running…


春季靴子中最重要的事件是

*   **ApplicationContextInitializedEvent** :在准备ApplicationContext并调用ApplicationContextInitializers之后但在加载bean定义之前触发
*   **ApplicationPreparedEvent** :在加载bean定义后触发
*   **ApplicationStartedEvent** :在刷新上下文之后但在调用命令行和应用程序运行程序之前触发
*   **ApplicationReadyEvent** :在调用任何应用程序和命令行运行程序之后触发
*   **ApplicationFailedEvent** :如果启动时发生异常则触发

可以创建多个ApplicationListeners。可以使用@Order批注或Ordered接口对其进行订购。

该顺序与其他相同类型的ApplicationListener共享,但不与ApplicationRunners或CommandLineRunners共享。

# 4.方法上的@Postconstruct注解

可以使用@PostConstruct批注标记方法。每当使用此注释标记方法时,将在依赖项注入后立即调用该方法。

@PostConstruct方法链接到特定的类,因此它仅应用于特定于类的代码。每个类只有一个带有postConstruct批注的方法。

@Component
public class PostContructImpl {

public PostContructImpl() {
    System.out.println("PostContructImpl Constructor called");
}

@PostConstruct
public void runAfterObjectCreated() {
    System.out.println("PostContruct method called");
}

}


输出:

PostContructImpl Constructor called
postContruct method called


需要注意的是,如果class标记为lazy,则意味着在请求时创建了class。之后,将执行标有@postConstruct批注的方法。

标有postConstruct批注的方法可以具有任何名称,但是不能具有任何参数。它必须是无效的,并且不能是静态的。

请注意,@postConstruct批注是Java EE模块的一部分,在Java 9中被标记为已弃用,在Java 11中已被删除。我们仍然可以通过将java.se.ee添加到应用程序中来使用它。

# 5\. InitializingBean接口

InitializingBean解决方案的工作原理与postConstruct批注完全相似。不必使用注释,我们必须实现InitializingBean接口。然后,我们需要重写afterPropertiesSet()方法。

InitializingBean是org.springframework.beans.factory包的一部分。

@Component
public class InitializingBeanImpl implements InitializingBean {
public InitializingBeanImpl() {
System.out.println(“InitializingBeanImpl Constructor called”);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(“InitializingBeanImpl afterPropertiesSet method called”);
}
}


您必须考虑如果同时使用@PostConstruct批注和InitializingBean会发生什么。那么在这种情况下,@PostConstruct方法将在InitializingBean的afterPropertiesSet()方法之前调用。

# 6\. @bean批注的init属性

我们可以在@Bean批注中使用initMethod属性提供一种方法。bean初始化后将调用此方法。

initMethod中提供的方法必须为空,并且不能包含任何参数。此方法甚至可以是私有的。

public class BeanInitMethodImpl {

public void runAfterObjectCreated() {
    System.out.println("yooooooooo......... someone called me");
}

}

@SpringBootApplication

最后总结

ActiveMQ+Kafka+RabbitMQ学习笔记PDF

image.png

  • RabbitMQ实战指南

image.png

  • 手写RocketMQ笔记

image.png

  • 手写“Kafka笔记”

image

关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦

笔记”

[外链图片转存中…(img-WdE7CTlT-1628440512546)]

关于分布式,限流+缓存+缓存,这三大技术(包含:ZooKeeper+Nginx+MongoDB+memcached+Redis+ActiveMQ+Kafka+RabbitMQ)等等。这些相关的面试也好,还有手写以及学习的笔记PDF,都是啃透分布式技术必不可少的宝藏。以上的每一个专题每一个小分类都有相关的介绍,并且小编也已经将其整理成PDF啦

资料领取方式:戳这里免费领取

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值