spring泛型依赖注入

泛型依赖注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QlRvZODG-1620216626229)(E:\开发笔记\ssm\spring\images\ioc\泛型依赖注入.jpg)]

  1. bean层

    package com.atguigu.bean;
    
    public class Book {
    
    }
    
    package com.atguigu.bean;
    
    public class User {
    
    }
    
    
  2. dao层

    package com.atguigu.dao;
    
    import org.springframework.stereotype.Repository;
    
    /**
     * 定义了各种 增删改查方法
     *
     * @param <T>
     */
    @Repository
    public abstract class BaseDao<T> {
    
    	public abstract void save();
    	
    }
    
    
    package com.atguigu.dao;
    
    import org.springframework.stereotype.Repository;
    
    import com.atguigu.bean.Book;
    
    @Repository
    public class BookDao extends BaseDao<Book>{
    
    	@Override
    	public void save() {
    		System.out.println("保存图书");
    	}
    	
    }
    
    
    package com.atguigu.dao;
    
    import org.springframework.stereotype.Repository;
    
    import com.atguigu.bean.User;
    
    @Repository
    public class UserDao extends BaseDao<User>{
    
    	@Override
    	public void save() {
    		System.out.println("保存用户");
    	}
    }
    
    
  3. service层

    //子类继承了父类,就相当于子类拥有了父类的属性与方法,尽管父类没有创建bean对象,但是因为子类创建了bean对象,所以在注入属性时会根据子类的的泛型注入属性。
    package com.atguigu.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.atguigu.dao.BaseDao;
    
    public class BaseService<T> {
    
    	@Autowired
    	BaseDao<T> baseDao;
    	
    	public void save(){
    		System.out.println("自动注入"+baseDao);
    		baseDao.save();
    	}
    	
    }
    
    
    package com.atguigu.service;
    
    
    import org.springframework.stereotype.Service;
    
    import com.atguigu.bean.Book;
    
    @Service
    public class BookService extends BaseService<Book>{
    }
    
    
    package com.atguigu.service;
    
    import org.springframework.stereotype.Service;
    
    import com.atguigu.bean.User;
    
    @Service
    public class UserService extends BaseService<User>{
    }
    
    
  4. 测试

    package com.atguigu.test;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.atguigu.service.BookService;
    import com.atguigu.service.UserService;
    import com.sun.xml.internal.bind.CycleRecoverable.Context;
    
    public class IOCTest {
    
    	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    	
    	@Test
    	public void test() {
    		BookService bookService = context.getBean("bookService",BookService.class);
    		UserService userService = context.getBean("userService",UserService.class);
    		
    		bookService.save();
    		userService.save();
            /*
            	自动注入com.atguigu.dao.BookDao@76329302
                保存图书
                自动注入com.atguigu.dao.UserDao@5e25a92e
                保存用户
            */
    		
    //		Spring中可以使用带泛型的父类类型来确定子类类型
    		System.out.println(bookService.getClass().getGenericSuperclass());
    //		com.atguigu.service.BaseService<com.atguigu.bean.Book>
    	}
    
    }
    
    
     System.out.println(bookService.getClass().getGenericSuperclass());
    

    // com.atguigu.service.BaseService<com.atguigu.bean.Book>
    }

    }

    
    
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值