04-23.eri-test 如何在Spring应用程序启动时执行代码

\n

The blog is originally published on my blog jsblogs.github.io

\n\n\n
\n\n \n\n

\n \n \n Introduction \n

\n\n

Have you ever encountered a situation where you\'ve to perform some tasks immediately after the Spring/SpringBoot application starts. i.e.
\nInitialize some data into database, initialize application level constants, make an API call, etc.

\n\n

There are several ways to achieve it. Here I\'m gonna discuss about:

\n\n
  1. \n
  2. Application events\n
  3. ApplicationRunner\n
\n

\n \n \n Technologies used \n

\n\n
  1. \n
  2. Java 11\n
  3. Spring Boot 2.2.4\n
  4. Gradle 6.0.1\n
\n

\n \n \n Application events \n

\n\n

The Spring framework triggers various events. For our use case we\'ll be more interested in ContextStartedEvent and ContextRefreshedEvent.
\nContextStartedEvent event triggered at the time of context gets started.
\nContextRefreshedEvent event triggered at the time of context gets started or refreshed.
\n

\n\n
@Component\npublic class EventHandler {\n    @EventListener(ContextStartedEvent.class)\n    public void handleContextStartEvent(ContextStartedEvent e) {\n        // Write your code here\n    }\n\n    @EventListener(ContextRefreshedEvent.class)\n    public void handleContextRefreshEvent(ContextRefreshedEvent e) {\n        // Write your code here\n    }\n    // Or you can handle both the events in 1 method  \n\n    @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})\n    public void handleBoth(ApplicationContextEvent e) {\n        if (e instanceof ContextStartedEvent) {\n\n        } else {\n\n        }\n    }\n}\n
\n\n\n\n

\n \n \n ApplicationRunner \n

\n\n

SpringBoot provides an interface called ApplicationRunner, any bean implementing this interface should run when that contained in the SpringApplication.
\n

\n\n
@Component\npublic class DBInitializer implements ApplicationRunner {\n\n    private final UserRepository userRepository;\n\n    private DBInitializer(UserRepository userRepository) {\n        this.userRepository = userRepository;\n    }\n\n    @Override\n    public void run(ApplicationArguments args) throws Exception {\n        // Initialize user here\n    }\n}\n
\n\n\n\n

or the above can be used
\n

\n\n
@Configuration\npublic class Config {\n\n@Bean\n    public ApplicationRunner initializeUser(UserRepository userRepository) {\n        return args -> {\n            // Initialize user here\n        };\n    }\n}\n
\n\n\n\n

ApplicationRunner provides ApplicationArguments in the run method which is used to get command line arguments by invoking getSourceArgs().
\nYou can also get the parsed arguments using this class. i.e.

\n\n

Let\'s say you\'ve passed command line arguments like
\n--source /usr/local --print-only --target /tmp/local

\n\n

So the method call to

\n\n
  1. \n
  2. \ngetOptionNames() in ApplicationArguments will return set of arguments - [\'source\', \'print-only\', \'target\']\n
  3. \ncontainsOption(String name) checks if argument contains \n
  4. \ngetOptionValues(name) returns list of option values. getOptionValues(\'source\') will return list - [\'/usr/local\']\n
\n\n\n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值