【问题标题】:Autowiring not working with @RestController自动装配不适用于 @RestController
【发布时间】:2015-03-17 11:20:40
【问题描述】:

我需要创建一个简单的 REST 服务,其中包含一些我想从 applicatonContext.xml 文件中导出的字段,但我已经有了

堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3RestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.emc.sk.data_mgmt_central.server.S3RestController.path; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
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:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at com.emc.sk.data_mgmt_central.main.Main.main(Main.java:39)

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.emc.sk.data_mgmt_central.server.S3RestController.path; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 16 common frames omitted

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=path)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527)
... 18 common frames omitted

我运行应用程序

主类

@ComponentScan("com.emc.sk.data_mgmt_central.server")
@EnableAutoConfiguration
public class Main {
    public static void main(String[] args) {    
        SpringApplication.run(Main.class, args);   
    }
}

这是我的

RestController

@RestController
@ContextConfiguration("/FileStorageContext.xml")
public class S3RestController {

@Autowired
private String path;

@RequestMapping("/getBucket")
public Bucket getBucket(@RequestParam(value="name", defaultValue="world") String name) {
    return new Bucket(name);
}   
}

我的 applicationContext 称为 FileStorageContext 并存储在 src/main/recourses 中(我有一个 maven 项目)

FileStorageContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name = "path" class="java.lang.String">
    <constructor-arg type="java.lang.String" value="#{ systemProperties['java.io.tmpdir'] }bucketDir" />
</bean>

@Autowired 在我的测试中可以正常工作,但这里会出现此错误。我是春天的新手,也许这种构造根本不正确。谢谢。

【问题讨论】:

  • 这通常意味着您没有正确导入配置。使用 JavaConfig 甚至只是 @Value 会容易得多。

标签: java spring rest maven


【解决方案1】:

添加@PropertySource("classpath:/your/properties/path/applicatio.properties") 到您的控制器并将@Value标签添加到路径而不是@Autowired

@Value("${java.io.tmpdir}")
private String path;

【讨论】:

    【解决方案2】:

    我不得不把 @ImportResource("classpath:/FileStorageContext.xml") 在我的 Main 类之前,问题是没有看到这个文件。

    【讨论】:

      【解决方案3】:

      这行导致你的异常:

      @Autowired
      private String path;
      

      自动装配仅适用于 Spring 托管 bean,String 不应被视为一个 (IMO)。

      如果你想从你的上下文中获取path 的值,你可以使用@Value 注解。或者,如果您想按原样使用,可以尝试使用 @Qualifier("path") 注释和 @Autowired

      【讨论】:

      • IDK 为什么他也想自动装配字符串
      • 感谢您的回复,但使用`@Value("${path}") private String path;`并没有解决我的问题,我仍然有一个错误。我的bean不是Spring管理的吗?我在一些测试中自动连接了路径,一切正常,尽管这不是一个好的决定。
      • 你试过@Value("#{ systemProperties['java.io.tmpdir'] })。关于您的测试,这完全取决于您如何进行测试,但是,String 是一个非常特殊的对象,IMO 不应该以这种方式使用。您仍然可以尝试@Qualifier("path") 选项
      猜你喜欢
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多