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

    在父类中建立关系 (spring4.x以上版本)

    package com.spring.annotation.generic;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BaseService<T> {
    
        @Autowired
        private BaseRepository<T> dao;
        
        public void add(T entity){
            System.out.println("add by " + dao);
            dao.save(entity);
        }
        
    }
    
    
    
    package com.spring.annotation.generic;
    
    public class BaseRepository<T> {
    
        public void save(T entity){
            System.out.println("save:" + entity);
        }
        
    }
    View Code

    子类

    package com.spring.annotation.generic;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService extends BaseService<User>{
    
    }
    
    
    package com.spring.annotation.generic;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class UserDao extends BaseRepository<User>{
    
    }
    View Code

    测试

    package com.spring.annotation.generic;
    
    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-annotation.xml");
            
            UserService userService = (UserService) ctx.getBean("userService");
            userService.add(new User());
            
            
        }
        
    }
    View Code

    结果

  • 相关阅读:
    关于如在本地虚拟机上linux系统上设置静态的ip地址
    编程规约(下)-阿里巴巴Java开发手册
    编程规约(上) -- 阿里巴巴Java开发手册
    eclipse项目导入到idea
    博客收藏
    springboot springcloud
    idea配置maven仓库
    理项目
    日志管理
    [置顶] 2016年终总结
  • 原文地址:https://www.cnblogs.com/lusufei/p/7355870.html
Copyright © 2011-2022 走看看