- 在父类配置的关系子类会继承
- 会指向泛型对应的子类
父类
package note.genericity;
public class BaseRepository<T> {
}
package note.genericity;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
//在父类指定装配
@Autowired
protected BaseRepository<T> repository;
public void add() {
System.out.println("Service Add");
System.out.println(repository);
}
}
pojo:
package note.genericity;
public class User {
}
子类:
package note.genericity;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User> {
}
package note.genericity;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User>{
}
相关配置...
/**
* 泛型依赖注入
* - 在父类配置的关系子类会继承
* - 会指向泛型对应的子类
* */
public static void testScanGenericity() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("scanb.xml");
UserService service=(UserService)ctx.getBean("userService");
service.add();
}