首页 > 其他 > 详细

泛型依赖注入

时间:2018-09-15 18:11:29      阅读:147      评论:0      收藏:0      [点我收藏+]

Spring 4.x的新特性

 

1.结构

技术分享图片

 

BaseRepository.java:

1 package com.hk.beans.generic.di;
2 
3 public class BaseRepository<T> {
4 
5 }

 

BaseService.java:

 1 package com.hk.beans.generic.di;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 public class BaseService<T> {
 6     
 7     @Autowired
 8     protected BaseRepository<T> repository;
 9     
10     public void add(){
11         System.out.println("add...");
12         System.out.println(repository);
13     }
14 }

 

User.java:

1 package com.hk.beans.generic.di;
2 
3 public class User {
4 
5 }

 

UserRepository.java:

1 package com.hk.beans.generic.di;
2 
3 import org.springframework.stereotype.Repository;
4 
5 @Repository
6 public class UserRepository extends BaseRepository<User>{
7  
8 }

 

UserService.java:

1 package com.hk.beans.generic.di;
2 
3 import org.springframework.stereotype.Service;
4 
5 @Service
6 public class UserService extends BaseService<User>{
7     
8 }

 

Main.java:

 1 package com.hk.beans.generic.di;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic-di.xml");
 9         UserService userService = (UserService) ctx.getBean("userService");
10         userService.add();
11     }
12 
13 }

 

运行结果:

技术分享图片

 

泛型依赖注入

原文:https://www.cnblogs.com/zhzcode/p/9651514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!