我遇到过问题。我已经研究了很多相同的主题,但我无法解决问题。 我有SpringBoot应用程序。我想开发RestController类。它看起来如何:
@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);
}
}
我在@Autowire经理领域遇到了麻烦
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)}
我的AchievmentManager课程:
@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在这里:
<?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>
主要课程:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
这里是我的项目结构(如果需要,我也可以附加资源): enter image description here
我试图弄清楚:
我不知道出了什么问题...... 如果你给我一些答案和建议,我将非常感激。希望你度过愉快的一天!)
更新: 我认为我的spring.xml可能有问题。如果它不起作用并且bean无法定义怎么办?如何检查...
更新: 添加@
时出错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>()
答案 0 :(得分:1)
您正在混合使用Spring Boot和基于XML的应用程序上下文。
默认情况下,Spring Boot不会加载应用程序上下文xml文件。 (Spring boot大力推广基于非xml的上下文)
您有两个选择:
@SpringBootApplication
包中的所有Spring注释类都将被扫描。这就是应该如何使用Spring Boot。