自动装配不适用于@RestController [英] Autowiring not working with @RestController

查看:93
本文介绍了自动装配不适用于@RestController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create a simple REST-service with some fields I want to export from applicatonContext.xml file, but I've got

堆栈跟踪

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 项目)

My applicationContext is called FileStorageContext and stored in src/main/recourses (I've got a maven project)

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 在我的测试中正常工作,但这里发生了这个错误.我是春天的新手,也许这种结构根本不正确.谢谢.

@Autowired works properly in my tests, but here this error occurs. I am new in Spring, maybe this construction is not correct at all. Thanks.

推荐答案

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

I had to put @ImportResource("classpath:/FileStorageContext.xml") before my Main class, the problem was that this file wasn't seen.

这篇关于自动装配不适用于@RestController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