送大家以下java学习材料,文末有支付形式

假如有这么一个需要,要求在我的项目启动过程中,实现线程池的初始化,加密证书加载等性能,你会怎么做?如果没想好答案,请接着往下看。明天介绍几种在Spring Boot中进行资源初始化的形式,帮忙大家解决和答复这个问题。

CommandLineRunner

  • 定义初始化类 MyCommandLineRunner
  • 实现 CommandLineRunner 接口,并实现它的 run() 办法,在该办法中编写初始化逻辑
  • 注册成Bean,增加 @Component注解即可
  • 示例代码如下:
package cn.zh.controller;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Componentpublic class MyCommandLineRunner implements CommandLineRunner {       @Override    public void run(String... args) throws Exception {        System.out.println("我的项目初始化---------------11");    }}

实现了 CommandLineRunner 接口的 Component 会在所有 Spring Beans 初始化实现之后, 在 SpringApplication.run() 执行之前实现。上面通过加两行打印来验证咱们的测试。

package cn.zh;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProcApplication {    public static void main(String[] args) {        System.out.println("...start SpringApplication.run");        SpringApplication.run(ProcApplication.class,args);        System.out.println("....end SpringApplication.run");    }}

ApplicationRunner

  • 定义初始化类 MyApplicationRunner
  • 实现 ApplicationRunner 接口,并实现它的 run() 办法,在该办法中编写初始化逻辑
  • 注册成Bean,增加 @Component注解即可
  • 示例代码如下:
package cn.zh.controller;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Componentpublic class MyApplicationRunner implements ApplicationRunner {    @Override    public void run(ApplicationArguments args) throws Exception {        System.out.println("我的项目初始化二---------");    }   }

能够看到,通过实现 ApplicationRunner 接口,和通过实现 CommandLineRunner 接口都能够实现我的项目的初始化操作,实现雷同的成果。两者之间惟一的区别是 run() 办法中自带的形参不雷同,在 CommandLineRunner 中只是简略的String... args形参,而 ApplicationRunner 则是蕴含了 ApplicationArguments 对象,能够帮忙取得更丰盛的我的项目信息。

@Order

如果我的项目中既有实现了 ApplicationRunner 接口的初始化类,又有实现了 CommandLineRunner 接口的初始化类,那么会是哪一个先执行呢?测试通知咱们,答案是实现了 ApplicationRunner 接口的初始化类先执行,我想这点倒是不须要大家过分去关注为什么。但如果须要扭转两个初始化类之间的默认执行程序,那么应用 @Order 注解就能够帮忙咱们解决这个问题。

@Component@Order(1)public class MyCommandLineRunner implements CommandLineRunner {    /**     * Callback used to run the bean.     *     * @param args incoming main method arguments     * @throws Exception on error     */    @Override    public void run(String... args) throws Exception {        System.out.println("我的项目初始化---------------11");    }}@Component@Order(2)public class MyApplicationRunner implements ApplicationRunner {    @Override    public void run(ApplicationArguments args) throws Exception {        System.out.println("我的项目初始化二---------");    }    @PostConstruct    public void init(){        System.out.println("@PostConstruct初始化");    }}

@PostConstruct

应用 @PostConstruct 注解同样能够帮忙咱们实现资源的初始化操作,前提是这些初始化操作不须要依赖于其它Spring beans的初始化工作。

能够看到 @PostConstruct 注解是用在办法上的,写一个办法测试一下吧。

@PostConstruct    public void init(){        System.out.println("@PostConstruct初始化");    }
  • 留神:
  • 只有一个非静态方法能应用此注解
  • 被注解的办法不得有任何参数
  • 被注解的办法返回值必须为void
  • 被注解办法不得抛出已查看异样
  • 此办法只会被执行一次
  • 综上,应用 @PostConstruct 注解进行初始化操作的程序是最快的,前提是这些操作不能依赖于其它Bean的初始化实现。通过增加 @Order 注解,咱们能够扭转同层级之间不同Bean的加载程序。

InitializingBean

InitializingBean 是 Spring 提供的一个接口,只蕴含一个办法 afterPropertiesSet()。但凡实现了该接口的类,当其对应的 Bean 交由 Spring 治理后,当其必要的属性全副设置实现后,Spring 会调用该 Bean 的 afterPropertiesSet()。

@Componentpublic class MyListener1 implements InitializingBean {    @Autowired    private ShopInfoMapper shopInfoMapper;    @Override    public void afterPropertiesSet() {        //应用spring容器中的bean        //System.out.println(shopInfoMapper.selectById("1").getShopName());        System.out.println("我的项目启动OK");    }}

ApplicationListener

ApplicationListener 就是spring的监听器,可能用来监听事件,典型的观察者模式。如果容器中有一个ApplicationListener Bean,每当ApplicationContext公布ApplicationEvent时,ApplicationListener Bean将主动被触发。这种事件机制都必须须要程序显示的触发。

其中spring有一些内置的事件,当实现某种操作时会收回某些事件动作。比方监听ContextRefreshedEvent事件,当所有的bean都初始化实现并被胜利装载后会触发该事件,实现ApplicationListener接口能够收到监听动作,而后能够写本人的逻辑。

同样事件能够自定义、监听也能够自定义,齐全依据本人的业务逻辑来解决。所以也能做到资源的初始化加载。

@Componentpublic class MyListener1 implements ApplicationListener {    @Override    public void onApplicationEvent(ApplicationEvent applicationEvent) {                //打印出每次事件的名称        System.out.println(applicationEvent.toString());                if (applicationEvent instanceof ApplicationReadyEvent) {            System.out.println("我的项目启动OK");        }    }}