Spring--泛型依赖注入

spring 4.x以上版本才有 
创建两个带泛型的类,并配置两者的依赖关系,对于继承这两个类的子类,如果泛型相同,则会继承这种依赖关系: 

如上图: 
定义了两个泛型base类:BaseService和BaseRepository 
对于UserService和UserRpository分别继承两个base类,泛型都是User,则他们俩继承了父类的依赖关系。

User:

package com.primary.spring.beans.generic.di;

public class User {

	
}

 

  • BaseRepository
    package com.primary.spring.beans.generic.di;
    
    public class BaseRepository <T> {
    
    	public void save() {
    		System.out.println("BaseRepository save...");
    	}
    }
    

     

  • BaseService
    package com.primary.spring.beans.generic.di;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BaseService <T> {
    
    	@Autowired
    	protected BaseRepository<T> repository;
    	
    	public void add() {
    		repository.save();
    	}
    }
    

     

  • UserRepository
    package com.primary.spring.beans.generic.di;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UserRepository extends BaseRepository<User> {
    
    	public void save() {
    		System.out.println("UserRepository save...");
    	}
    }
    

     

  • UserService
    package com.primary.spring.beans.generic.di;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService extends BaseService<User> {
    
    }
    

    测试:

  • package com.primary.spring.beans.generic.di;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    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();
    	}
    }
    

     

    beans-generic-di.xml:

  • <?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.3.xsd">
    
    	<context:component-scan base-package="com.primary.spring.beans.generic.di"></context:component-scan>
    </beans>
    

    结果:

  •  

          在以上的代码中,BaseService中引用了BaseReponsitory,并且在BaseService的add方法中调用了BaseReponsitory的save方法;而在他们的子类中,继承了这种关系,因此我们在测试方法中调用userService.add(); 也是可以成功UserReponsitory中的save方法而不是BaseReponsitory的save方法。
    根据泛型T自动注入相应的Reponsitory实际应用中我们可以把经常使用到的增删改查等通用的操作些在base类中,简化代码。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金州饿霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值