spring 泛型依赖注入
2015-10-12 21:48
260 查看
泛型依赖注入不是很懂,参考链接
http://www.tuicool.com/articles/bIRfUv
1.
package com.hxzy.generic.di;
public class BaseRepository<T> {
}
2.
package com.hxzy.generic.di;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
//此处加注解,使得T中注入User类的bean?不懂
@Autowired
protected BaseRepository<T> repository;
public void add() {
System.out.println("add");
System.out.println(repository);
}
}
3.
package com.hxzy.generic.di;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User>{
}
4.
package com.hxzy.generic.di;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User>{
}
5.
package com.hxzy.generic.di;
public class User {
}
6.配置文件
<?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.0.xsd">
<context:component-scan base-package="com.hxzy.generic.di"></context:component-scan>
</beans>
7.测试文件
package com.hxzy.generic.di;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("generic.xml");
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
userService.add();
}
}
http://www.tuicool.com/articles/bIRfUv
1.
package com.hxzy.generic.di;
public class BaseRepository<T> {
}
2.
package com.hxzy.generic.di;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
//此处加注解,使得T中注入User类的bean?不懂
@Autowired
protected BaseRepository<T> repository;
public void add() {
System.out.println("add");
System.out.println(repository);
}
}
3.
package com.hxzy.generic.di;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User>{
}
4.
package com.hxzy.generic.di;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User>{
}
5.
package com.hxzy.generic.di;
public class User {
}
6.配置文件
<?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.0.xsd">
<context:component-scan base-package="com.hxzy.generic.di"></context:component-scan>
</beans>
7.测试文件
package com.hxzy.generic.di;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("generic.xml");
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
userService.add();
}
}
相关文章推荐
- POJO和javaBean的区别
- Java 中深层理解父类引用指向子类对象
- 6 Spring入门 DispatcherServlet的工作方式
- Java中,父类引用指向子类对象的问题分析
- struts总结
- Java 命名规范
- Java回合阵列List
- [java学习笔记]java语言核心----面向对象之static关键字
- Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask
- [Java] Java中带标签的break语句
- eclipse java工程和maven工程的互相转换
- switch增强_九九乘法表_goto_键盘的输入JAVA019-027
- 学习Java的第一天
- java Proxy(代理机制)
- Struts2 重定向
- Java随机数生成原理
- java反射与动态代理
- VS2010安装因net framework4.0无法安装以及Eclipse中ADB无法连天天接模拟器
- 系统里有Courier New等字体但Eclipse没有字体选项的解决方案
- 主要的排序算法——Java实现