高手的存在,就是让服务10亿人的时候,你感觉只是为你一个人服务......

Spring4.x 泛型依赖注入

目录
  1. 1. Demo
    1. 1.1. Model层
    2. 1.2. DAO持久层
    3. 1.3. Service业务层
    4. 1.4. Controller表现层
    5. 1.5. 配置文件
    6. 1.6. 测试

Spring4.0以后提供了泛型依赖注入,开发时减少许多代码量。


Demo

开发的时候,大多数情况是对对象进行增删改查,使用泛型依赖注入将重复使用的代码全部放到一个类之中,方便以后的维护和修改。

比如对学生和老师进行添加操作,都有add方法,可以创建公共的持久层类进行add操作。


Model层

Student类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.spring.test.model;

import org.springframework.stereotype.Component;

@Component
public class Student {

private String name;



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


@Override
public String toString() {
return "Student [name=" + name + "]";
}
}

Teacher类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.spring.test.model;

import org.springframework.stereotype.Component;

@Component
public class Teacher {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Teacher [name=" + name + "]";
}

}

DAO持久层

泛型依赖注入将add操作放入公共的持久层类中。

定义BaseDao接口和BaseDaoImpl实现类:

BaseDao:

1
2
3
4
5
6
7
8
package com.spring.test.dao;

public interface BaseDao<T> {


public void add(T t);

}

BaseDaoImpl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.spring.test.dao.impl;

import org.springframework.stereotype.Repository;

import com.spring.test.dao.BaseDao;

@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {

public void add(T t) {

System.out.println("Insert DataBase:" + t);

}

}

Service业务层

StudentService:

1
2
3
4
5
6
7
8
9
10
11
12
package com.spring.test.service;

import org.springframework.stereotype.Service;

import com.spring.test.model.Student;
import com.spring.test.service.impl.BaseService;

@Service
public class StudentService extends BaseService<Student>{


}

TeacherService:

1
2
3
4
5
6
7
8
9
10
11
package com.spring.test.service;

import org.springframework.stereotype.Service;

import com.spring.test.model.Teacher;
import com.spring.test.service.impl.BaseService;

@Service
public class TeacherService extends BaseService<Teacher> {

}

Controller表现层

MyController:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spring.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.spring.test.model.Student;
import com.spring.test.model.Teacher;
import com.spring.test.service.StudentService;
import com.spring.test.service.TeacherService;

@Controller
public class MyController {

@Autowired
private StudentService studentService;
@Autowired
private TeacherService teacherService;

public void add() {

Student student = new Student();
student.setName("I'm a Student");
studentService.add(student);

Teacher teacher = new Teacher();
teacher.setName("I'm a Teacher");
teacherService.add(teacher);
}

}

配置文件

spring-component-scan.xml:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<context:component-scan base-package="com.spring.test"/>

</beans>

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.spring.test.test;

import org.springframework.context.ApplicationContext;

public class Main {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring-component-scan.xml");
MyController p = (MyController) ctx.getBean("myController");
p.add();
}

}

结果:

Insert DataBase:Student [name=I’m a Student]
Insert DataBase:Teacher [name=I’m a Teacher]

```