最简spring IOC实现
public class Main {
public static void main(String[] args) throws Exception {
Class<Address> addressClass = Address.class;
Constructor<Address> declaredConstructor = addressClass.getDeclaredConstructor();
Field field1 = addressClass.getDeclaredField("address");
field1.setAccessible(true);
MyValue myValue = field1.getAnnotation(MyValue.class);
String value = myValue.value();
Address address = declaredConstructor.newInstance();
field1.set(address, value);
field1.setAccessible(false);
MyComponent annotation1 = addressClass.getAnnotation(MyComponent.class);
Map<String, Object> container = new HashMap<>();
container.put(annotation1.name(), address);
Class<MyService> myServiceClass = MyService.class;
Constructor<MyService> declaredConstructor1 = myServiceClass.getDeclaredConstructor();
MyService myService = declaredConstructor1.newInstance();
Field field2 = myServiceClass.getDeclaredField("name");
field2.setAccessible(true);
MyValue annotation = field2.getAnnotation(MyValue.class);
field2.set(myService, annotation.value());
field2.setAccessible(false);
Field field3 = myServiceClass.getDeclaredField("address");
field3.setAccessible(true);
field3.set(myService, container.get(field3.getAnnotation(MyResource.class).name()));
field3.setAccessible(false);
container.put(myServiceClass.getAnnotation(MyComponent.class).name(), myService);
for (Map.Entry<String, Object> entry : container.entrySet()) {
String key = entry.getKey();
Object value1 = entry.getValue();
System.out.println(key + " => " + value1);
}
}
}
@MyComponent(name="myService")
class MyService {
@MyValue(value="yury")
private String name;
@MyResource(name="address")
private Address address;
@Override
public String toString() {
return "MyService{" +
"name='" + name + '\'' +
", address=" + address +
'}';
}
}
@MyComponent(name="address")
class Address {
@MyValue(value="shanghai")
private String address;
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyComponent {
String name();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyValue {
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyResource {
String name();
}
最简spring IOC实现的更多相关文章
- 最简 Spring IOC 容器源码分析
前言 BeanDefinition BeanFactory 简介 Web 容器启动过程 bean 的加载 FactoryBean 循环依赖 bean 生命周期 公众号 前言 许多文章都是分析的 xml ...
- Spring(2)——Spring IoC 详解
Spring IoC 概述 IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控 ...
- 【死磕 Spring】----- IOC 之深入理解 Spring IoC
在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...
- 关于Spring IOC (DI-依赖注入)需要知道的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 <Spring入门经典>这本书无论对于初学者或者有 ...
- IOC 之深入理解 Spring IoC
在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...
- 关于Spring IOC (DI-依赖注入)
<Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初 ...
- Spring IOC容器启动流程源码解析(四)——初始化单实例bean阶段
目录 1. 引言 2. 初始化bean的入口 3 尝试从当前容器及其父容器的缓存中获取bean 3.1 获取真正的beanName 3.2 尝试从当前容器的缓存中获取bean 3.3 从父容器中查找b ...
- Spring IOC 巨多 非常 有用
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 <Spring入门经典>这本书无论对于初学者或者有 ...
- 从零开始手写 spring ioc 框架,深入学习 spring 源码
IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...
- 关于Spring IOC (DI-依赖注入)你需要知道的一切
<Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初 ...
随机推荐
- Nginx12 openresty使用lua-resty-http模块
1 简介 https://github.com/ledgetech/lua-resty-http 在lua中操作http请求有两种方式 第一种方式:使用通过ngx.location.capture 去 ...
- 解决Linux上tomcat解析war包中文文件乱码
解决Linux上tomcat解析war包中文文件乱码 第一步 编辑tomcat/conf server.xml vim /usr/local/src/tomcat/conf/server.xml us ...
- ACR20新闻_RA指南更新_最大化应用MTX
美国风湿病学院(ACR)学术年会2020新闻 译自Medscape官网 https://www.medscape.com/viewarticle/940636#vp_1 (注: 译文借助谷歌翻译助手, ...
- SpringBoot多数据源以及事务处理
背景 在高并发的项目中,单数据库已无法承载大数据量的访问,因此需要使用多个数据库进行对数据的读写分离,此外就是在微服化的今天,我们在项目中可能采用各种不同存储,因此也需要连接不同的数据库,居于这样的背 ...
- Canvas:绘制曲线
前言 画曲线要用到二次贝塞尔曲线或三次贝塞尔曲线.贝塞尔曲线是计算机图形学中相当重要的参数曲线,在一些比较成熟的位图软件中也有贝塞尔曲线工具,如 PhotoShop. 二次贝塞尔曲线 二次贝塞尔曲线在 ...
- LeetCode-846 一手顺子
来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/hand-of-straights 题目描述 Alice 手中有一把牌,她想要重新排列这些牌,分成 ...
- 7. Light (对象)
Light Mode:模式 Realtime: 实时的.就是当前光照效果是实时的,不包含烘焙效果(即使场景曾经烘焙过) Mixed: 混合的.就是既使用烘焙数据对静态对象(Lightmap stati ...
- swiper滑动异常
遇到这样的情况,一共5张slide,但是从第三个起再往右划就不动了.后来发现是已经初始化swiper后又追加了slide.(大概意思是这样.太久的程序了,也不想仔细捋了) 暂且用到的办法: 先定义好: ...
- Django models.py 表的参数选择
from django.db import models # Create your models here. class Department(models.Model): # 以后可以新增, ...
- java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)问题
此报错大多数存在的问题为mysql密码错误,需要去JDBC文件中寻找url查看是否密码错误.