马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前言
在使用spring的过程中,我们有没有发现它的扩展本领很强呢? 由于这个优势的存在,使得spring具有很强的包容性,所以很多第三方应用大概框架可以很轻易的投入到spring的怀抱中。今天我们主要来学习Spring中很常用的11个扩展点,你用过几个呢?
1. 类型转换器
假如接口中接收参数的实体对象中,有一个字段类型为Date,但实际通报的参数是字符串类型:2022-12-15 10:20:15,该如那边理惩罚?
Spring提供了一个扩展点,类型转换器Type Converter,具体分为3类:
- Converter<S,T>: 将类型 S 的对象转换为类型 T 的对象
- ConverterFactory<S, R>: 将 S 类型对象转换为 R 类型或其子类对象
- GenericConverter:它支持多种源和目的类型的转换,还提供了源和目的类型的上下文。 此上下文答应您根据注释或属性信息实行类型转换。
还是不明白的话,我们举个例子吧。
- @Data
- public class User {
- private Long id;
- private String name;
- private Date registerDate;
- }
复制代码
- public class DateConverter implements Converter<String, Date> {
- private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- @Override
- public Date convert(String source) {
- if (source != null && !"".equals(source)) {
- try {
- simpleDateFormat.parse(source);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
复制代码
- @Configuration
- public class WebConfig extends WebMvcConfigurerAdapter {
- @Override
- public void addFormatters(FormatterRegistry registry) {
- registry.addConverter(new DateConverter());
- }
- }
复制代码
- @RequestMapping("/user")
- @RestController
- public class UserController {
- @RequestMapping("/save")
- public String save(@RequestBody User user) {
- return "success";
- }
- }
复制代码
哀求接口时,前端传入的日期字符串,会自动转换成Date类型。
2. 获取容器Bean
在我们一样平常开辟中,常常必要从Spring容器中获取bean,但是你知道怎样获取Spring容器对象吗?
2.1 BeanFactoryAware
- @Service
- public class PersonService implements BeanFactoryAware {
- private BeanFactory beanFactory;
- @Override
- public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
- this.beanFactory = beanFactory;
- }
- public void add() {
- Person person = (Person) beanFactory.getBean("person");
- }
- }
复制代码
实现BeanFactoryAware接口,然后重写setBeanFactory方法,可以从方法中获取spring容器对象。
2.2 ApplicationContextAware
- @Service
- public class PersonService2 implements ApplicationContextAware {
- private ApplicationContext applicationContext;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- this.applicationContext = applicationContext;
- }
- public void add() {
- Person person = (Person) applicationContext.getBean("person");
- }
- }
复制代码
实现ApplicationContextAware接口,然后重写setApplicationContext方法,也可以通过该方法获取spring容器对象。
2.3 ApplicationListener
- @Service
- public class PersonService3 implements ApplicationListener<ContextRefreshedEvent> {
- private ApplicationContext applicationContext;
- @Override
- public void onApplicationEvent(ContextRefreshedEvent event) {
- applicationContext = event.getApplicationContext();
- }
- public void add() {
- Person person = (Person) applicationContext.getBean("person");
- }
- }
复制代码
3. 全局异常处理惩罚
以往我们在开辟界面的时间,假如出现异常,要给用户更友好的提示,比方:
- @RequestMapping("/test")
- @RestController
- public class TestController {
- @GetMapping("/add")
- public String add() {
- int a = 10 / 0;
- return "su";
- }
- }
复制代码
假如不对哀求添加接口结果做任那边理惩罚,会直接报错:
用户可以直接看到错误信息吗?
这种交互给用户带来的体验非常差。 为相识决这个问题,我们通常在接口中捕获异常:
- @GetMapping("/add")
- public String add() {
- String result = "success";
- try {
- int a = 10 / 0;
- } catch (Exception e) {
- result = "error";
- }
- return result;
- }
复制代码
界面修改后,出现异常时会提示:“数据异常”,更加人性化。
看起来不错,但是有一个问题。
假如只是一个接口还好,但是假如项目中有成百上千个接口,还得加异常捕获代码吗?
答案是否定的,这就是全局异常处理惩罚派上用场的地方:RestControllerAdvice。
- @RestControllerAdvice
- public class GlobalExceptionHandler {
- @ExceptionHandler(Exception.class)
- public String handleException(Exception e) {
- if (e instanceof ArithmeticException) {
- return "data error";
- }
- if (e instanceof Exception) {
- return "service error";
- }
- retur null;
- }
- }
复制代码
方法中处理惩罚异常只必要handleException,在业务接口中就可以安心使用,不再必要捕获异常(统一有人处理惩罚)。
4. 自界说拦截器
Spring MVC拦截器,它可以得到HttpServletRequest和HttpServletResponse等web对象实例。
Spring MVC拦截器的顶层接口是HandlerInterceptor,它包罗三个方法:
- preHandle 在目的方法实行之前实行
- 实行目的方法后实行的postHandle
- afterCompletion 在哀求完成时实行
为了方便,我们一样平常继续HandlerInterceptorAdapter,它实现了HandlerInterceptor。
假如有授权鉴权、日志、统计等场景,可以使用该拦截器,我们来演示下吧。
- 写一个类继续HandlerInterceptorAdapter:
- public class AuthInterceptor extends HandlerInterceptorAdapter {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- String requestUrl = request.getRequestURI();
- if (checkAuth(requestUrl)) {
- return true;
- }
- return false;
- }
- private boolean checkAuth(String requestUrl) {
- return true;
- }
- }
复制代码
- @Configuration
- public class WebAuthConfig extends WebMvcConfigurerAdapter {
- @Bean
- public AuthInterceptor getAuthInterceptor() {
- return new AuthInterceptor();
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new AuthInterceptor());
- }
- }
复制代码
- Spring MVC在哀求接口时可以自动拦截接口,并通过拦截器验证权限。
5. 导入配置
偶然我们必要在某个配置类中引入其他的类,引入的类也加入到Spring容器中。 这时间可以使用注解@Import来完成这个功能。
假如你检察它的源代码,你会发现导入的类支持三种不同的类型。
但是我觉得最好把平常类的配置类和@Configuration注解分开表明,所以列出了四种不同的类型:
5.1 通用类
这种引入方式是最简单的,引入的类会被实例化为一个bean对象。
- public class A {
- }
- @Import(A.class)
- @Configuration
- public class TestConfiguration {
-
- }
复制代码
通过@Import注解引入类A,spring可以自动实例化A对象,然后在必要使用的地方通过注解@Autowired注入:
5.2 配置类
这种引入方式是最复杂的,因为@Configuration支持还支持多种组合注解,比如:
- @Import
- @ImportResource
- @PropertySource
- public class A {
- }
- public class B {
- }
- @Import(B.class)
- @Configuration
- public class AConfiguration {
- @Bean
- public A a() {
- return new A();
- }
- }
- @Import(AConfiguration.class)
- @Configuration
- public class TestConfiguration {
- }
复制代码
@Configuration注解的配置类通过@Import注解导入,配置类@Import、@ImportResource相关注解引入的类会一次性全部递归引入@PropertySource所在的属性。
5.3 ImportSelector
该导入方法必要实现ImportSelector接口
- public class AImportSelector implements ImportSelector {
- private static final String CLASS_NAME = "com.sue.cache.service.test13.A";
- public String[] selectImports(AnnotationMetadata importingClassMetadata) {
- return new String[]{CLASS_NAME};
- }
- }
- @Import(AImportSelector.class)
- @Configuration
- public class TestConfiguration {
- }
复制代码
这种方法的好处是selectImports方法返回的是一个数组,也就是说可以同时引入多个类,非常方便。
5.4 ImportBeanDefinitionRegistrar
该导入方法必要实现ImportBeanDefinitionRegistrar接口:
- public class AImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
- @Override
- public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
- RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(A.class);
- registry.registerBeanDefinition("a", rootBeanDefinition);
- }
- }
- @Import(AImportBeanDefinitionRegistrar.class)
- @Configuration
- public class TestConfiguration {
- }
复制代码
这种方法是最机动的。 容器注册对象可以在registerBeanDefinitions方法中获取,可以手动创建BeanDefinition注册到BeanDefinitionRegistry种。
6. 当工程启动时
偶然候我们必要在项目启动的时间自界说一些额外的功能,比如加载一些体系参数,完成初始化,预热当地缓存等。 我们应该做什么?
好消息是 SpringBoot 提供了:
- CommandLineRunner
- ApplicationRunner
这两个接口资助我们实现了上面的需求。
它们的用法很简单,以ApplicationRunner接口为例:
- @Component
- public class TestRunner implements ApplicationRunner {
- @Autowired
- private LoadDataService loadDataService;
- public void run(ApplicationArguments args) throws Exception {
- loadDataService.load();
- }
- }
复制代码 oid run(ApplicationArguments args) throws Exception { loadDataService.load(); } } 复制代码
实现ApplicationRunner接口,重写run方法,在该方法中实现您的自界说需求。
假如项目中有多个类实现了ApplicationRunner接口,怎样指定它们的实行顺序?
答案是使用@Order(n)注解,n的值越小越早实行。 固然,顺序也可以通过@Priority注解来指定。
7. 修改BeanDefinition
在实例化Bean对象之前,Spring IOC必要读取Bean的相关属性,保存在BeanDefinition对象中,然后通过BeanDefinition对象实例化Bean对象。
假如要修改BeanDefinition对象中的属性怎么办?
答案:我们可以实现 BeanFactoryPostProcessor 接口。
- @Component
- public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
- DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
- BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
- beanDefinitionBuilder.addPropertyValue("id", 123);
- beanDefinitionBuilder.addPropertyValue("name", "Tom");
- defaultListableBeanFactory.registerBeanDefinition("user", beanDefinitionBuilder.getBeanDefinition());
- }
- }
复制代码
在postProcessBeanFactory方法中,可以获取BeanDefinition的相关对象,修改对象的属性。
8. 初始化 Bean 前和后
偶然,您想在 bean 初始化前后实现一些您自己的逻辑。
这时间就可以实现:BeanPostProcessor接口。
该接口现在有两个方法:
- postProcessBeforeInitialization:应该在初始化方法之前调用。
- postProcessAfterInitialization:此方法在初始化方法之后调用。
- @Component
- public class MyBeanPostProcessor implements BeanPostProcessor {
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- if (bean instanceof User) {
- ((User) bean).setUserName("Tom");
- }
- return bean;
- }
- }
复制代码
我们常常使用的@Autowired、@Value、@Resource、@PostConstruct等注解都是通过AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor来实现的。
9. 初始化方法
现在在Spring中初始化bean的方式有很多种:
- 使用@PostConstruct注解
- 实现InitializingBean接口
9.1 使用 @PostConstruct
- @Service
- public class AService {
- @PostConstruct
- public void init() {
- System.out.println("===init===");
- }
- }
复制代码
为必要初始化的方法添加注解@PostConstruct,使其在Bean初始化时实行。
9.2 实现初始化接口InitializingBean
- @Service
- public class BService implements InitializingBean {
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("===init===");
- }
- }
复制代码
实现InitializingBean接口,重写afterPropertiesSet方法,在该方法中可以完成初始化功能。
10. 关闭Spring容器前
偶然候,我们必要在关闭spring容器之前做一些额外的工作,比如关闭资源文件。
此时你可以实现 DisposableBean 接口并重写它的 destroy 方法。
- @Service
- public class DService implements InitializingBean, DisposableBean {
- @Override
- public void destroy() throws Exception {
- System.out.println("DisposableBean destroy");
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("InitializingBean afterPropertiesSet");
- }
- }
复制代码
如许,在spring容器烧毁之前,会调用destroy方法做一些额外的工作。
通常我们会同时实现InitializingBean和DisposableBean接口,重写初始化方法和烧毁方法。
11. 自界说Bean的scope
我们都知道spring core默认只支持两种Scope:
- Singleton单例,从spring容器中获取的每一个bean都是同一个对象。
- prototype多实例,每次从spring容器中获取的bean都是不同的对象。
Spring Web 再次扩展了 Scope,添加
- RequestScope:同一个哀求中从spring容器中获取的bean都是同一个对象。
- SessionScope:同一个session从spring容器中获取的bean都是同一个对象。
只管如此,有些场景还是不符合我们的要求。
比如我们在同一个线程中要从spring容器中获取的bean都是同一个对象,怎么办?
答案:这必要一个自界说范围。
- public class ThreadLocalScope implements Scope {
- private static final ThreadLocal THREAD_LOCAL_SCOPE = new ThreadLocal();
- @Override
- public Object get(String name, ObjectFactory<?> objectFactory) {
- Object value = THREAD_LOCAL_SCOPE.get();
- if (value != null) {
- return value;
- }
- Object object = objectFactory.getObject();
- THREAD_LOCAL_SCOPE.set(object);
- return object;
- }
- @Override
- public Object remove(String name) {
- THREAD_LOCAL_SCOPE.remove();
- return null;
- }
- @Override
- public void registerDestructionCallback(String name, Runnable callback) {
- }
- @Override
- public Object resolveContextualObject(String key) {
- return null;
- }
- @Override
- public String getConversationId() {
- return null;
- }
- }
复制代码
- @Component
- public class ThreadLocalBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
- beanFactory.registerScope("threadLocalScope", new ThreadLocalScope());
- }
- }
复制代码- @Scope("threadLocalScope")
- @Service
- public class CService {
- public void add() {
- }
- }
复制代码
总结
本文总结了Spring中很常用的11个扩展点,可以在Bean创建、初始化到烧毁各个阶段注入自己想要的逻辑,也有Spring MVC相关的拦截器等扩展点,希望对各人有资助。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |