Spring阶段性学习总结(九)泛型依赖注入

1 package SpringGenericDi;
2 
3 import org.springframework.stereotype.Repository;
4 
5 
6 public class BaseRepository<T> {
7 
8 
9 }
 1 package SpringGenericDi;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 
 7 public class BaseService<T> {
 8     @Autowired
 9         protected BaseRepository<T> repository;
10 
11         public void add(){
12             System.out.println("BaseService add......");
13             System.out.println(repository);
14         }
15 
16 }
package SpringGenericDi;

public class User {
}
1 package SpringGenericDi;
2 
3 import org.springframework.stereotype.Repository;
4 
5 @Repository
6 public class UserRepository extends  BaseRepository<User> {
7 }
1 package SpringGenericDi;
2 
3 import org.springframework.stereotype.Service;
4 
5 @Service
6 public class UserService extends BaseService<User> {
7 }
package SpringGenericDi;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("SpringGenericDi/beans-genericDi.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.add();
    }
}
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 7        http://www.springframework.org/schema/context
 8        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 9 
10     <context:component-scan base-package="SpringGenericDi"></context:component-scan>
11 
12 </beans>

 

转载于:https://www.cnblogs.com/zhang188660586/p/11557427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值