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();
    }
}
> Task :spring-demo:OrderAnnotationExample.main()
MyBeanFactoryPostProcessor1
MyBeanFactoryPostProcessor2
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();
	}
}
> Task :spring-demo:OrderedInterfaceExample.main()
MyBeanFactoryPostProcessor2
MyBeanFactoryPostProcessor1

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值