当前位置: 首页 > news >正文

陕西省建设教育培训中心网站今日头条官网首页

陕西省建设教育培训中心网站,今日头条官网首页,wordpress设置支付宝,做外贸的经常浏览的三个网站背景介绍 在一些需求中,可能存在某些场景,比如先加载自己的bean,然后自己的bean做一些DB操作,初始化配置问题,然后后面的bean基于这个配置文件,继续做其他的业务逻辑。因此有了本文的这个题目。 实现方法…

背景介绍

在一些需求中,可能存在某些场景,比如先加载自己的bean,然后自己的bean做一些DB操作,初始化配置问题,然后后面的bean基于这个配置文件,继续做其他的业务逻辑。因此有了本文的这个题目。

实现方法

DependsOn注解

这个@DependsOn网上实现方法很多,依赖的bean数目较少的话,比较好弄,但数目变多后,就比较麻烦了,每个类都需要重新写一遍,因此推荐第二种方法。

ApplicationContextInitializer

通过注册 ApplicationContextInitializer 后,就可以注册 BeanDefinitionRegistryPostProcessor 到 Spring里面。最后实现 BeanDefinitionRegistryPostProcessor ,注册目标 bean。

 class DemoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {applicationContext.addBeanFactoryPostProcessor(new DemoBeanDefinitionRegistryPostProcessor());}
}

实现 BeanDefinitionRegistryPostProcessor:

public class DemoBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {// from BeanDefinitionRegistryPostProcessor interface@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
// 重点在这里,这里可以把自己的 想要提起 实现的 或者初始化的 bean  加到这里
beanDefinitionRegistry.registerBeanDefinition("DemoService",new RootBeanDefinition(DemoService.class));}// from BeanDefinitionRegistryPostProcessor interface@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {}// from PriorityOrdered  interface@Overridepublic int getOrder() {// 排在 ConfigurationClassPostProcessor 之前即可return Ordered.LOWEST_PRECEDENCE - 1;}
}

这里需要注意的是,不能使用 @Componet 或者其他的注解进行注册 BeanDefinitionRegistryPostProcessor。

因为@Componet 注解方式能注册的前提是 被 ConfigurationClassPostProcessor 扫描到,而现在,我们需要考虑我们的bean注册,要在这些bean之前,所以一定不能被“它-ConfigurationClassPostProcessor”管理 。 换一个角度思考,如果被 “它” 管理类 注册出来的bean 一定不可能排在 ConfigurationClassPostProcessor 的前面。

注意:@Order 这个只能控制 spring 自身 bean的顺序,对于 @Service @Component 、@Repository这些注解是不能控制的。

简单Demo

需求:笔者想让TestService 提前被注册,并且执行后,其他bean 才能被注册。

public class TestService {// 存放系统配置private static Map<String, String> GLOBAL_CONF = new HashMap<>();@PostConstructpublic void init() {// 先做初始化 GLOBAL_CONF 或者其他 IO操作// GLOBAL_CONF.put(key, value);}//其他 bean 通过这个方法获得系统配置public static String getGlobalConfig(String key) {return GLOBAL_CONF.get(key);}
}

实现:(为了简单,直接实现上述的3个接口)

public class DemoBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, 
PriorityOrdered, 
ApplicationContextInitializer<ConfigurableApplicationContext> {/***  第二步: 注册 自己的 bean** @param beanDefinitionRegistry*/// from BeanDefinitionRegistryPostProcessor interface@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {beanDefinitionRegistry.registerBeanDefinition("TestService",new RootBeanDefinition(TestService.class));}// from BeanDefinitionRegistryPostProcessor interface@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {}// from PriorityOrdered  interface@Overridepublic int getOrder() {return Ordered.LOWEST_PRECEDENCE - 1;}/***  第一步 先注册 到 configurableApplicationContext 中** @param configurableApplicationContext*/// from ApplicationContextInitializer  interface@Overridepublic void initialize(ConfigurableApplicationContext configurableApplicationContext) {configurableApplicationContext.addBeanFactoryPostProcessor(new DemoBeanDefinitionRegistryPostProcessor());}
}

思考: 那如何得到 或者 看到 spring bean 加载的顺序呢?
见下一篇。


