泛型依赖注入

BaseRepository.java

package com.spring.beans.generic.di;public class BaseRepository 
{}

BaseService.java

package com.spring.beans.generic.di;import org.springframework.beans.factory.annotation.Autowired;public class BaseService 
{ @Autowired protected BaseRepository
baseRepository; public void add() { System.out.println("add..."); System.out.println(baseRepository); }}

UserRepository.java

package com.spring.beans.generic.di;import org.springframework.stereotype.Repository;@Repositorypublic class UserRepository extends BaseRepository
{}

UserService.java

package com.spring.beans.generic.di;import org.springframework.stereotype.Service;@Servicepublic class UserService extends BaseService
{}

beans-generic-di.xml

Main.java

public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-generic-di.xml");		UserService userService = (UserService) applicationContext.getBean("userService");		userService.add();	}

输出

2014-9-22 20:20:17 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@8b819f: startup date [Mon Sep 22 20:20:17 CST 2014]; root of context hierarchy

2014-9-22 20:20:17 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [beans-generic-di.xml]

add...

com.spring.beans.generic.di.UserRepository@c01e99