没有合适的资源?快使用搜索试试~ 我知道了~
丛林探险之Spring自定义注解加载Bean
0 下载量 137 浏览量
2020-12-20
21:51:52
上传
评论 1
收藏 78KB PDF 举报
温馨提示
试读
3页
文章目录丛林背景角色(一)自定义注解 StrategyBean(二)自定义注解 StrategyBeanScan(三)自定义注册机 StrategyBeanScanRegistrar(四)自定义Bean工厂后置处理器StrategyBeanAnnotationBeanPostProcessor时序图源码地址 丛林背景 自定义注解加载Bean是Spring框架提供的一个扩展点。基于这个扩展点可以实现灵活加载Bean的功能。 例如 Dubbo框架通过这个扩展点将添加了自定义注解@org.apache.dubbo.config.annotation.Service和@org.apache.dubbo
资源详情
资源评论
资源推荐
丛林探险之丛林探险之Spring自定义注解加载自定义注解加载Bean
文章目录文章目录丛林背景角色(一)自定义注解 StrategyBean(二)自定义注解 StrategyBeanScan(三)自定义注册机
StrategyBeanScanRegistrar(四)自定义Bean工厂后置处理器StrategyBeanAnnotationBeanPostProcessor时序图源码地址
丛林背景丛林背景
自定义注解加载Bean是Spring框架提供的一个扩展点。基于这个扩展点可以实现灵活加载Bean的功能。
例如 Dubbo框架通过这个扩展点将添加了自定义注解@org.apache.dubbo.config.annotation.Service和
@org.apache.dubbo.config.annotation.Reference的类加载到Spring的Ioc容器中。
今天我们像Dubbo框架一样自定义注解和必要的处理类来演绎自定义注解加载到Ioc容器的过程。
角色角色
(一)自定义注解(一)自定义注解 StrategyBean
/**
* 自定义注解
* 添加该自定义注解的类会被动态添加到Ioc容器
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface StrategyBean {
String value();
}
@StrategyBean("beijing")
public class BeijingStrategy {
public BeijingStrategy(){
System.out.println("BeijingStrategy Created");
}
}
@StrategyBean("hangzhou")
public class HangzhouStrategy {
public HangzhouStrategy(){
System.out.println("HangzhouStrategy Created");
}
}
(二)自定义注解(二)自定义注解 StrategyBeanScan
这个类有两个功能
1) 组合了了一个注解 @Import(StrategyBeanScanRegistrar.class),这个Import注解会将StrategyBeanScanRegistrar加载到
Ioc容器中,Ioc容器会在后续调用加载进来的StrategyBeanScanRegistrar的registerBeanDefinitions方法
2) 配置要扫描的包路径
这个注解可以添加在启动类上
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(StrategyBeanScanRegistrar.class)
public @interface StrategyBeanScan {
String[] basePackages() default {};
Class[] basePackageClasses() default {};
String[] value() default {};
}
@StrategyBeanScan(basePackages = {"com.liuapi.incubator.repository"})
public class RepositoryApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(RepositoryApplication.class);
}
}
(三)自定义注册机(三)自定义注册机 StrategyBeanScanRegistrar
StrategyBeanScanRegistrar实现了ImportBeanDefinitionRegistrar 接口,他的职责就是将工厂后置处理器
StrategyBeanAnnotationBeanPostProcessor加载到Ioc容器中
weixin_38565801
- 粉丝: 3
- 资源: 971
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0