Gil Peretz
Gil Peretz

Reputation: 2419

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

"could not autowire. No beans of 'ServerEngine' type found".

I already tried to replace @SpringBootApplication with @Configuration, @EnableAutoConfiguration and @ComponentScan but still getting the error.

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)

public class ServerEngine {

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

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

}

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

<?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>

Upvotes: 1

Views: 6460

Answers (2)

amaljoyc
amaljoyc

Reputation: 57

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. If you do that, you don't have to explicitly create beans for them in xml or java configurations.

Upvotes: 1

Jiri Tousek
Jiri Tousek

Reputation: 12440

In main, you're instantiating an ApplicationContext applicationContext, but then you do nothing with it - you don't pass it to the 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. 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.

Upvotes: 4

Related Questions