模仿 spring IOC Annotation版自动装配
spring 有两大核心 IOC和AOP。 IOC (inversion of control) 译为 控制反转,也可以称为 依赖注入 ; AOP(Aspect Oriented Programming)即面向切面编程。
我们此次所模仿的是 spring IOC 中的 Annotation 版的自动装配;Spring 在2.5版本后 引入了 @Autowired 以及一系列的 Annotation,它可以对类成员变量、方法及构造函数进行注解,完成自动装配的工作。相比于我们繁琐的传统xml配置注入来说,Annotation 的自动装配会更加简便,给需要注入的属性或方法加上** @Autowired** 后即可对该属性或方法进行自动注入,如果我们的属性是接口,
或者是一个父类的话,我们可以再加一个 @Qualifier 并为其设置一个value 即可指定该接口或该父类所指定的`
实现类或子类(对应于实现类或父类中 @Component 中的value)。
一、实现功能:
Annotation版的spring自动装配
二、实现思路:
spring ioc 底层也是基于java反射技术实现的,本次模仿牵扯到很多关于java反射方面的知识,如果各位小伙伴对java反射还不是太了解的的话可能这篇博文你会听的晕乎乎的噢!
创建我们需要的Annotation @Component @Autowired @Qualifier
创建ApplicationContext接口,里面一个getBean()方法,创建AnnotationConfigApplicationContext类实现ApplicationContext接口
AnnotationConfigApplicationContext的构造方法会接收一个包名,然后负责把这个包下面的所有java类的路径拿出来,再将路径处理一下即可得到类全名,并放入集合,再通Class.forName(类全名)得到所有Java类的Class 并放入集合
遍历集合所有的class判断该类上是否加了@Component, 再把加了@Component 的Class放置一个集合,然后再判断Class 的@Component 是否存在value,如果存在,则把valuevalue作为key 该Class作为value 放置一个Map集合
AnnotationConfigApplicationContext重写的 getBean() 接收一个类的Class, 得到接收Class的实例对象,进行相应属性的依赖注入,解决完依赖后return实例对象。得到Class所有的Field,遍历Field是否加了 @Autowired,如果加了 @Autowired再次判断是否加了 @Qualifier,如果加了 @Qualifier ,用 @Qualifier的value去Map集合 得到对应的Class,然后使用Field.set为实例对象的该Field赋值如(field.set(object,value))value为回调本方法后的返回值(本方法会返回Class的实例), 如果没有加 @Qualifier 则得到该Field的类型的Class ,使用Field.set为实例对象的该Field赋值,值为回调本方法后的返回值。待处理完所有的依赖注入后返回实例对象。
三、Java代码:
创建需要的Annotation
1、创建@Component
package com.custom.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
public String value() default "";
}
2、创建@Autowired
package com.custom.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target(value={ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
}
3、创建@Qualifier
package com.custom.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Qualifier {
public String value() default "";
}
主要功能实现
1、创建ApplicationContext接口
package com.custom.controller;
public interface ApplicationContext {
public Object getBean(Class clazz);
}
2、创建AnnotationConfigApplicationContext类
package com.custom.controller;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.custom.annotation.Autowired;
import com.custom.annotation.Component;
import com.custom.annotation.Qualifier;
import com.custom.exception.CustomException;
import com.example.Hello;
import com.example.World;
public class AnnotationConfigApplicationContext implements ApplicationContext {
private String projectPath=this.getClass().getResource("/").getPath();//项目路径
private List<Class> clazzList;//包下所有类
private List<String> filePaths;//包下所有文件名
private Map<String,Class> existComponentClassMap;//含有@Component注解的类
private Map<String,Class> existComponentValueClassMap;//含有@Qualifier值的类
public AnnotationConfigApplicationContext(String... strings){
clazzList=new ArrayList<Class>();
existComponentClassMap=new HashMap<>();
existComponentValueClassMap=new HashMap<>();
for (String tempPackageName : strings) {//遍历传进来的包
filePaths=getFileName(projectPath+(tempPackageName.replaceAll("[.]","/")));
try {
//把扫描到包下的类都放入clazzList集合
clazzList.addAll(getFileClass(filePaths)) ;
//遍历所有类,看是否加了@Component
isComponent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public Object getBean(Class clazz) {
try {
//判断传入的类是否加了@Component
if(existComponentClassMap.get(clazz.getName())!=null){
//解决clazz依赖并返回clazz的实例
return isAutowired(clazz);
}else{
//抛出异常
throw new CustomException("not found "+clazz.getName()+" mapping class");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//循环判断是否加了@Component
private void isComponent(){
for (Class tempClass : clazzList) {
//判断该类是否加有@Component
if(tempClass.isAnnotationPresent(Component.class)){
//把加了@Component注解的类放入集合
existComponentClassMap.put(tempClass.getName(),tempClass);
Component component=(Component)tempClass.getAnnotation(Component.class);
//得出@Component中的value
String componentValue=component.value();
//判断@Component中的value是否有值
if(componentValue.length()>0){
//把@Component中的value和当前类的class放入Map集合中
existComponentValueClassMap.put(componentValue, tempClass);
}
}
}
}
//循环判断加了Component注解类里面是否有加了@Autowired注解,和@Qualifier的属性或方法
private Object isAutowired(Class clazz) throws Exception{
//得到传入clazz的实例
Object object=clazz.newInstance();
//得到clazz的所有的属性
Field fields[]=clazz.getDeclaredFields();
//遍历所有属性
for (Field field : fields) {
//判断该属性是否加了@Autowired
if(field.isAnnotationPresent(Autowired.class)){
//判断该属性是否加了@Qualifier
if(field.isAnnotationPresent(Qualifier.class)){
Qualifier qualifier=field.getAnnotation(Qualifier.class);
//使用@Qualifier的值从Map集合中拿出对该值对应的class
Class Tempclazz=existComponentValueClassMap.get(qualifier.value());
if(Tempclazz!=null){
//为属性设置赋值权限
field.setAccessible(true);
//为实例出来的clazz对象的该属性赋值,赋值之前再次递归调用本方法,传入该属性类型的class
field.set(object,isAutowired(Tempclazz));
}else{
throw new CustomException("not found "+qualifier.value()+" mapping class");
}
}else{
//得到该属性的类型
Class fieldType=field.getType();
Class Tempclazz=existComponentClassMap.get(fieldType.getName());
if(Tempclazz!=null){
field.setAccessible(true);
field.set(object,isAutowired(Tempclazz));
}else{
throw new CustomException("not found "+fieldType.getName()+" mapping class");
}
}
}
}
return object;
}
//得到指定包下面的所有java文件路径
public List<String> getFileName(String packgePath){
List<String> filePaths=new ArrayList<>();
String filePath=packgePath;
File file=new File(filePath);
//判断是否为目录
if(file.isDirectory()){
//得到包下所有文件
File files[]=file.listFiles();
for (File file2 : files) {
//判断是否为目录
if(file2.isDirectory()){
//递归调用
filePaths.addAll(getFileName(file2.getPath()));
}else{
//如果后缀为class则把该文件路径放入集合
if(file2.getName().substring(file2.getName().lastIndexOf(".")+1).equals("class")){
filePaths.add(file2.getPath());
}
}
}
}
return filePaths;
}
//返回所有java文件的class
public List<Class> getFileClass(List<String> filePath) throws ClassNotFoundException{
List<Class> list=new ArrayList<Class>();
for (String tempFileName : filePath) {
//从项目路径之后开始截取java文件名
String tempClassName=tempFileName.substring(projectPath.length()-1);
//把路径中的“\”替换成“.”例如“com\test\test.java”替换后“com.test.test.java”
tempClassName=tempClassName.replaceAll("\\\\",".");
//再把后面的“.java”截取掉 然后使用Class.forName得到该类的class,并放入集合
list.add(Class.forName(tempClassName.substring(0,tempClassName.lastIndexOf("."))));
}
return list;
}
}
四、测试
好了,代码贴完了 那么我们来看一下实现的效果吧!,为了自动装配效果更加突出,这里我准备了六个类一个接口,每个类都有依赖。我们的最终目的是,需要通过getBean()方法得到World类的实例,并且 World类里面所有的依赖都被自动注入经来了。
- World类有如下依赖

- 各个类之间的依赖

- 最终结果我们得到了World类的实例,并且里面的各种依赖都自动注入经来了

这里我只给属性实现了自动装配的功能,方法的自动装配由于时间的原因 我就没有写啦,其实原理都是一样的,只要各位小伙伴能够领悟到java反射的精髓的话 都是分分钟的事啦。
当然Annotation 自动装配也不是特别完美的,就比如我们要自动装配一个类的话 必须为其设置 @Component ,比如我们要实例一些第三方jar包中的类,我们总不可能让第三方为我们加上@Component吧,当然 这是不现实的,不过如果你面子够大的话还是可以试一下的哈!所以在我们的实际开发中结合Annotation和xml使用才是最佳王道、当然,前者是基于普通是ssm项目、如果是基于springboot来的话 我们要配合javaConfig配置模式
模仿 spring IOC Annotation版自动装配的更多相关文章
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring基于的注解自动装配和依赖注入(***)
#自动装配的小Demo: package com.gyf.annotation; //DAO层 public interface UserDao { public void save(); } pac ...
- Spring入门(八):自动装配的歧义性
1. 什么是自动装配的歧义性? 在Spring中,装配bean有以下3种方式: 自动装配 Java配置 xml配置 在这3种方式中,自动装配为我们带来了很大的便利,大大的降低了我们需要手动装配bean ...
- Spring学习总结(2)-自动装配
上面说过,IOC的注入有两个地方需要提供依赖关系,一是类的定义中,二是在spring的配置中需要去描述.自动装配则把第二个取消了,即我们仅仅需要在类中提供依赖,继而把对象交给容器管理即可完成注入.在实 ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 【转】Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- Spring@Autowired注解与自动装配(转发)
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- [转] Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
随机推荐
- 在IIS下配置自定义的报错页面
这里介绍在IIS中配置自定义出错页面的方法,主要以404为例,其他状态可类推 1.远程桌面连接IIS所在的服务器,进入控制面板>系统和安全>管理工具,双击打开IIS管理器,选择需要配置的网 ...
- oracle 启动停止过程
oracle 主要由两部分组成:instance和database .instance是指一组后台进程/线程和一块共享内存区域,而database是指存储在磁盘上的一组物理文件. 数据库启动包括三个步 ...
- Excel向数据库插入数据(执行一次只需连接一次)-batch简单使用
由于前端时间向数据库插入excel中的数据时,每插入一条数据,就得连接一次数据库:后来发现这种做法不好,如果excel中有很多条数据,就得连接很多次数据库,这样就很浪费资源而且不安全,有时数据库也会报 ...
- 环境变量,include搜索路径,lib库搜索路径
环境变量 系统环境变量 我们知道,我们经常要设置一些环境变量,系统环境变量我们非常容易理解.其实我们在windows中经常容易接触.其实环境变量是一个非常广泛的一个概念,它与web应用程序中的web. ...
- 【总结整理】openlayer加载搜狗地图,qq地图,mapabc
qq http://www.cnblogs.com/gisvip/archive/2012/11/01/2750493.html mapabc http://www.cnblogs.com ...
- ZF 语法
Zend Framework Command Line Console Tool v1.11.11 Details for action "" and provider " ...
- boost::thread 库的使用
转载自:http://blog.csdn.net/yockie/article/details/9181939 概要 通过实例介绍boost thread的使用方式,本文主要由线程启动.Interru ...
- apt-get默认下载路径
备忘: Ubuntu中apt-get下载的安装包都在哪里呢? 在/var/cache/apt/archives里,里边的安装包可以取出来以备后用.
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-005Table per subclass with joins(@Inheritance(strategy = InheritanceType.JOINED)、@PrimaryKeyJoinColumn、)
一.结构 The fourth option is to represent inheritance relationships as SQL foreign key associations. Ev ...
- R: 判别分析
判别与聚类的比较: 聚类分析和判别分析有相似的作用,都是起到分类的作用. 判别分析是已知分类然后总结出判别规则,是一种有指导的学习: 聚类分析则是有了一批样本,不知道它们的分类,甚至连分成几类也不知道 ...