Spring学习笔记之泛型依赖注入
2016-12-20 09:58
549 查看
泛型依赖注入
spring 4.0 中可以为子类注入子类对应的泛型类型的成员变量的引用
<!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.atguigu.spring.generic.di"></context:component-scan>
public class BaseRepository<T> {}
add...
com.atguigu.spring.generic.di.UserRepository@6d08d69f
个人觉得其主要思想在于泛型的使用,开发时能大大减少代码量。
spring 4.0 中可以为子类注入子类对应的泛型类型的成员变量的引用
<!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.atguigu.spring.generic.di"></context:component-scan>
public class BaseRepository<T> {}
public class BaseService<T> { @Autowired protected BaseRepository<T> repository; public void add() { System.out.println("add..."); System.out.println(repository); } }
@Repository public class UserRepository extends BaseRepository<User> {}
@Service public class UserService extends BaseService<User>{}
public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic-di.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.add(); } }运行结果:
add...
com.atguigu.spring.generic.di.UserRepository@6d08d69f
个人觉得其主要思想在于泛型的使用,开发时能大大减少代码量。
相关文章推荐
- Spring学习笔记之泛型依赖注入
- Spring4学习笔记-泛型依赖注入
- spring 控制反转与依赖注入原理-学习笔记
- spring 控制反转与依赖注入原理-学习笔记
- spring 学习小结10—泛型依赖注入
- Spring.net 学习笔记二 依赖注入
- spring学习笔记3——注入依赖对象
- [学习笔记]Spring依赖注入
- java学习笔记之Spring依赖注入和控制反转
- Spring学习笔记(5)----依赖注入的简单实现
- Spring学习笔记(5)----依赖注入的简单实现
- Spring学习笔记(二)依赖注入Dependency Injection
- Spring 学习笔记之依赖注入
- Spring 学习笔记 - IOC/依赖注入
- Spring学习笔记(6)----编码剖析Spring依赖注入的原理
- Spring 学习笔记 ----依赖注入
- Spring 学习笔记 ----依赖注入
- Spring2.5学习笔记1-控制反转-依赖注入类型
- spring.net学习笔记1--依赖对象注入
- spring学习笔记整理--05(编码剖析Spring依赖注入的原理)