• Spring (二)@Order, Ordered 失效


    Spring (二)@Order, Ordered 失效

    先上例子

    public class OrderAnnotationExample {
    
        @Order(2)
        static class MyBeanFactoryPostProcessor1 implements BeanFactoryPostProcessor {
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                System.out.println("MyBeanFactoryPostProcessor1");
            }
        }
    
        @Order(1)
        static class MyBeanFactoryPostProcessor2 implements BeanFactoryPostProcessor {
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                System.out.println("MyBeanFactoryPostProcessor2");
            }
        }
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
            context.register(MyBeanFactoryPostProcessor1.class, MyBeanFactoryPostProcessor2.class);
            context.refresh();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    > Task :spring-demo:OrderAnnotationExample.main()
    MyBeanFactoryPostProcessor1
    MyBeanFactoryPostProcessor2
    
    • 1
    • 2
    • 3
    public class OrderedInterfaceExample {
    
    	static class MyBeanFactoryPostProcessor1 implements BeanFactoryPostProcessor, Ordered {
    		@Override
    		public int getOrder() {
    			return 2;
    		}
    
    		@Override
    		public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    			System.out.println("MyBeanFactoryPostProcessor1");
    		}
    	}
    
    	static class MyBeanFactoryPostProcessor2 implements BeanFactoryPostProcessor, Ordered {
    		@Override
    		public int getOrder() {
    			return 1;
    		}
    
    		@Override
    		public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    			System.out.println("MyBeanFactoryPostProcessor2");
    		}
    	}
    
    	public static void main(String[] args) {
    		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    		context.register(MyBeanFactoryPostProcessor1.class, MyBeanFactoryPostProcessor2.class);
    		context.refresh();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    > Task :spring-demo:OrderedInterfaceExample.main()
    MyBeanFactoryPostProcessor2
    MyBeanFactoryPostProcessor1
    
    • 1
    • 2
    • 3

    BeanFactoryPostProcessor 文档中有一句话

    Factory hook that allows for custom modification of an application context’s bean definitions, adapting the bean property values of the context’s underlying bean factory.
    Useful for custom config files targeted at system administrators that override bean properties configured in the application context. See PropertyResourceConfigurer and its concrete implementations for out-of-the-box solutions that address such configuration needs.
    A BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side effects. If bean instance interaction is required, consider implementing BeanPostProcessor instead.
    Registration
    An ApplicationContext auto-detects BeanFactoryPostProcessor beans in its bean definitions and applies them before any other beans get created. A BeanFactoryPostProcessor may also be registered programmatically with a ConfigurableApplicationContext.
    Ordering
    BeanFactoryPostProcessor beans that are autodetected in an ApplicationContext will be ordered according to org.springframework.core.PriorityOrdered and org.springframework.core.Ordered semantics. In contrast, BeanFactoryPostProcessor beans that are registered programmatically with a ConfigurableApplicationContext will be applied in the order of registration; any ordering semantics expressed through implementing the PriorityOrdered or Ordered interface will be ignored for programmatically registered post-processors. Furthermore, the @Order annotation is not taken into account for BeanFactoryPostProcessor beans.

  • 相关阅读:
    【Unity Shader】屏幕后处理1.0:调整亮度/饱和度/对比度
    基于VSR-GUI的视频去字幕水印
    “目标检测”任务基础认识
    VR航天科普体验馆VR航空馆规划遨游太空感受其中乐趣
    计算机毕业设计(附源码)python校园疫情防控管理软件
    UI自动化测试单例实现报错:AttributeError: ‘NoneType‘ object has no attribute ‘get_driver‘
    计算机毕业设计Java超市进销存管理系统(源码+系统+mysql数据库+lw文档)
    (附源码)springboot火车票售卖系统 毕业设计 211004
    Spring(SpringBoot)--解决拦截器中注入Bean失败的问题
    减少Cache失效开销例题1(多级Cache)
  • 原文地址:https://blog.csdn.net/endswell/article/details/134543516