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

Spring--泛型依赖注入

如上图: 
定义了两个泛型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>
    

    结果:

  • Spring--泛型依赖注入

  •  

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

 

相关文章:

  • 2021-06-19
  • 2021-12-24
  • 2022-02-15
  • 2021-08-07
  • 2021-10-14
  • 2022-12-23
  • 2022-02-05
  • 2021-09-12
猜你喜欢
  • 2021-09-05
  • 2021-07-03
  • 2021-12-21
  • 2021-07-24
  • 2022-12-23
相关资源
相似解决方案