Spring——泛型依赖注入(@Autowired注解原理)

关于泛型的依赖注入

  • 在之前的书城案例中,service层中的UserService和BookService分别在各自的类中定义UserDao和BookDao并调用各自的save()方法进行数据的保存;使用注解关键字@Autowired实现类型的自动装配:
    @Autowired原理:
    @Autowired
    private BookService bookService;
    1、先按照类型去容器中去找对应的组件:bookService=ioc.getBean(BookService.class);
    (1)找到一个就赋值;
    (2)没找到抛异常;
    (3)找到多个(子类继承):
    1)、再按照变量名作为id继续匹配去找BookService(bookService)、BookServiceExt(bookServiceExt)
    a、匹配上
    b、没有匹配上,报错
    原因:按照变量名继续匹配
    使用Qualified:指定一个名作为id,让spring别使用变量名作为id
    @Autowired(required = false):找不到装配null

  • 为了简化这种重复的调用工作,引入泛型依赖注入,UserDao和BookDao同时继承自BaseDao(dao层的一个抽象的泛型类),UserService和BookService同时继承自BaseService(service层的泛型类)。

  • BaseDao

package com.xyb.fourth.dao;
public abstract class BaseDao<T> {
    public abstract void save();
}
  • UserDao
package com.xyb.fourth.dao;
import com.xyb.fourth.bean.User;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao extends BaseDao<User> {
    @Override
    public void save() {
        System.out.println("正在保存用户...");
    }
}
  • BookDao
package com.xyb.fourth.dao;
import com.xyb.fourth.bean.Book;
import org.springframework.stereotype.Repository;

@Repository
public class BookDao extends BaseDao<Book> {
    @Override
    public void save() {
        System.out.println("正在保存图书...");
    }
}

  • BaseService
package com.xyb.fourth.service;
import com.xyb.fourth.dao.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class BaseService<T> {
    @Autowired
    BaseDao<T> baseDao;
    public void save(){
        System.out.println("自动注入的dao"+baseDao);
        baseDao.save();
    }
}

  • BookService
package com.xyb.fourth.service;
import com.xyb.fourth.bean.Book;
import com.xyb.fourth.dao.BaseDao;
import com.xyb.fourth.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService extends BaseService<Book> {

}

  • UserService
package com.xyb.fourth.service;
import com.xyb.fourth.bean.User;
import com.xyb.fourth.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User>{

}

  • IOC单元测试
package com.xyb.fourth.test;
import com.xyb.fourth.service.BookService;
import com.xyb.fourth.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest {
    ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc.xml");
    @Test
    public void test23(){
        BookService bookService=ioc.getBean(BookService.class);
        UserService userService=ioc.getBean(UserService.class);
        bookService.save();
        userService.save();
        System.out.println(bookService.getClass().getGenericSuperclass());
        System.out.println(userService.getClass().getGenericSuperclass());
    }
}

具体的逻辑关系图如下:
Spring——泛型依赖注入(@Autowired注解原理)_第1张图片

你可能感兴趣的:(学习笔记1)