Spring
泛型依赖注入
泛型:具有占位符(类型参数)的类、结构、接口和方法,通过 <> 的方式定义了一个形式参数,在实例化时再指明具体类型
依赖注入:IoC 的具体实现,指对象之间的依赖关系在程序运行时由外部容器动态的注入依赖行为方式
泛型依赖注入:在进行依赖注入的同时,使用泛型将可重复使用的代码提取在相同地方中,可以简化代码,提高代码复用性,便于维护和修改
简单示例:
首先创建学生和教师两个实体类,定义编号和姓名两个属性:
packagecn.edu.springdemo.model;//学生类publicclassStudent{privateint id ;//学号privateString name ;//姓名publicStudent(){super();}publicintgetId(){return id;}publicStringgetName(){return name;}publicvoidsetId(int id){this.id = id;}publicvoidsetName(String name){this.name = name;}@OverridepublicStringtoString(){return"Student{"+"id="+ id +", name='"+ name +'\''+'}';}}
packagecn.edu.springdemo.model;//教师类publicclassTeacher{privateint id;//职工号privateString name;//姓名publicTeacher(){super();}publicintgetId(){return id;}publicStringgetName(){return name;}publicvoidsetId(int id){this.id = id;}publicvoidsetName(String name){this.name = name;}@OverridepublicStringtoString(){return"Teacher{"+"id="+ id +", name='"+ name +'\''+'}';}}
然后定义一个接口 StudentDao ,声明一个 Update 方法:
packagecn.edu.springdemo.dao;importcn.edu.springdemo.model.Student;publicinterfaceStudentDao{publicvoidUpdate(Student student);}
定义一个接口 TeacherDao ,同样声明一个 Update 方法:
packagecn.edu.springdemo.dao;importcn.edu.springdemo.model.Teacher;publicinterfaceTeacherDao{publicvoidUpdate(Teacher teacher);}
两个接口都有 Update 方法,可以将其提取在同一地方。即定义一个泛型 GenericsDao ,通过类型参数来实现代码复用以提高代码的编写效率:
packagecn.edu.springdemo.dao;//泛型publicclassGenericsDao<T>{//定义形式参数,调用时再具体指明publicvoidUpdate(T t){System.out.println(t);}}
再创建对应接口的实现类,并继承泛型 GenericsDao 和传入对应的类型:
packagecn.edu.springdemo.dao;importcn.edu.springdemo.model.Student;importorg.springframework.stereotype.Repository;//通过注解方式配置 bean 示例@Repository("studentDao")publicclassStudentDaoImplextendsGenericsDao<Student>implementsStudentDao{// Update 方法无需再写}
packagecn.edu.springdemo.dao;importcn.edu.springdemo.model.Teacher;importorg.springframework.stereotype.Repository;@Repository("teacherDao")publicclassTeacherDaoImplextendsGenericsDao<Teacher>implementsTeacherDao{//同样 Update 方法无需再写}
同理,在 Service 层中将可重复使用的注入代码提取在泛型 GenericsService 中:
packagecn.edu.springdemo.service;importcn.edu.springdemo.dao.GenericsDao;importorg.springframework.beans.factory.annotation.Autowired;publicclassGenericsService<T>{@AutowiredprotectedGenericsDao<T> genericsDao;//泛型注入}
接着定义 StudentService 和 TeacherService 接口,声明一个 Update 方法;
再让其实现类继承泛型 GenericsService ,调用 genericsDao 属性即可:
packagecn.edu.springdemo.service;importcn.edu.springdemo.model.Student;importorg.springframework.stereotype.Service;@Service("studentService")publicclassStudentServiceImplextendsGenericsService<Student>implementsStudentService{@OverridepublicvoidUpdate(Student student){genericsDao.Update(student);}}
packagecn.edu.springdemo.service;importcn.edu.springdemo.model.Teacher;importorg.springframework.stereotype.Service;@Service("teacherService")publicclassTeacherServiceImplextendsGenericsService<Teacher>implementsTeacherService{publicvoidUpdate(Teacher teacher){genericsDao.Update(teacher);}}
最后测试结果:
packagecn.edu.springdemo.test;importcn.edu.springdemo.service.StudentService;importcn.edu.springdemo.service.TeacherService;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassAdminTest{publicstaticvoidmain(String[] args){ApplicationContext applicationContext =newClassPathXmlApplicationContext("annontationDemo.xml");StudentService studentService =(StudentService) applicationContext.getBean("studentService");System.out.println(studentService);TeacherService teacherService =(TeacherService) applicationContext.getBean("teacherService");System.out.println(teacherService);}}
结果如图:
问题解决
报错:Could not autowire. No beans of ‘GenericsDao’ type found. more… (Ctrl+F1)
解决:其实并没有错误,对运行结果没有影响。但看起来不太舒服,解决办法只需要降低错误级别即可
步骤:点击 File ,点击 Settings… ,弹出窗口选择 Editor ,然后点击 Inspections ,找到 Spring ,选择 Spring Core 和 Code ,点击 Autowiring for Bean Class ,最后在右侧的 Severity 下,将 Error 级别降低为 Warning 。如图:
问题成功解决。如图:
版权归原作者 啊Q老师 所有, 如有侵权,请联系我们删除。