2017-03-16 103 views
0

我遇到了问题。我已经研究了很多相同的主题,但我无法解决问题。 我有SpringBoot应用程序。我想开发RestController类。而如何看起来:@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

我试图找出:

  1. 我试图使用@Qualyfier( “经理” )
  2. 尝试使用@ComponentScan(basePackages = {“com.rpglife”})和 @SpringBootApplication(scanBasePackages = {“com.rpglife”})(但主要的 类留在com.rpglife。其他的包也在这儿,我肯定 这是无用的)

我不知道什么是错的... 我会,如果你给我一些答案和建议非常感谢。希望你有愉快的一天!)

更新: 我认为这可能是我的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

是com.rpglife,你必须在@ComponentScan org.rpglife变化basepackage到com.rpglife –

+0

在什么包是你的SpringBootApplication (Main.class)? @SpringBootApplication自动扫描spring boot main包下的所有包/类(在你的情况下为com.rpglife)。所以它应该选择它。你为什么在春季启动应用程序中使用spring xml?为什么不用'@ Component' /'@ Service'注释所有的类? – ddewaele

+0

@hichamabdedaime aw,这里只是一个错字,现在我写了它怎么样) –

回答

1

你混合春天引导和基于XML的应用程序环境。

默认情况下,Spring Boot不会加载您的应用程序上下文xml文件。 (春季启动巨资促进非基于XML的上下文)

你有两个选择:

  1. 不要使用XML和注释你的类的一切。嵌套在您的@SpringBootApplication包中的所有Spring注释类将被扫描。这是如何使用Spring Boot应该使用恕我直言。如果
  2. 检查这个你真的想继续attachement基本软件包使用XML http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-importing-xml-configuration
+0

是的,谢谢,你是对的。将来我们不想使用SpringBoot。谢谢,我会尝试第二种方式... –

+0

我强烈建议使用Spring Boot并倾卸xml。特别是在新项目上。如果你喜欢答案,请接受它。 – ddewaele

+0

它的工作原理,我们解决这个问题 –

相关问题