当前位置: 首页 > 面试题库 >

@Validated服务无法自动连线

郭兴文
2023-03-14
问题内容

如果Service类使用Validated注释进行注释,则同一类无法自动装配自身。

@Service
@Validated
public class SomeService {
    @Autowired
    private SomeService someService;
}

这是在Spring Context尝试加载时引发的异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someService' defined in file [/home/user/company/code/project/target/classes/com/company/project/controller/SomeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'someService': Bean with name 'someService' has been injected into other beans [someService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.company.project.ProjectApplication.main(ProjectApplication.java:16) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'someService': Bean with name 'someService' has been injected into other beans [someService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:585) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 19 common frames omitted

同样,当您有很多依赖于类的自身时,就会发生这种情况(当某个服务使用使用第一个服务的其他服务时)。我想知道@Validated注解,但是我总是在bean上遇到同样的错误。

有人知道我该怎么解决吗?


问题答案:

@Validated在这种情况下,注释与错误的自动装配无关。正确阅读错误,然后找出问题所在。第一行是自我描述:

创建名称为“ someService”的bean时出错:名称为“ someService”的bean已作为循环引用的一部分注入到其原始版本的其他bean
[someService]中,但最终被包装了。这意味着所说的其他bean不使用该bean的最终版本。这通常是过度渴望的类型匹配的结果-例如,考虑在关闭“
allowEagerInit”标志的情况下使用“ getBeanNamesOfType”。

您将相同的服务自动连接到自身,这导致无休止的圆形bean接线。一个办法?不要那样设计豆子。如果您确实受限于此设计,请使用以下任一方法

  • 二传手注射
  • @Lazy@PostConstruct注释

有关Baeldung网站上有关此问题的更多信息,请访问:https ://www.baeldung.com/circular-dependencies-in-
spring



 类似资料:
  • 我正在尝试创建一个Spring Boot测试类,它应该创建Spring上下文,并自动连接服务类以供我测试。 这就是我得到的错误: 原因:org。springframework。豆。工厂NoSuchBeanDefinitionException:没有“com”类型的合格bean。目瞪口呆。戈布斯。基础服务FileImportService'可用:至少需要1个符合autowire候选资格的bean。依

  • 我正在将一个项目从Symfony 3升级到Symfony 4(https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md),我有很多这样的存储库/服务: 当我尝试在浏览器中这样运行项目时: 我得到这个错误: 这是否意味着您必须在services.yaml文件中为“条令\ORM\Mapping\ClassMetadata”创建服务? 由

  • 我们正在使用Spring框架5和Spring Boot 2.0.0。M6,我们也在使用WebClient进行反应式编程。我们为我们的反应式Restendpoint创建了测试方法,所以我查找了一些关于如何做到这一点的例子。我发现这个或这个以及许多其他的都一样。他们只是自动绑定一个WebTestClient。所以我尝试了同样的方法: 我无法运行此操作,因为我收到错误信息: 因此,似乎不存在自动配置。我

  • 我创建了使用jdbc处理DB的Dao存储库。 我在我的服务类中自动连接了这个存储库。 然后我尝试在测试类中自动连接我的服务类。 当我开始测试时,我得到下一个错误: 服务中构造函数的参数0需要找不到DaoImpl类型的bean。 我如何解决我的问题?

  • 我已经用@Autowired注释为相应的存储库定义了服务类 存储库接口定义为从JpaReepository扩展 应用程序自动编译服务类 运行时,我得到以下错误

  • 在试图通过浏览器访问我的应用程序时,我遇到了不少错误。错误包括: 无法自动连线方法:public void com。ProjectOne。Util。自定义HibernatedAOSupport。anyMethodName(org.hibernate.SessionFactory);嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException