泛型依赖注入

看代码:

package logan.spring.study.generic.di;

public class BaseRepository<T> {

}
package logan.spring.study.generic.di;

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

public class BaseService<T> {
@Autowired
protected BaseRepository<T> repository; public void add(){
System.out.println("add...");
System.out.println(repository);
} }
package logan.spring.study.generic.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User>{ }
package logan.spring.study.generic.di;

public class User{

}
 
package logan.spring.study.generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRepository<User>{ }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="logan.spring.study.generic.di"></context:component-scan> </beans>
package logan.spring.study.generic.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generic.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.add(); } }

输出结果:

五月 27, 2017 2:04:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42110406: startup date [Sat May 27 14:04:02 CST 2017]; root of context hierarchy
五月 27, 2017 2:04:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-generic.xml]
add...
logan.spring.study.generic.di.UserRepository@43a0cee9

Spring入门第十五课的更多相关文章

  1. Spring入门第十九课

    后置通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j ...

  2. Spring入门第十八课

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...

  3. Spring入门第十六课

    接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...

  4. Spring入门第十四课

    基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...

  5. Spring入门第十二课

    Bean的配置方法 通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户 ...

  6. Spring入门第十课

    Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...

  7. 大数据入门第十五天——HBase整合:云笔记项目

    一.功能简述 1.笔记本管理(增删改) 2.笔记管理 3.共享笔记查询功能 4.回收站 效果预览: 二.库表设计 1.设计理念 将云笔记信息分别存储在redis和hbase中. redis(缓存):存 ...

  8. Spring入门第二十五课

    使用具名参数 直接看代码: db.properties jdbc.user=root jdbc.password=logan123 jdbc.driverClass=com.mysql.jdbc.Dr ...

  9. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

随机推荐

  1. 【BZOJ4166】月宫的符卡序列 Manacher+hash

    [BZOJ4166]月宫的符卡序列 题解:题倒不难,就是有点恶心. 首先学习回文串的时候一定学到了这样一个结论:一个长度为n的串的本质不同的回文子串数量不超过n个. 那么我们就可以试图将所有回文串的价 ...

  2. Python 字符串操作(截取/替换/查找/分割)

    Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...

  3. c++标准库比较

    1 GNU standard c++ library debian发行版中使用的c++标准库是GNU standard c++标准库. 2 Boost debian发行版中也是用了boost库,但是不 ...

  4. Feature Selection 其一 —— Filter Approach

    这一个部分都将只涉及到选择特征的某个子集的方法,将高纬度的特征空间映射到低维度空间的方法(如PCA)都不会涉及到. 一. 单变量 优点:运算速度快,独立于分类器 缺点:忽略的特征之间的联系,忽略了与分 ...

  5. 剑指Offer:字符串排列【38】

    剑指Offer:字符串排列[38] 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bc ...

  6. 9.2 NOIP提高组试题精解(2)

    9-18 fruit.c #include <stdio.h> #define MAXN 10000 int Queue1[MAXN], Queue2[MAXN]; void Insert ...

  7. laravel基础课程---16、数据迁移(数据库迁移是什么)

    laravel基础课程---16.数据迁移(数据库迁移是什么) 一.总结 一句话总结: 是什么:数据库迁移就像是[数据库的版本控制],可以让你的团队轻松修改并共享应用程序的数据库结构. 使用场景:解决 ...

  8. SpringBoot_Exception_02_Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:run

    一.现象 上一个异常解决之后,出现了这个异常: [WARNING] The requested profile "pom.xml" could not be activated b ...

  9. T58

    他们投产的新轿车广获传媒报道.The launch of their new saloon received much media coverage.法律规定禁止近亲结婚.Marriages in p ...

  10. “libgomp.so.1: version `GOMP_4.0' not found” || “libstdc++.so.6: version `CXXABI_1.3.8' not found”错误

    类似问题还有 'ImportError ../lib/libstdc++.so.6: version `CXXABI_1.3.7' not found (required by xxx)'.      ...