为什么Spring @Autowired不能与java泛型一起使用

编程入门 行业动态 更新时间:2024-10-26 09:35:28
本文介绍了为什么Spring @Autowired不能与java泛型一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

受到spring数据的启发,我想创建一个抽象的RESTController,我可以为很多控制器扩展它。我创建了以下类:

Inspired by spring data awesomeness I wanted to create a abstract RESTController that I could extend for a lot of my controllers. I created the following class:

@Controller public abstract class RESTController<E, PK extends Serializable, R extends PagingAndSortingRepository<E, PK>> { @Autowired private R repository; @RequestMapping(method=RequestMethod.GET, params={"id"}) @ResponseBody public E getEntity(@RequestParam PK id) { return repository.findOne(id); } ... }

我希望泛型允许我在存储库中@Autowired,但是我收到以下错误:

I was hoping that the generics would allow me to @Autowired in the repository but I get the following error:

SEVERE: Allocate exception for servlet appServlet org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.data.repository.PagingAndSortingRepository] is defined: expected single matching bean but found 3: [groupRepository, externalCourseRepository, managedCourseRepository] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

我理解错误告诉我的内容,@ Autowired有多个匹配。我很困惑因为我想通过创建以下控制器它可以工作:

I understand what the error is telling me, there is more than one match for the @Autowired. I am confused because I thought by creating the following controller it would work:

@Controller @RequestMapping(value="/managedCourse") public class ManagedCourseController extends RESTController<ManagedCourse, Long, ManagedCourseRepository> { ... }

这很容易通过在RESTController中使用这样的方法来解决这个问题:

This is easy enough to work around by doing having a method like this in the RESTController:

protected abstract R getRepository();

然后在您的实施类中执行此操作:

and then doing this in your implementing class:

@Autowired private ManagedCourseRepository repository; @Override protected ManagedCourseRepository getRepository() { return repository; }

我只是想知道是否有人想到我怎么能得到这个工作。

I was just wondering if someone had any thoughts of how I could get this to work.

我还发现这个有趣的aritcle。

Also I found this interesting aritcle.

推荐答案

在这种情况下,我实际上会建议基于XML的注释。在我看来,你试图在控制器实例中避免大量不必要的重复。如果您有一个REST控制器类,则容器可以根据需要实例化任意数量的实例,将每个实例映射到不同的URI。

I would actually recommend XML-based wiring over annotations in this case. It seems to me you're trying to avoid a lot of needless duplication in your controller instances. If you had a single REST controller class, the container can then instantiate as many instances as you need, mapping each to a different URI.

注释更多的是计划创建单个实例的情况。这恰好覆盖了大约90%的基于表单的J2EE会话bean,但它不是灵丹妙药。

Annotations are more of a shorthand for cases where you plan to create a single instance. This happens to cover about 90% of form-based J2EE session beans, but it's not a panacea.

更多推荐

为什么Spring @Autowired不能与java泛型一起使用

本文发布于:2023-11-15 17:04:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能与   Spring   Autowired   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!