Spring4新特性泛型依赖注入

什么叫做泛型依赖注入呢?

就是带泛型的两个父类他们之间有引用关系,
子类各自继承他们,子类之间彼此之间也会有父类间的引用关系。

举例:
两个父类:之间有引用关系。

package one;

public class Respository<T> {
}

import org.springframework.beans.factory.annotation.Autowired;

public class Service<T> {
    @Autowired
    protected Respository<T> respository;
    public void add(){
        System.out.println("Add");
        System.out.println(respository);
    }
}

随便创建个泛型实例类:

public class User {
}

两个子类继承父类:

import org.springframework.stereotype.Component;

@Component
public class UserRespository extends Respository<User> {
}

@Component
public class UserService extends Service<User> {
}

配置xml注解注入:

<context:component-scan base-package="one">
   </context:component-scan>

测试:

 public static void main(String[] args) throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService five = (UserService) applicationContext.getBean("userService");
        five.add();
    }

输出:

Add
one.UserRespository@799d4f69

可以看到子类也继承了父类的引用关系,不过UserService中的引用类是Respository的实现类,即UserRespository类。
注意:
@Autowired注解是为了 引用关系,但是父类上不需要注解,其 实现类需要注解,交给IOC管理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值