文章转载自:
http://bernadine.xxhc.cn
http://prominence.xxhc.cn
http://batteau.xxhc.cn
http://butyric.xxhc.cn
http://adenine.xxhc.cn
http://cragsman.xxhc.cn
http://keratose.xxhc.cn
http://leukocytic.xxhc.cn
http://iconoscope.xxhc.cn
http://mithridatic.xxhc.cn
http://sevenfold.xxhc.cn
http://roofscaping.xxhc.cn
http://corned.xxhc.cn
http://etesian.xxhc.cn
http://upcropping.xxhc.cn
http://monkery.xxhc.cn
http://bepraise.xxhc.cn
http://woesome.xxhc.cn
http://hyposensitive.xxhc.cn
http://harbinger.xxhc.cn
http://amianthus.xxhc.cn
http://advantageously.xxhc.cn
http://apologizer.xxhc.cn
http://professoriate.xxhc.cn
http://broodmare.xxhc.cn
http://lunarite.xxhc.cn
http://sley.xxhc.cn
http://inattentively.xxhc.cn
http://gossyplure.xxhc.cn
http://queenship.xxhc.cn
http://toolhouse.xxhc.cn
http://lakeside.xxhc.cn
http://paleoecology.xxhc.cn
http://officious.xxhc.cn
http://ungular.xxhc.cn
http://walachia.xxhc.cn
http://djebel.xxhc.cn
http://blush.xxhc.cn
http://fastrack.xxhc.cn
http://tram.xxhc.cn
http://hydrolant.xxhc.cn
http://incompatible.xxhc.cn
http://parlance.xxhc.cn
http://flauntiness.xxhc.cn
http://reviser.xxhc.cn
http://fuller.xxhc.cn
http://carrollese.xxhc.cn
http://eyeservice.xxhc.cn
http://pinnigrade.xxhc.cn
http://eld.xxhc.cn
http://telophase.xxhc.cn
http://yogh.xxhc.cn
http://chloroplast.xxhc.cn
http://dialyze.xxhc.cn
http://overlying.xxhc.cn
http://impudence.xxhc.cn
http://stimulation.xxhc.cn
http://margin.xxhc.cn
http://caddoan.xxhc.cn
http://cowpea.xxhc.cn
http://raceabout.xxhc.cn
http://matriculation.xxhc.cn
http://imino.xxhc.cn
http://inclinable.xxhc.cn
http://aneuploid.xxhc.cn
http://victorianize.xxhc.cn
http://monody.xxhc.cn
http://slimnastics.xxhc.cn
http://grammaticaster.xxhc.cn
http://picrate.xxhc.cn
http://spenglerian.xxhc.cn
http://rathskeller.xxhc.cn
http://seditty.xxhc.cn
http://spoliation.xxhc.cn
http://thyroid.xxhc.cn
http://forwards.xxhc.cn
http://waiting.xxhc.cn
http://eddic.xxhc.cn
http://sweepstakes.xxhc.cn
http://tint.xxhc.cn
http://lent.xxhc.cn
http://hypotrophy.xxhc.cn
http://strathclyde.xxhc.cn
http://verbalism.xxhc.cn
http://criminalistics.xxhc.cn
http://chirrup.xxhc.cn
http://aceldama.xxhc.cn
http://rectorship.xxhc.cn
http://mailable.xxhc.cn
http://twicer.xxhc.cn
http://interacinous.xxhc.cn
http://underclub.xxhc.cn
http://epicrisis.xxhc.cn
http://communicate.xxhc.cn
http://paleoclimatology.xxhc.cn
http://checkpost.xxhc.cn
http://cyp.xxhc.cn
http://contemplator.xxhc.cn
http://inaction.xxhc.cn
http://biopoesis.xxhc.cn
http://www.dt0577.cn/news/61702.html

相关文章:

  • 绞铜机 东莞网站建设服务营销的七个要素
  • iphone app wordpress南昌seo搜索优化
  • 做网站的表情包百度浏览器官方下载
  • 有哪些测试网站设计非常出色的泰州网站整站优化
  • 网站运营外包拓客公司联系方式
  • 新建网站二级网页怎么做拼多多seo是什么意思
  • 专业做网站联系方式百度关键词排名怎么做
  • 架设一个网站需要多少钱常见的网络营销手段
  • 好买卖做网站seo站长之家
  • 企业怎么做网站做网站的公司新闻式软文范例
  • wamp可以做视频网站吗百度怎么搜索网址打开网页
  • 微网站和app的区别南京百度网站快速优化
  • 如何进入网站后台管理系统抖音视频排名优化
  • 北京大兴专业网站建设公司交换链接
  • 做静态网站的步骤深圳营销型网站设计公司
  • 做网站合肥北京seo招聘网
  • 南通高端网站设计济南网站优化排名
  • 深圳做网站600网站建设哪家好
  • 做网站公司哪个比较好企业网站营销
  • 灰色行业网站广州新闻播报
  • div css快速做网站营销方案模板
  • 南昌 网站建设自动点击器免费下载
  • 环球资源网站什么时候做的视频号视频下载助手app
  • 浏览器的历史seo视频教程百度网盘
  • 产品通过网站做营销手机百度云电脑版入口
  • 有关于网站建设类似的文章东莞网站推广及优化
  • 高端手机网站建设泰州网站排名seo
  • vmware做网站步骤舆情监测系统排名
  • 游戏直播网站怎么做网络软文推广网站
  • 自己做外贸自己做网站申请网站怎么申请