原文网址:Spring--BeanPostProcessor--注意/问题/坑--SpringBoot_IT利刃出鞘的博客-CSDN博客
简介
本文用实例来介绍Spring的后置处理器(BeanPostProcessor)的一些坑,开发时要注意这些问题。
坑1:依赖的bean不走BeanPostProcessor
简介
在BeanPostProcessor的实现类中,依赖了其他的bean,那么被依赖的bean被创建时,将不会执行它所在的BeanPostProcessor实现类实现的方法。
因为由于BeanPostProcessor的实现类依赖于其他bean,所以这个bean需要在PostBean之前创建完成,这也就意味着在这个bean创建时,BeanPostProcessor的实现类还未初始化完成,所以不会调用它的方法。
以下用实例进行验证
BeanPostProcessor实现类
package com.example.processor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyProcessor implements BeanPostProcessor {
@Autowired
private MyBean myBean;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyBean) {
System.out.println("postProcessBeforeInitialization==> " + "This is MyBean");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MyBean) {
System.out.println("postProcessAfterInitialization==> " + "This is MyBean");
}
return bean;
}
}
被依赖的Bean
package com.example.processor;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
}
测试
启动后,没有相关打印。
若将@Autowired MyBean myBean删掉,则启动之后,会打印出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2021-03-05 21:39:47.861 INFO 16100 --- [ main] com.example.DemoApplication : Starting DemoApplication on DESKTOP-QI6B9ME with PID 16100 (E:\work\Idea_proj\demo_JAVA\demo_SpringBoot\target\classes started by Liu in E:\work\Idea_proj\demo_JAVA\demo_SpringBoot)
2021-03-05 21:39:47.864 INFO 16100 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-05 21:39:48.645 INFO 16100 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-05 21:39:48.652 INFO 16100 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-05 21:39:48.652 INFO 16100 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2021-03-05 21:39:48.730 INFO 16100 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-05 21:39:48.731 INFO 16100 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 825 ms
postProcessBeforeInitialization==> This is MyBean
postProcessAfterInitialization==> This is MyBean
2021-03-05 21:39:48.865 INFO 16100 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-05 21:39:48.998 INFO 16100 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-05 21:39:49.007 INFO 16100 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.557 seconds (JVM running for 2.638)
坑2:无法使用AOP
见:Spring--BeanPostProcessor无法使用AOP--原因/解决方案_IT利刃出鞘的博客-CSDN博客
坑3:注册方式及其限制
上边是文章的部分内容,为便于维护,全文已转移到此网址:Spring-BeanPostProcessor-注意问题/坑 - 自学精灵