自定义迷你版Spring框架
一、需求概述
自定义@Service、@Autowired、@Transactional注解类,完成基于注解的IOC容器(Bean对象创建及依赖注入维护)和声明式事务控制,写到转账工程中,并且可以实现转账成功和转账异常时事务回滚
二、实现思路分析
根据需求的描述,我们可以参考spring框架的xml文件启动方式简单实现
1.创建配置文件beans.xml 配置一个包扫描路径
2.根据包扫描路径我们可以得到包以及子包下的所有类全限定名
3.根据反射对带有@Service注解的类进行实例化存到缓存并把有实例对象的bean以及beanDefinition放入缓存
4.对带有Autowired注解的属性注入对象实例并赋值
三、主要代码实现展示
public class DefaultAnnotationHandler {
private Map<Class<?>, BeanDefinition> hasInstanceBeanCache = new HashMap<>();
public void processAnnotations(List<String> allClassName) {
if (allClassName != null) {
for (String className : allClassName) {
//根据类名对bean做加载注册到缓存map对象
processAnnotations(className);
}
}
}
public void processAnnotations(String className) {
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (clazz == null) {
return;
}
//如果反射获取到的类不为空且不是接口且不是注解的话做处理
if (clazz != null && !clazz.isInterface() && !clazz.isAnnotation()) {
// AAA.处理对象实例化
Service service = clazz.getAnnotation(Service.class);
//如果类上有@Service注解做处理
if (service != null) {
// 0.根据当前类名称注入 把beanDefinition(当前类的代理对象和bean实例)注入map
BeanDefinition beanDefinition = BeanFactory.registryBeanByInterface(clazz.getName(), clazz);
// 1.service注解有指定id
String id = service.value();
if (!StringUtils.isEmpty(id)) {
//如果在注解上加上了命名id则根据id注入beanDefinition
BeanFactory.registryBean(id, beanDefinition);
}
// 2.未指定ID,则根据父接口名注入beanDefinition
Class<?>[] interfaces = clazz.getInterfaces();
if (interfaces != null) {
for (Class<?> anInterface : interfaces) {
BeanFactory.registryBeanByInterface(anInterface.getName(), beanDefinition);
}
}
//把有实例对象的bean以及beanDefinition放入缓存
hasInstanceBeanCache.put(clazz, beanDefinition);
}
}
}
//对带有Autowired注解的属性注入对象实例
public void doProcessPropertiesBeanAutowired() {
for (Map.Entry<Class<?>, BeanDefinition> entry : hasInstanceBeanCache.entrySet()) {
Class<?> aClass = entry.getKey();
//获取有实例对象bean的所有属性
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
Autowired autowired = field.getAnnotation(Autowired.class);
//如果属性带有Autowired注解且required=true那么就给属性依赖注入
if (autowired != null && autowired.required()) {
field.setAccessible(true);
//根据属性类名获取对象
Object bean = BeanFactory.getBean(field.getType());
try {
//获取BeanDefinition
BeanDefinition needInjected = entry.getValue();
//把对象实例赋值给对象
field.set(needInjected.getInstance(), bean);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
hasInstanceBeanCache.clear();
}
}
四、效果演示
在工程中能拿到注解对应的bean说明我们的IOC容器管理是没有问题的
然后在转账中故意制造了一个异常测试事务的效果
工程结构
五、总结
通过以上需求那么我们对基本的自定义Spring框架是有了一定的了解,那么我们可以更好的理解spring的思想,对于看spring源码
也可以有一定的思路,希望大家有兴趣的哥们之间多探讨技术,有助于彼此的提升,不好的地方欢迎指出。
注意:数据源需要换成自己的本地数据库信息,工程中附带demo的sql脚本
代码地址:https://gitee.com/xiangaiya/csdn/tree/master/lagou-transfer