1、order注解(不一定有效)
org.springframework.core.annotation.Order
2、dependsOn注解(有效)
org.springframework.context.annotation.DependsOn
3、提前将bean注册为BeanDefinition
java
1、实现BeanDefinitionRegistryPostProcessor,将userService注册
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(UserService.class);
registry.registerBeanDefinition("userService", beanDefinition);
}
}
2、加入上下文中
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addBeanFactoryPostProcessor(new MyBeanDefinitionRegistryPostProcessor());
}
}
3、配置扫描生效,在resources下创建MATE-INF文件夹,在MATE-INF文件夹下创建spring.factories文件,然后加入下面的配置
org.springframework.context.ApplicationContextInitializer=com.example.springbootshutdown.MyApplicationContextInitializer
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor