springboot2.x解决运⾏顺序及Bean对象注⼊顺序的问题⽬录
1前⾔
2测试
3@Dependon和@Order
4利⽤BeanFactoryPostProcessor初始化
1 前⾔
通过指定接⼝,重写指定⽅法,可以在Bean对应的⽣命周期⽅法中执⾏相应的程序
2 测试
本⽂将分析⼏个Bean对象,为它们设置优先级(通过@Order),然后再打断点调试,测试各种⽣命周期⽅法的运⾏的顺序
在项⽬当中最让⼈头疼的就是bean对象不被注⼊的问题,通过本⽂,你可以很好的解决这个问题。
先看看本程序使⽤的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wu</groupId>
<artifactId>smartport</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>smartport</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>spring boot依赖注入原理
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.1 Order = 1
package com.ller;
import com.st.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.fig.AutowireCapableBeanFactory;
import org.springframework.fig.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import t.ApplicationContext;
import t.ApplicationContextAware;
import annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class TestController implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanDefinitionRegistryPostProcessor, InitializingBean, DisposableBean { @Autowired
private User user;
/**
* 最先执⾏,需要接⼝BeanNameAware
*
* @param name
*/
@Override
public void setBeanName(String name) {
// 获取到的是testController,即注⼊进Spring中的名称
System.out.println("setBeanName:" + name);
}
/**
* 第2个执⾏,需要接⼝BeanFactoryAware
* beanFactory可以获取其他已注⼊的Bean对象,但⽆法注⼊其他对象
*
* @param beanFactory
* @throws BeansException
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
}
/**
* 第3个执⾏,需要ApplicationContextAware接⼝
* 可以获取到上下⽂对象applicationContext,允许容器通过应⽤程序上下⽂环境创建、获取、管理bean
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 可以通过上下⽂对象获取⼯⼚并注⼊对象
AutowireCapableBeanFactory beanFactory = AutowireCapableBeanFactory();
}
/**
* 第4个执⾏,依赖于InitializingBean接⼝
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
}
/**
* 第5个执⾏,依赖于BeanDefinitionRegistryPostProcessor接⼝
* 当完成本⽅法后,对象就会完成注⼊
* @param registry
* @throws BeansException
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
}
/**
* 第6个执⾏,来源于BeanFactoryPostProcessor接⼝,该接⼝被BeanDefinitionRegistryPostProcessor接⼝实现了。
* ⼀般的Bean对象在此⽅法前都被注⼊到了容器中,如果本对象没有获取到,则可以在这⾥获取。
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Object user = Bean("user");
}
/**
* 第7个执⾏,本⽅法将被多次执⾏,依赖于BeanPostProcessor接⼝
* 每注⼊⼀个对象的时候,都会调⽤该⽅法
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return null;
}
/**
* 第8个执⾏,本⽅法将被多次执⾏,依赖于BeanPostProcessor接⼝
* 每注⼊⼀个对象之后,都会调⽤该⽅法
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return null;
}
/**
* 在本对象被销毁后执⾏,依赖于DisposableBean接⼝
* @throws Exception
*/
@Override
public void destroy() throws Exception {
}
}
2.2 Order = 15
package com.st;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.AutowireCapableBeanFactory;
import org.springframework.fig.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import t.ApplicationContext;
import t.ApplicationContextAware;
import annotation.Order;
import org.springframework.stereotype.Service;
/**
* @Author :吴⽤
* @Date :2021-01-05 8:40
* @Version :1.0
*/
@Service
@Order(15)
public class User implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanDefinitionRegistryPostProcessor, InitializingBean, DisposableBean { @Value("张三")
private String name;
@Value("12")
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 最先执⾏,需要接⼝BeanNameAware
*
* @param name
*/
@Override
public void setBeanName(String name) {
System.out.println("setBeanName:" + name);
}
/**
* 第2个执⾏,需要接⼝BeanFactoryAware
* beanFactory可以获取其他已注⼊的Bean对象,但⽆法注⼊其他对象
*
* @param beanFactory
* @throws BeansException
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
}
/**
* 第3个执⾏,需要ApplicationContextAware接⼝
* 可以获取到上下⽂对象applicationContext,允许容器通过应⽤程序上下⽂环境创建、获取、管理bean
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
}
/**
* 第4个执⾏,依赖于InitializingBean接⼝
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
}
/**
* 第5个执⾏,依赖于BeanDefinitionRegistryPostProcessor接⼝
* 当完成本⽅法后,对象就会完成注⼊
* @param registry
* @throws BeansException
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
}
/**
* 第6个执⾏,来源于BeanFactoryPostProcessor接⼝,该接⼝被BeanDefinitionRegistryPostProcessor接⼝实现了。 * ⼀般的Bean对象在此⽅法前都被注⼊到了容器中,如果本对象没有获取到,则可以在这⾥获取。
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Object user = Bean("user");
}
/**
* 第7个执⾏,本⽅法将被多次执⾏,依赖于BeanPostProcessor接⼝
* 每注⼊⼀个对象的时候,都会调⽤该⽅法
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return null;
}
/**
* 第8个执⾏,本⽅法将被多次执⾏,依赖于BeanPostProcessor接⼝
* 每注⼊⼀个对象之后,都会调⽤该⽅法
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return null;
}
/**
* 在本对象被销毁后执⾏,依赖于DisposableBean接⼝
* @throws Exception
*/
@Override
public void destroy() throws Exception {
}
}
2.3 Order = 30
package com.st;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import t.ApplicationContext;
import t.ApplicationContextAware;
import annotation.Order;
import org.springframework.stereotype.Component;
/**
* @Author :吴⽤
* @Date :2021-01-05 9:48
* @Version :1.0
*/
@Order(30)
@Component
public class User2 implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanDefinitionRegistryPostProcessor, InitializingBean, DisposableBean { @Value("张三")
private String name;
@Value("12")
private int age;
public User2() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 最先执⾏,需要接⼝BeanNameAware
*
* @param name
*/
@Override
public void setBeanName(String name) {
System.out.println("setBeanName:" + name);
}
/**
* 第2个执⾏,需要接⼝BeanFactoryAware
* beanFactory可以获取其他已注⼊的Bean对象,但⽆法注⼊其他对象
*
* @param beanFactory
* @throws BeansException
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
}
/**
* 第3个执⾏,需要ApplicationContextAware接⼝
* 可以获取到上下⽂对象applicationContext,允许容器通过应⽤程序上下⽂环境创建、获取、管理bean
*
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
}
/**
* 第4个执⾏,依赖于InitializingBean接⼝
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
}
/**
* 第5个执⾏,依赖于BeanDefinitionRegistryPostProcessor接⼝
* 当完成本⽅法后,对象就会完成注⼊
* @param registry
* @throws BeansException
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
}
/**
* 第6个执⾏,来源于BeanFactoryPostProcessor接⼝,该接⼝被BeanDefinitionRegistryPostProcessor接⼝实现了。
* ⼀般的Bean对象在此⽅法前都被注⼊到了容器中,如果本对象没有获取到,则可以在这⾥获取。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论