当前位置: 首页 > 知识库问答 >
问题:

Spring@Autowired失败

曹原
2023-03-14

当我尝试使用@Autowired注入服务类时,我遇到了一些问题

我的Rest控制器:

@RestController
@RequestMapping(value = "/SettlementType")
public class SettlementTypeController extends  Abstract4EyeController<SettlementTypeAudit, SettlementType, Integer>{
@Autowired
@Qualifier("settlementTypeService")
SettlementTypeService service;

@Override
public Abstract4EyeService<SettlementTypeAudit, SettlementType, Integer> get4EyeService() {
    return service;
}
}

我的服务:

@Service("settlementTypeService")
public class SettlementTypeService extends Abstract4EyeService<SettlementTypeAudit, SettlementType, Integer> implements IMasterFileUpload<SettlementTypeAudit, SettlementType, Integer>
{
    @Autowired
    SettlementTypeAuditRepository auditRepository;
    //my implementations
}

界面:

public interface IMasterFileUpload <AuditModel extends Base4Eye,  MainModel extends ReadOnlyEntity, ID extends Serializable> {

public List<MainModel> getMainFromCollection(List<Map<String, String>> collection);
}

抽象4眼服务:

public abstract class Abstract4EyeService<AuditModel extends Base4Eye, MainModel extends ReadOnlyEntity, ID extends Serializable> {
 @JsonIgnore
 public abstract MainModel getMainEntity(AuditModel auditModel);
}

我的堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settlementTypeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: project.test.rms.service.SettlementTypeService project.test.rms.controller.SettlementTypeController.service; nested exception is java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at project.test.genie.config.BootConfig.main(BootConfig.java:12)
 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: project.test.rms.service.SettlementTypeService project.test.rms.controller.SettlementTypeController.service; nested exception is java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:557)
... 18 common frames omitted

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settlementTypeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: project.test.rms.service.SettlementTypeService project.test.rms.controller.SettlementTypeController.service; nested exception is java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at project.test.genie.config.BootConfig.main(BootConfig.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: project.test.rms.service.SettlementTypeService project.test.rms.controller.SettlementTypeController.service; nested exception is java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 more
Caused by: java.lang.IllegalArgumentException: Can not set project.test.rms.service.SettlementTypeService field project.test.rms.controller.SettlementTypeController.service to com.sun.proxy.$Proxy102
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:557)
... 18 more

我试图注入定居点服务,但它抛出java.lang.非法参数例外:无法设置项目.test.rms.服务.定居点类型服务字段项目.test.rms.controller.SettlementTypeController.service到 com.sun.proxy.$Proxy 102 异常

共有1个答案

姚子石
2023-03-14

您在自动装配S结算类型服务时遇到问题的原因是Spring在内部创建了该类的代理。要解决这个问题,您可以创建一个由S结算类型服务实现的接口,并在控制器中使用该接口自动装配。

 类似资料:
  • 我做了一些搜索,但我无法找出是什么问题。我知道这个问题来自于ClassNotFoundException,但我无法解决它。 我把我需要的东西都装上了(嗯,我想)。下面是我的代码: DAO类: 服务类别:

  • 在启动springboot应用程序时出现了一些异常。我不知道我错过了什么。 这里是我的代码:这是入口: 这是控制器: 这是pom.xml 这是日志(对格式抱歉): 问题已经修复,我更新了ContextConfig.class,如下所示:` `

  • 我正在尝试使用springboot访问数据库,但是spring应用程序在下面抛出了一个异常。 创建名为“welcome controller”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beancreationexception:无法自动连接字段:private org.springframework.jdbc.

  • 问题内容: 是否通过@Autowired自动检测到?使用@Qualifier时是否按名称进行依赖项注入?我们如何使用这些批注进行setter和构造函数注入? 问题答案: 你可以与一起使用。实际上,如果发现模棱两可的bean类型,spring会询问你是否明确选择了bean,在这种情况下,你应该提供限定符 例如在以下情况下,有必要提供一个限定词 编辑: 在Lombok 1.18.4中,最终可以避免使用

  • 问题内容: 在Spring会连接的类中使用 @Autowired的 优缺点是什么? 只是为了澄清,我是在专门谈论 @Autowired 注释,而不是XML的自动 装配 。 我可能只是不了解它,但是对我来说,这几乎就像是一种反模式- 您的类开始意识到它们与DI框架相关联,而不仅仅是POJO。也许我是惩罚的嘴,但我喜欢为bean设置外部XML配置,并且我喜欢使用显式的接线,因此我确切知道在哪里接线。

  • 问题内容: Spring Boot入门级 控制器类 实体 数据jpa存储库接口 服务等级 gradle文件 我收到以下错误 如果我从中删除注释,它将抛出`NullPointerException 我在这里尝试过 问题答案: 您的Spring配置不正确。 在已经提供了依赖。当您使用特定版本声明它时: 您不必再次声明它,因为您指定的版本可能不同,并且与启动程序提供的版本不兼容。 并且根据您的错误,似乎