zoukankan      html  css  js  c++  java
  • 15Spring泛型依赖注入

    Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用

    BaseService<T>:有RoleService和UserService两的子类

    BaseRepepositry<T>:有UserRepository和RoleRepositry两个子类

    由于 BaseService<T>和 BaseRepepositry<T> 有关系所以,得出下面的子类也存在这样的关系

    package generic.di; 
    
    public class BaseRepository<T> { } 
    
    ————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    package generic.di;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BaseService<T> {
    
        @Autowired
        protected BaseRepository<T> repository;
    
        public void add() {
            System.out.println("add");
            System.out.println(repository);
        }
    }
    package generic.di;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class RoleRepository extends BaseRepository<Organization> {
    }
    
    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————

    package generic.di; import org.springframework.stereotype.Service; @Service public class RoleService extends BaseService<Organization> { }
    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
    package generic.di; public class Organization { }
    package generic.di;
    
    public class User {
    }
    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
    package generic.di;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UserRepository extends BaseRepository<User> {
    
    }
    
    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
    package generic.di;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService extends BaseService<User> {
    }
    <?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.xsd">
           <context:component-scan base-package="generic.di">
           </context:component-scan>
    </beans>
    package 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("generic/di/15-1.xml");
            UserService userService = (UserService) ctx.getBean("userService");
            RoleService roleService = (RoleService) ctx.getBean("roleService");
            userService.add();
            roleService.add();
        }
    }

  • 相关阅读:
    SQL Server 删除重复数据只保留一条
    英语冠词有哪些?
    英语基本语法
    统一身份认证服务(客户端用户身份验证)
    解决MVC中使用BundleConfig.RegisterBundles引用Css及js文件发布后丢失的问题
    统一身份认证服务 源码(客户端)
    MVC 如何设定默认默认路由为指定的Area下的某个action(笔记)
    MongoDB安装笔记
    消息队列第二篇:MessageQueue实战(课程订单)
    消息队列第一篇:MessageQueue介绍
  • 原文地址:https://www.cnblogs.com/jecyhw/p/4589921.html
Copyright © 2011-2022 走看看