Sprinig泛型依赖注入
Posted lusufei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sprinig泛型依赖注入相关的知识,希望对你有一定的参考价值。
在父类中建立关系 (spring4.x以上版本)
package com.spring.annotation.generic; import org.springframework.beans.factory.annotation.Autowired; public class BaseService<T> { @Autowired private BaseRepository<T> dao; public void add(T entity){ System.out.println("add by " + dao); dao.save(entity); } } package com.spring.annotation.generic; public class BaseRepository<T> { public void save(T entity){ System.out.println("save:" + entity); } }
子类
package com.spring.annotation.generic; import org.springframework.stereotype.Service; @Service public class UserService extends BaseService<User>{ } package com.spring.annotation.generic; import org.springframework.stereotype.Repository; @Repository public class UserDao extends BaseRepository<User>{ }
测试
package com.spring.annotation.generic; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.add(new User()); } }
结果
以上是关于Sprinig泛型依赖注入的主要内容,如果未能解决你的问题,请参考以下文章