最代码官方2021-01-24 22:29:36
证
Spring通过@Required注解实现在容器启动时对bean进行依赖校验
项目描述
该注解@Required是方法级别的注解,一般是在set方法上添加
运行环境
jdk8+IntelliJ IDEA+maven
项目技术(必填)
依赖包文件(可选)
链接:https://pan.baidu.com/s/1VZVlYNzEi81np3Shf7OCCQ
提取码:zl7t
是否原创(转载必填原文地址)
转载https://memorynotfound.com/dependency-checking-with-spring-required-annotation/
项目截图(必填)
运行截图(必填)
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正常运行
注意事项(可选)
idea也可以在编码时校验并提示,而不用等到编译运行时刻
猜你喜欢
- spring通过@Value注解注入到java List属性
- spring实现忽略禁用@Required注解的实例
- Spring通过@Autowired注解实现Bean依赖注入的简单实例
- spring通过p-namespace的方式实现xml配置bean的属性实例
- spring通过c-namespace的方式实现xml配置bean构造函数实例
- Spring通过properties文件的配置实现bean初始化的简单示例
- Spring通过配置component-scan base-package实现自动扫码指定路径的类并初始化bean的简单示例
- 如何优雅的关闭Spring容器并销毁Bean实例
- spring通过3种写法来inject value到bean的property中。
- 自己手动模拟spring框架的IOC(依赖注入,DI,控制反转)
- Spring两种常用的事务管理范例AspectJ和基于注解方式
- spring通过@Async注解实现异步执行Service方法并且通过Java Future对象得到执行结果
请下载代码后再发表评论
文件名:[email protected],文件大小:5.236K下载
- /
- /[email protected]
- /[email protected]/pom.xml
- /[email protected]/src
- /[email protected]/src/main
- /[email protected]/src/main/java
- /[email protected]/src/main/java/com
- /[email protected]/src/main/java/com/memorynotfound
- /[email protected]/src/main/java/com/memorynotfound/spring
- /[email protected]/src/main/java/com/memorynotfound/spring/core
- /[email protected]/src/main/java/com/memorynotfound/spring/core/required
- /[email protected]/src/main/java/com/memorynotfound/spring/core
- /[email protected]/src/main/java/com/memorynotfound/spring
- /[email protected]/src/main/java/com/memorynotfound
- /[email protected]/src/main/java/com
- /[email protected]/src/main/java
- /[email protected]/src/main
- /[email protected]

- Spring两种常用的事务管理范例AspectJ和基于注解方式
- 证spring通过@Async注解实现异步执行Service方法并且通过Java Future对象得到执行结果
- Java Spring定时器Demo, 可自动跳过节假日
- 证spring通过p-namespace的方式实现xml配置bean的属性实例
- 证spring通过c-namespace的方式实现xml配置bean构造函数实例
- 证Spring通过properties文件的配置实现bean初始化的简单示例
- 证Spring生命周期接口InitializingBean和DisposableBean的实例演示
- spring 各种demo实例
- 证Spring通过配置component-scan base-package实现自动扫码指定路径的类并初始化bean的简单示例
- 证Spring自定义类似@Required注解实现在容器启动时对bean进行依赖校验
- Java Spring的三种注入方式
- [工作必备]spring定时器简单的demo


3399509525 LV24月14日
liuqingfang LV24月2日
水光浮藻 LV53月25日
gfdgjkfodijigidfj LV12月10日
a1525723924 LV32月7日
hhhhllll122月7日
暂无贡献等级
vaterz LV252月7日
yuanmafx LV22月1日
浙江螃蟹 LV71月27日
297129499171月26日
暂无贡献等级