2017-03-16 29 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

它的工作原理,我們解決這個問題 –

相關問題