Evgeny Ignatik
Evgeny Ignatik

Reputation: 97

Couldn't autowire field in @RestController

I've faced with issue. And I've researched a lot of same topics but I can't solve an issue. I have SpringBoot application. And I want to develop RestController class. And how it looks:

@RequestMapping("achievement/")
@RestController
public class AchievementController {

@Autowired
private AchievementManager manager;

@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Achievement showAchievement(@PathVariable("id") Long id) throws DBException {
    return manager.getAchievementById(id);
}

@RequestMapping(value = "/", consumes = "application/json", method = RequestMethod.POST)
public Achievement createAchievement(@RequestBody Achievement achievement) throws DBException {
    return manager.createAchievementAndReturn(achievement);
}

@RequestMapping(value = "{id}", consumes = "application/json", method = RequestMethod.PUT)
public Achievement changeAchievement(@PathVariable("id") Long id, @RequestBody Achievement achievement) throws DBException {
    return manager.saveChangesAndReturn(id, achievement);
}
}

And I've trouble with @Autowire manager field

Exception encountered during context initialization - cancelling
refresh attempt:org.springframework.beans.factory.
BeanCreationException: 
Error creating bean with name 'achievementController': Injection of 
autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private  
com.rpglife.data.managers.AchievementManager  
com.rpglife.controller.AchievementController.manager; nested exception   
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type [com.rpglife.data.managers.AchievementManager] 
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)}
.
.
.
Caused by: org.springframework.beans.factory.
NoSuchBeanDefinitionException: No qualifying bean of type  
[com.rpglife.data.managers.AchievementManager] 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)}

My AchievmentManager class:

@SuppressWarnings("unchecked")
public class AchievementManager {

private final Class thisClass = Achievement.class;
private BaseDAO dao;

public AchievementManager(BaseDAO dao) {
    this.dao = dao;
}

public Achievement getAchievementById(Long id) {
    return (Achievement) dao.getItem(thisClass, id);
}

public Achievement createAchievementAndReturn(Achievement achievement) {
    dao.createItem(achievement);
    return getAchievementById(achievement.getId());
}

public Achievement saveChangesAndReturn(Long id, Achievement achievement) {
    return dao.updateItem(thisClass, id, achievement);
}
}

Spring xml is here:

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

<bean name="manager" class="com.rpglife.data.managers.AchievementManager" >
    <constructor-arg name="dao" ref="dao"/>
</bean>

<bean name="dao" class="com.rpglife.data.BaseDAO" >
    <constructor-arg name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation" value="hibernate.cfg.xml"/>
</bean>

<context:component-scan base-package="com.rpglife.data"/>

</beans>

Main class:

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

And here my project structure (I can attach resources too if needed): enter image description here

What I tried to figure out:

  1. I tried use @Qualyfier("manager")
  2. Tried use @ComponentScan(basePackages = {"com.rpglife"}) and @SpringBootApplication(scanBasePackages={"com.rpglife"}) (but Main class is stayed in com.rpglife. Other packages are here too, I sure this was useless)

I have no idea what is wrong... I will very thankful if you give me some answers and advices. Hope you have a nice day!)

Update: I think it can be trouble with my spring.xml. What if it doesn't work and bean can't be defined? How to check it...

Update: Error when I added @

Exception encountered during context initialization - cancelling 
refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'achievementController': Injection of autowired 
dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not 
autowire field: private com.rpglife.data.managers.AchievementManager 
com.rpglife.controller.AchievementController.manager; nested exception 
is org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'achievementManager' defined in file 
[/Users/eignatik/IdeaProjects/LifeRPG/be/target
/classes/com/rpglife/data/managers/AchievementManager.class]: 
Instantiation of bean failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [com.rpglife.data.managers.AchievementManager]: No default 
constructor found; nested exception is java.lang.NoSuchMethodException: 
com.rpglife.data.managers.AchievementManager.<init>()

Upvotes: 0

Views: 3314

Answers (1)

ddewaele
ddewaele

Reputation: 22603

You are mixing Spring Boot and XML based application contexts.

By default Spring Boot will not load your application context xml file. (Spring boot heavily promotes non-xml based contexts)

You have 2 choices :

  1. Don't use XML, and annotate your classes everything. All Spring annotated classes nested in the package of your @SpringBootApplication will get scanned. This is how Spring Boot should be used IMHO.
  2. Check this if you really want to continue using xml http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-importing-xml-configuration

Upvotes: 1

Related Questions