简体   繁体   English

春季:无法自动连线字段-未找到类型的bean

[英]Spring: couldn't autowire field - no beans of type found

I'm struggling this for couple of hours, but couldn't figure what is wrong with my settings... 我为此奋斗了几个小时,但无法确定我的设置有什么问题...

Currently, in the TestController , the engine field is marked with the error 当前,在TestController ,engine字段标记有错误

"could not autowire. No beans of 'ServerEngine' type found". “无法自动装配。找不到'ServerEngine'类型的bean”。

I already tried to replace @SpringBootApplication with @Configuration , @EnableAutoConfiguration and @ComponentScan but still getting the error. 我已经尝试用@Configuration @EnableAutoConfiguration@ComponentScan @EnableAutoConfiguration@ComponentScan替换@SpringBootApplication ,但是仍然出现错误。

below are the relevant files: 以下是相关文件:

Application.java 应用程序

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applictionContext.xml");
        SpringApplication.run(Application.class, args);
    }
}

ServerEngine.java (act as the main system's singleton) ServerEngine.java(充当主系统的单例)

public class ServerEngine {

    @Autowired
    private DataLayer dataLayer;
    public DataLayer getDal(){
        return dataLayer;
    }

    @Autowired
    private UsersDal usersDal;
    public UsersDal getUsersDal(){
        return usersDal;
    }

}

TestController.java TestController.java

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    ServerEngine engine;

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public void test(HttpServletRequest request, HttpServletResponse response){    
        engine.getUsersDal().addOrUpdateUser(...);
    }
}

applictionContext.xml applictionContext.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 id="serverEngine" class="partners.dataaccess.ServerEngine"/>

    <bean id="usersDal" class="partners.dataaccess.UsersDal"/>
    <bean id="dataLayer" class="partners.dal.DataLayer">
        <constructor-arg name="username" value="..."/>
        <constructor-arg name="password" value="..."/>
        <constructor-arg name="url" value="..."/>
    </bean>
</beans>

In main , you're instantiating an ApplicationContext applicationContext , but then you do nothing with it - you don't pass it to the SpringApplication . main ,你实例化一个ApplicationContext applicationContext ,但你什么都不做吧-你不将它传递给SpringApplication

I'm not familiar with Spring Boot, but I see no place in your code in which the XML config's name is given to the SpringApplication either. 我对Spring Boot不熟悉,但是在您的代码中也看不到将XML配置的名称赋予SpringApplication任何地方。 So it probably only uses annotation config. 因此,它可能仅使用注释配置。

And finally, since ServerEngine isn't annotated with @Service , it won't be instantiated as a Spring bean during component-scan. 最后,由于ServerEngine并未使用@Service注释,因此在组件扫描期间不会将其实例化为Spring Bean。

The easiest and best solution would be to annotate the classes that you are autowiring in you application like ServerEngine, UsersDal and DataLayer with the annotation @Service or @Component. 最简单,最好的解决方案是,使用注解@Service或@Component注释应用程序中自动装配的类,例如ServerEngine,UsersDal和DataLayer。 If you do that, you don't have to explicitly create beans for them in xml or java configurations. 如果这样做,则不必在xml或java配置中为它们显式创建bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Validator:无法自动装配。 找不到&#39;Validator&#39;类型的bean - Spring Validator: Could not autowire. No beans of 'Validator' type found Spring 批量测试 - 无法自动接线。 找不到“JobLauncherTestUtils”类型的 bean - Spring Batch Test - Could not autowire. No beans of 'JobLauncherTestUtils' type found Spring 测试 - WebTestClient 无法自动装配。 未找到“WebTestClient”类型的 bean - Spring Testing - WebTestClient Could not autowire. No beans of 'WebTestClient' type found Spring 电子邮件:无法自动装配。 找不到“JavaMailSender”类型的 bean - Spring Email: Could not autowire. No beans of 'JavaMailSender' type found 无法自动装配未找到类型的 bean - could not autowire no beans of type found 无法自动连线@RestController中的栏位 - Couldn't autowire field in @RestController 无法自动装配。 没有找到&#39;MessagingPropertiesRefactor&#39;类型的bean - Could not autowire. No beans of 'MessagingPropertiesRefactor' type not found 无法自动接线。 找不到“ NoteRepository”类型的bean - Could not autowire. No beans of 'NoteRepository' type found 无法自动装配。 找不到SimpMessagingTemplate类型的bean - Could not autowire. No beans of SimpMessagingTemplate type found 无法自动装配。未找到 UserDetailService 类型的 Bean - Could not Autowire.No Beans of UserDetailService type found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM