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.

你可能感兴趣的:(spring,java,后端)