Spring
- 泛型依赖注入
- 问题解决
泛型依赖注入
泛型:具有占位符(类型参数)的类、结构、接口和方法,通过 <> 的方式定义了一个形式参数,在实例化时再指明具体类型
依赖注入:IoC 的具体实现,指对象之间的依赖关系在程序运行时由外部容器动态的注入依赖行为方式
泛型依赖注入:在进行依赖注入的同时,使用泛型将可重复使用的代码提取在相同地方中,可以简化代码,提高代码复用性,便于维护和修改
简单示例:
首先创建学生和教师两个实体类,定义编号和姓名两个属性:
package cn.edu.springdemo.model;
//学生类
public class Student {
private int id ; //学号
private String name ; //姓名
public Student() {
super();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package cn.edu.springdemo.model;
//教师类
public class Teacher {
private int id; //职工号
private String name; //姓名
public Teacher() {
super();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
然后定义一个接口 StudentDao ,声明一个 Update 方法:
package cn.edu.springdemo.dao;
import cn.edu.springdemo.model.Student;
public interface StudentDao {
public void Update(Student student);
}
定义一个接口 TeacherDao ,同样声明一个 Update 方法:
package cn.edu.springdemo.dao;
import cn.edu.springdemo.model.Teacher;
public interface TeacherDao {
public void Update(Teacher teacher);
}
两个接口都有 Update 方法,可以将其提取在同一地方。即定义一个泛型 GenericsDao ,通过类型参数来实现代码复用以提高代码的编写效率:
package cn.edu.springdemo.dao;
//泛型
public class GenericsDao<T> {
//定义形式参数,调用时再具体指明
public void Update(T t){
System.out.println(t);
}
}
再创建对应接口的实现类,并继承泛型 GenericsDao 和传入对应的类型:
package cn.edu.springdemo.dao;
import cn.edu.springdemo.model.Student;
import org.springframework.stereotype.Repository;
//通过注解方式配置 bean 示例
@Repository("studentDao")
public class StudentDaoImpl extends GenericsDao<Student> implements StudentDao {
// Update 方法无需再写
}
package cn.edu.springdemo.dao;
import cn.edu.springdemo.model.Teacher;
import org.springframework.stereotype.Repository;
@Repository("teacherDao")
public class TeacherDaoImpl extends GenericsDao<Teacher> implements TeacherDao {
//同样 Update 方法无需再写
}
同理,在 Service 层中将可重复使用的注入代码提取在泛型 GenericsService 中:
package cn.edu.springdemo.service;
import cn.edu.springdemo.dao.GenericsDao;
import org.springframework.beans.factory.annotation.Autowired;
public class GenericsService<T> {
@Autowired
protected GenericsDao<T> genericsDao; //泛型注入
}
接着定义 StudentService 和 TeacherService 接口,声明一个 Update 方法;
再让其实现类继承泛型 GenericsService ,调用 genericsDao 属性即可:
package cn.edu.springdemo.service;
import cn.edu.springdemo.model.Student;
import org.springframework.stereotype.Service;
@Service("studentService")
public class StudentServiceImpl extends GenericsService<Student> implements StudentService {
@Override
public void Update(Student student) {
genericsDao.Update(student);
}
}
package cn.edu.springdemo.service;
import cn.edu.springdemo.model.Teacher;
import org.springframework.stereotype.Service;
@Service("teacherService")
public class TeacherServiceImpl extends GenericsService<Teacher> implements TeacherService {
public void Update(Teacher teacher){
genericsDao.Update(teacher);
}
}
最后测试结果:
package cn.edu.springdemo.test;
import cn.edu.springdemo.service.StudentService;
import cn.edu.springdemo.service.TeacherService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AdminTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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 。如图:
问题成功解决。如图: