Spring泛型依赖注入
Spring4.x中可以为子类注入子类对应的泛型类型的成员变量的引用
目录:
package generic;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author chenpeng
* @date 2018/6/3 13:42
*/
public class BaseService<T> {
@Autowired//整个类不使用注解,该地方使用注解是为了让子类继承这个注解
protected BaseRepository<T> repository;
public void save(){
System.out.println("add....");
System.out.println(repository);
}
}
@Service
public class UserService extends BaseService<User> {
}
public class BaseRepository<T> {
}
@Repository
public class UserRepository extends BaseRepository<User> {
}
public class GenericTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean_generic.xml");
UserService userService = (UserService) context.getBean("userService");
userService.save();
}
}
还没有评论,来说两句吧...