Spring 泛型依赖注入
2016-10-10 10:24
246 查看
Spring 4.x可以支持泛型依赖注入,也就是子类可以继承父类的依赖,注入相应的泛型成员变量子类。
比如,有泛型基类 BaseService<T>和BaseController<T>,Controller包含Service成员,分别有Teacher和Student两个子类,TeacherService和StudentService、TeacherController和StudentController。
1、 BaseService<T>
package com.generic.di;
public class BaseService<T> {
}
2、 BaseController<T>
package com.generic.di;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseController<T> {
@Autowired
protected BaseService<T> service;
public void execute() {
System.out.println("execute...");
System.out.println(service); // 此处打印出实际的子类对象
}
}
3、TeacherService
package com.generic.di.teacher;
import org.springframework.stereotype.Service;
import com.generic.di.BaseService;
@Service
public class TeacherService extends BaseService<Teacher> {
}
4、TeacherController
package com.generic.di.teacher;
import org.springframework.stereotype.Controller;
import com.generic.di.BaseController;
@Controller
public class TeacherController extends BaseController<Teacher> {
}
5、StudentService
package com.generic.di.student;
import org.springframework.stereotype.Service;
import com.generic.di.BaseService;
@Service
public class StudentService extends BaseService<Student> {
}
6、StudentController
package com.generic.di.student;
import org.springframework.stereotype.Controller;
import com.generic.di.BaseController;
@Controller
public class StudentController extends BaseController<Student> {
}
7、配置文件
<?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan
base-package="com.generic">
</context:component-scan>
</beans>
8、main
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentController studentController = (StudentController) context.getBean("studentController");
studentController.execute();
TeacherController teacherController = (TeacherController) context.getBean("teacherController");
teacherController.execute();
}
}
执行结果
execute...
com.generic.di.student.StudentService@d6da883
execute...
com.generic.di.teacher.TeacherService@45afc369
从结果可以看出,StudentController注入的是StudentSerive对象,TeacherController注入的是TeacherService对象,说明泛型基类的依赖关系,被子类自动继承,并根据泛型找到对应的子类。
比如,有泛型基类 BaseService<T>和BaseController<T>,Controller包含Service成员,分别有Teacher和Student两个子类,TeacherService和StudentService、TeacherController和StudentController。
1、 BaseService<T>
package com.generic.di;
public class BaseService<T> {
}
2、 BaseController<T>
package com.generic.di;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseController<T> {
@Autowired
protected BaseService<T> service;
public void execute() {
System.out.println("execute...");
System.out.println(service); // 此处打印出实际的子类对象
}
}
3、TeacherService
package com.generic.di.teacher;
import org.springframework.stereotype.Service;
import com.generic.di.BaseService;
@Service
public class TeacherService extends BaseService<Teacher> {
}
4、TeacherController
package com.generic.di.teacher;
import org.springframework.stereotype.Controller;
import com.generic.di.BaseController;
@Controller
public class TeacherController extends BaseController<Teacher> {
}
5、StudentService
package com.generic.di.student;
import org.springframework.stereotype.Service;
import com.generic.di.BaseService;
@Service
public class StudentService extends BaseService<Student> {
}
6、StudentController
package com.generic.di.student;
import org.springframework.stereotype.Controller;
import com.generic.di.BaseController;
@Controller
public class StudentController extends BaseController<Student> {
}
7、配置文件
<?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan
base-package="com.generic">
</context:component-scan>
</beans>
8、main
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentController studentController = (StudentController) context.getBean("studentController");
studentController.execute();
TeacherController teacherController = (TeacherController) context.getBean("teacherController");
teacherController.execute();
}
}
执行结果
execute...
com.generic.di.student.StudentService@d6da883
execute...
com.generic.di.teacher.TeacherService@45afc369
从结果可以看出,StudentController注入的是StudentSerive对象,TeacherController注入的是TeacherService对象,说明泛型基类的依赖关系,被子类自动继承,并根据泛型找到对应的子类。
相关文章推荐
- Spring 4.x 新特性:泛型依赖注入
- Spring4新特性:泛型限定式依赖注入
- spring4.0之泛型依赖注入
- Spring使用教程(三)泛型依赖注入
- Spring基础—— 泛型依赖注入
- Spring 泛型依赖注入
- Spring4新特性:泛型限定式依赖注入
- Spring中泛型依赖注入
- 浅谈spring4泛型依赖注入
- Spring之泛型依赖注入---spring4.0的新特性
- Spring4.0新特性--泛型依赖注入
- Spring的泛型依赖注入
- Spring 4.xx新特性:泛型依赖注入
- Spring学习笔记之泛型依赖注入
- spring 泛型依赖注入
- Spring-13:泛型依赖注入(Spring4.X新特性)
- Spring学习笔记之泛型依赖注入
- spring泛型依赖注入
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
- Spring泛型依赖注入