Spring bean的创建,其本质上还是一个对象的创建,包含两部分:当前对象实例化和对象属性的实例化;
![](https://img.kancloud.cn/e3/15/e31505e7ff8e84e51c85f101267efd70_2066x714.png)
实现原理:
~~~
public class Appliction {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
BeanFactory beanFactory = new BeanFactory();
Object bean = beanFactory.getBean(ObjectA.class);
System.err.println(bean);
}
}
public class BeanFactory {
private Map<String, Object> cacheMap = new HashMap<>();
public Object getBean(Class cla) throws IllegalAccessException, InstantiationException {
String simpleName = cla.getSimpleName();
System.err.println(simpleName);
if (cacheMap.containsKey(simpleName)) {
return cacheMap.get(simpleName);
}
Object instance = cla.newInstance();
cacheMap.put(simpleName, instance);
Field[] fields = cla.getDeclaredFields();
for (Field field : fields) {
boolean present = field.isAnnotationPresent(AutoIoc.class);
if (present) {
Class<?> declaringClass = field.getType();
Object obj = getBean(declaringClass);
field.setAccessible(true);
field.set(instance, obj);
}
}
return instance;
}
}
public class ObjectA {
@AutoIoc
private ObjectB objectB;
}
public class ObjectB {
@AutoIoc
private ObjectA objectA;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
public @interface AutoIoc {
}
~~~
- 概述
- Spring的使命
- 环境变量
- spring架构
- Spring各版本特性
- Spring3.1新特性
- spring顶级项目
- spring基础
- spring环境变量
- 依赖注入
- spring获取bean方法
- BeanFactory vs FactoryBean
- JavaBean装配
- XML显式配置
- 基于JAVA配置
- 自动化装配bean
- SpringBean的作用域
- Spring应用上下文实现
- springbean的生命周期
- 自定义Bean的创建与销毁
- Spring容器启动过程
- spring加载xsd文件的流程
- spring扩展接口
- Spring主要类功能说明
- spring事务管理
- 事务特性
- 数据库事务隔离级别
- 事务隔离性问题
- spring事务隔离级别
- 事务传播行为
- @Transactional
- 循环依赖
- 构造器注入
- 循环依赖原理
- spring循环依赖原理
- spring三级缓存
- Spring注解
- @Component
- @ComponentScan
- @Autowired
- @Import
- @ImportResource
- @Profile
- @Conditional
- @Qualifier
- @Scope
- @PropertySource
- @Value
- @EnableScheduling
- SpEL-Spring表达式
- Spring-AOP
- SpringAOP五种通知类型
- AOP术语
- SpringMVC
- MVC原理图
- SpringMVC工作原理
- springboot
- @SpringCloudApplication
- springboot tomcat配置
- Spring Boot Starter POMs
- Spring Boot technical starters
- spring boot事件类型
- Springboot日志
- SpringCloud
- springcloud微服务解决方案
- 服务组件
- 注册中心
- Eureka
- Spring Cloud Zookeeper
- nacos
- Hystrix熔断原理
- Hystrix应用
- Spring Cloud Config
- 服务网关
- Zuul
- Spring Cloud Gateway
- 服务调用及负载
- Ribbon
- Feign&OpenFeign
- Turbine
- actuator
- springboot & springcloud
- springcloud vs dubbo
- 常见面试题
- BeanFactory和FactoryBean
- @Autowired/@Resource和@Inject的区别
- Singleton bean注入prototype bean
- 附录