今天小編就爲大家分享一篇關於Spring組件開發模式支持SPEL表達式,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
本文是一個 Spring 擴展支持 SPEL 的簡單模式,方便第三方通過 Spring 提供額外功能。
簡化版方式
這種方式可以在任何能獲取ApplicationContext
的地方使用。還可以提取一個方法處理動態 SPEL 表達式。
import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.*; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.expression.StandardBeanExpressionResolver; import org.springframework.core.annotation.AnnotationUtils; import java.lang.reflect.Method; /** * 針對 Spring 實現某些特殊邏輯時,支持 SPEL 表達式 * @author liuzh */ public class SpelUtil implements ApplicationContextAware { /** * 通過 ApplicationContext 處理時 * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (applicationContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext; ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); StandardBeanExpressionResolver expressionResolver = new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()); for (String definitionName : applicationContext.getBeanDefinitionNames()) { BeanDefinition definition = beanFactory.getBeanDefinition(definitionName); Scope scope = (definition != null ? beanFactory.getRegisteredScope(definition.getScope()) : null); //根據自己邏輯處理 //例如獲取 bean Object bean = applicationContext.getBean(definitionName); //獲取實際類型 Class<?> targetClass = AopUtils.getTargetClass(bean); //獲取所有方法 for (Method method : targetClass.getDeclaredMethods()) { //獲取自定義的註解(Bean是個例子) Bean annotation = AnnotationUtils.findAnnotation(method, Bean.class); //假設下面的 value 支持 SPEL for (String val : annotation.value()) { //解析 ${} 方式的值 val = beanFactory.resolveEmbeddedValue(val); //解析 SPEL 表達式 Object value = expressionResolver.evaluate(val, new BeanExpressionContext(beanFactory, scope)); //TODO 其他邏輯 } } } } } }
上面是完全針對ApplicationContext
的,下面是更推薦的一種用法。
推薦方式
import org.springframework.aop.support.AopUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.*; import org.springframework.context.annotation.Bean; import org.springframework.context.expression.StandardBeanExpressionResolver; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.ReflectionUtils; /** * 針對 Spring 實現某些特殊邏輯時,支持 SPEL 表達式 * @author liuzh */ public class SpelUtil2 implements BeanPostProcessor, BeanFactoryAware, BeanClassLoaderAware { private BeanFactory beanFactory; private BeanExpressionResolver resolver; private BeanExpressionContext expressionContext; /** * 解析 SPEL * @param value * @return */ private Object resolveExpression(String value){ String resolvedValue = resolve(value); if (!(resolvedValue.startsWith("#{") && value.endsWith("}"))) { return resolvedValue; } return this.resolver.evaluate(resolvedValue, this.expressionContext); } /** * 解析 ${} * @param value * @return */ private String resolve(String value){ if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) { return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value); } return value; } @Override public void setBeanClassLoader(ClassLoader classLoader) { this.resolver = new StandardBeanExpressionResolver(classLoader); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; if(beanFactory instanceof ConfigurableListableBeanFactory){ this.resolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver(); this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null); } } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } /** * 對 bean 的後置處理 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { //獲取實際類型 Class<?> targetClass = AopUtils.getTargetClass(bean); //獲取所有方法 ReflectionUtils.doWithMethods(targetClass, method -> { //獲取自定義的註解(Bean是個例子) Bean annotation = AnnotationUtils.findAnnotation(method, Bean.class); //假設下面的 value 支持 SPEL for (String val : annotation.value()) { //解析表達式 Object value = resolveExpression(val); //TODO 其他邏輯 } }, method -> { //TODO 過濾方法 return true; }); return null; } }
這種方式利用了 Spring 生命週期的幾個接口來獲取需要用到的對象。
Spring 生命週期調用順序
擴展 Spring 我們必須瞭解這個順序,否則就沒法正確的使用各中對象。
完整的初始化方法及其標準順序是:
- BeanNameAware 的 setBeanName 方法
- BeanClassLoaderAware 的 setBeanClassLoader 方法
- BeanFactoryAware 的 setBeanFactory 方法
- EnvironmentAware 的 setEnvironment 方法
- EmbeddedValueResolverAware 的 setEmbeddedValueResolver 方法
- ResourceLoaderAware 的 setResourceLoader 方法 (僅在應用程序上下文中運行時適用)
- ApplicationEventPublisherAware 的 setApplicationEventPublisher 方法 (僅在應用程序上下文中運行時適用)
- MessageSourceAware 的 setMessageSource 方法 (僅在應用程序上下文中運行時適用)
- ApplicationContextAware 的 setApplicationContext 方法 (僅在應用程序上下文中運行時適用)
- ServletContextAware 的 setServletContext 方法 (僅在Web應用程序上下文中運行時適用)
- BeanPostProcessors 的 postProcessBeforeInitialization 方法
- InitializingBean 的 afterPropertiesSet 方法
- 自定義初始化方法
- BeanPostProcessors 的 postProcessAfterInitialization 方法
關閉bean工廠時,以下生命週期方法適用:
- DestructionAwareBeanPostProcessors 的 postProcessBeforeDestruction 方法
- DisposableBean 的 destroy 方法
- 自定義銷燬方法
靈活運用
利用上述模式可以實現很多便捷的操作。
Spring 中,使用類似模式的地方有:
- @Value 註解支持 SPEL(和 ${})
- @Cache 相關的註解(支持 SPEL)
- @EventListener 註解
- @RabbitListener 註解
- …
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接