zoukankan      html  css  js  c++  java
  • Spring(十六):泛型依赖注入

    简介:

    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

  • 相关阅读:
    maven中net.sf.json报错的解决方法(转载)
    [PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)
    [LNMP]——LNMP环境配置
    Tomcat
    Amoeba+Mysql 实现读写分离
    LVS+keepalived DR模式配置高可用负载均衡集群
    [Mysql高可用]——双主互备+keepalived
    Mysql 日志管理
    Mysql基本操作总结
    [Mysql]——通过例子理解事务的4种隔离级别
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/9108598.html
Copyright © 2011-2022 走看看