最代码官方的gravatar头像
最代码官方 2021-01-25 23:55:59

Spring自定义类似@Required注解实现在容器启动时对bean进行依赖校验

项目描述

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

是通过Spring内置的@Required注解实现的bean依赖的校验

这一个代码通过自定义注解实现类似机制

运行环境

jdk8+IntelliJ IDEA+maven

项目技术(必填)

spring+自定义Annotation

依赖包文件(可选)

链接:https://pan.baidu.com/s/1mAmWlS3H3MPmcK7NYxHrvg
提取码:silt

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

转载https://memorynotfound.com/spring-custom-required-styled-annotation-example/

项目截图(必填)

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

注意事项(可选)

运行Main即可


打赏

文件名:custom-spring-@required-annotation-example.zip,文件大小:6.409K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友