Spring框架:spring4.0泛型依赖注入

泛型依赖注入

Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用

举个栗子 :

我们新建一个包com.yorkmass.spring.beans.generic.di

在里面新建5个类和一个主类(测试类)如下图:

BaseReposiory类:

package com.yorkmass.spring.beans.generic.di;

public class BaseRepository<T> {

}

 BaseService类:

package com.yorkmass.spring.beans.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);
	}
}

User类:

package com.yorkmass.spring.beans.generic.di;

public class User {

}

UserRepository类:

package com.yorkmass.spring.beans.generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRepository<User> {

}

UserService类

package com.yorkmass.spring.beans.generic.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User> {

}

新建一个配置文件beans-generic-di.xml

beans-generic-di.xml

<?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.yorkmass.spring.beans.generic.di"></context:component-scan>

</beans>

Main类:

package com.yorkmass.spring.beans.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-di.xml");
		UserService userService=(UserService)ctx.getBean("userService");
		userService.add();
		//UserService继承BaseService
		//UserRepository继承BaseRepository
		//UserService、UserRepository都被注解
		//BaseService被@Autowire自动装配,注入BaseRepository<User>子类对象UserRepository
		
	}

}

运行结果:

一月 18, 2019 12:23:49 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1405ef7: startup date [Fri Jan 18 12:23:49 CST 2019]; root of context hierarchy
一月 18, 2019 12:23:49 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-generic-di.xml]
add...
com.yorkmass.spring.beans.generic.di.UserRepository@1921e20

整合多个配置文件

Spring 允许通过 <import> 将多个配置文件引入到一个文件中,进行配置文件的集成。这样在启动 Spring 容器时,仅需要指定这个合并好的配置文件就可以。

import 元素的 resource 属性支持 Spring 的标准的路径资源

转载于:https://www.cnblogs.com/yorkmass/p/11109876.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值