简介:

Spring4.X之后开始支持泛型依赖注入。

使用示例:

1、定义实体

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class Member implements Serializable {
    private static final long serialVersionUID = -7106886149424419957L;

}
package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class Role implements Serializable{
    private static final long serialVersionUID = 514142692576163383L;

}

2、定义基础仓库

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

public class BaseRepository<T extends Serializable> {
    public void add() {
        System.out.println("BaseRepository add");
    }
}

3、定义基础服务层

package com.dx.spring.bean.componentscan;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseService<T extends Serializable> {    
    @Autowired
    protected BaseRepository<T> baseRepository;

    public void add() {
        System.out.println("BaseService add ");
    }
}

4、定义仓库服务层

package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Repository;

@Repository
public class MemberRepositoryImpl extends BaseRepository<Member> {
    @Override
    public void add() {
        System.out.println("Add");
    }
}
package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Repository;

@Repository
public class RoleRepositoryImpl extends BaseRepository<Role> {

}

5、定义Member/Role服务层

package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl extends BaseService<Member> {
    @Override
    public void add() {
        System.out.println(baseRepository);
        super.add();
    }
}
package com.dx.spring.bean.componentscan;

import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends BaseService<Role> {
    @Override
    public void add() {
        System.out.println(baseRepository);
        super.add();
    }
}

6、测试类

package com.dx.spring.bean.componentscan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-component-scan2.xml");
        MemberServiceImpl memberService = (MemberServiceImpl) ctx.getBean("memberServiceImpl");
        memberService.add();
        
        RoleServiceImpl roleServiceImpl = (RoleServiceImpl) ctx.getBean("roleServiceImpl");
        roleServiceImpl.add();
    }
}

打印日志:

com.dx.spring.bean.componentscan.MemberRepositoryImpl@530612ba
BaseService add
com.dx.spring.bean.componentscan.RoleRepositoryImpl@2a40cd94
BaseService add

 

基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
+加关注】。