最代码官方的gravatar头像
最代码官方2021-01-24 22:29:36

Spring通过@Required注解实现在容器启动时对bean进行依赖校验

项目描述

该注解@Required是方法级别的注解,一般是在set方法上添加

运行环境

jdk8+IntelliJ IDEA+maven

项目技术(必填)

[email protected]

依赖包文件(可选)

链接:https://pan.baidu.com/s/1VZVlYNzEi81np3Shf7OCCQ
提取码:zl7t

是否原创(转载必填原文地址)

转载https://memorynotfound.com/dependency-checking-with-spring-required-annotation/

项目截图(必填)

Spring通过@Required注解实现在容器启动时对bean进行依赖校验

运行截图(必填)

Spring通过@Required注解实现在容器启动时对bean进行依赖校验

Spring通过@Required注解实现在容器启动时对bean进行依赖校验

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'luke' defined in class path resource [app-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'theForce' is required for bean 'luke'
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.memorynotfound.spring.core.required.Main.main(Main.java:9)
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'theForce' is required for bean 'luke'
	at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	... 11 more

Process finished with exit code 1

默认情况下@Required是不启动校验的,只有在如下情况下任意一种才会有效

1.初始化RequiredAnnotationBeanPostProcessor

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"></bean>

2.配置<context:annotation-config/>

3.配置<context:component-scan/>

以上3种情况都不存在的情况下,@Required是无效的,spring正常运行

Spring通过@Required注解实现在容器启动时对bean进行依赖校验

注意事项(可选)

idea也可以在编码时校验并提示,而不用等到编译运行时刻

Spring通过@Required注解实现在容器启动时对bean进行依赖校验


打赏

顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友