一、常见的两个扩展点

1、ApplicationContextInitializer

1.1、作用实现

作用:接口实在Spring容器执行refresh之前的一个回调。

Callback interface for initializing a Spring {@link ConfigurableApplicationContext}

实现:

/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.context; /**
* Callback interface for initializing a Spring {@link ConfigurableApplicationContext}
* prior to being {@linkplain ConfigurableApplicationContext#refresh() refreshed}.
*
* <p>Typically used within web applications that require some programmatic initialization
* of the application context. For example, registering property sources or activating
* profiles against the {@linkplain ConfigurableApplicationContext#getEnvironment()
* context's environment}. See {@code ContextLoader} and {@code FrameworkServlet} support
* for declaring a "contextInitializerClasses" context-param and init-param, respectively.
*
* <p>{@code ApplicationContextInitializer} processors are encouraged to detect
* whether Spring's {@link org.springframework.core.Ordered Ordered} interface has been
* implemented or if the @{@link org.springframework.core.annotation.Order Order}
* annotation is present and to sort instances accordingly if so prior to invocation.
*
* @author Chris Beams
* @since 3.1
* @see org.springframework.web.context.ContextLoader#customizeContext
* @see org.springframework.web.context.ContextLoader#CONTEXT_INITIALIZER_CLASSES_PARAM
* @see org.springframework.web.servlet.FrameworkServlet#setContextInitializerClasses
* @see org.springframework.web.servlet.FrameworkServlet#applyInitializers
*/
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> { /**
* Initialize the given application context.
* @param applicationContext the application to configure
*/
void initialize(C applicationContext); }

1.2、使用步骤

步骤一、写一个类,实现ApplicationContextInitializer接口

步骤二、注册ApplicationContextInitializer接口的实现

注册方式

方式一、SpringApplication

        SpringApplication application = new SpringApplication(App.class);
application.addInitializers(new MyApplicationContextInitializer());

方式二、配置文件

context.initializer.classes=com.lhx.spring.springboot_enableauto.MyApplicationContextInitializer

多个用逗号隔开

方式三、使用spring.factories配置机制,注册监听器

在需要注入的项目包中添加META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=类名

1.3、使用:

新建一个:MyApplicationContextInitializer

package com.lhx.spring.springboot_enableauto;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("bean count:" + applicationContext.getBeanDefinitionCount());
// ConfigurableListableBeanFactory factory =
// applicationContext.getBeanFactory();
} }

调用

package com.lhx.spring.springboot_enableauto;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class App {
public static void main(String[] args) {
// ConfigurableApplicationContext context = SpringApplication.run(App.class,
// args);
SpringApplication application = new SpringApplication(App.class);
application.addInitializers(new MyApplicationContextInitializer());
ConfigurableApplicationContext context = application.run(args); context.close();
}
}

2、CommandLineRunner、ApplicationRunner【类似开机自启动】

2.1、作用于实现

作用:在spring容器全部初始化完毕,接口在容器启动成功后的最后一步回调

* Interface used to indicate that a bean should <em>run</em> when it is contained within
* a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
* within the same application context and can be ordered using the {@link Ordered}
* interface or {@link Order @Order} annotation.

实现

/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.boot; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; /**
* Interface used to indicate that a bean should <em>run</em> when it is contained within
* a {@link SpringApplication}. Multiple {@link CommandLineRunner} beans can be defined
* within the same application context and can be ordered using the {@link Ordered}
* interface or {@link Order @Order} annotation.
* <p>
* If you need access to {@link ApplicationArguments} instead of the raw String array
* consider using {@link ApplicationRunner}.
*
* @author Dave Syer
* @see ApplicationRunner
*/
public interface CommandLineRunner { /**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception; }

2.2、使用步骤

步骤一、写一个类实现CommandLineRunner接口

步骤二、把该类加入到Spring容器之中,可以通过@Order或Ordered接口控制顺序,注解越小越先执行

2.3、使用

新建一个类ServerSuccessReport实现接口CommandLineRunner

package com.lhx.spring.springboot_enableauto;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; @Component
public class ServerSuccessReport implements CommandLineRunner { @Override
public void run(String... args) throws Exception {
System.out.println("----CommandLineRunner执行-----");
} }

注意同类接口

ApplicationRunner,

void run(ApplicationArguments args) 

只是参数不一样

CommandLineRunner是原始参数,没有任何处理

ApplicationRunner是ApplicationArguments 对象,是对原始参数进一步的封装

3.ApplicationArguments

是对参数(main方法)进一步重载

可以解析--name=value的,可以通过name获取value

package com.lhx.spring.springboot_enableauto;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
ConfigurableApplicationContext context = application.run(args); ApplicationArguments arguments = context.getBean(ApplicationArguments.class);
System.out.println(arguments.getSourceArgs().length);
System.out.println(arguments.getOptionNames());
System.out.println(arguments.getOptionValues("name")); context.close();
}
}

最新文章

  1. bzoj1415[NOI2005]聪聪和可可-期望的线性性
  2. PHP用法
  3. PyCharm3.0默认快捷键(翻译的)
  4. PHP读取word文档
  5. Android高仿微信图片选择功能的PhotoPicker
  6. php---将数组转化为数组对象
  7. hdu 2196
  8. MongoDB入门三步曲1--安装、基本操作
  9. 给Apache加载rewrite模块后,服务器返回500错误,以及a2enmod命令
  10. Spring MVC 解读——@RequestMapping (2)(转)
  11. Source Insight设置总结
  12. Linux 终端颜色高亮
  13. weak引用变量是否线程安全
  14. CentOS + EPEL YUM源地址
  15. java-两个jre目录和三个lib目录-转
  16. (原创) Maven查看JAR包的依赖关系
  17. JavaScript脚本放在哪里用户体验好
  18. C# 处理PPT水印(二)——去除水印效果(文本水印、图片水印)
  19. 295B - Greg and Graph (floyd逆序处理)
  20. taro之React Native 端开发研究

热门文章

  1. 【转】Hadoop 1.x中fsimage和edits合并实现
  2. C#导出大量数据到excel,怎么提升性能
  3. Hive内部表,外部表和分区表
  4. 53. Maximum Subarray (JAVA)
  5. python 变量、列表、元组、字典
  6. c#中DataTable和DataSet区别
  7. 瞎JB逆
  8. 动态SQL的注意
  9. PKUSC2019颓废记
  10. mongdb 学习