2015-12-07 95 views
3

我看了很多關於這樣的問題在這裏,但似乎我的代碼是好的,但自動裝配不工作:彈簧自動裝配Autowired服務和控制器不工作

Error creating bean with name 'optionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private service.InteractionBanque controllers.OptionController.interactionBanque; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.InteractionBanque] 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)} 

這裏是我的控制器的代碼:

package controllers; 


package controllers; 

import javax.annotation.Resource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 

import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import model.Banque; 
import model.Client; 
import service.InteractionBanque; 
import serviceimpl.InteractionBanqueImpl; 

@Controller 
public class OptionController { 

    @Autowired 
    private InteractionBanque interactionBanque; 


    @RequestMapping(value="/virement",method=RequestMethod.GET) 
    public String index(Model model, @ModelAttribute Client client) { 
     model.addAttribute("virement", new Virement()); 
     return "virement"; 
    } 
    @RequestMapping(value="/virement",method=RequestMethod.POST) 
    public String index(@ModelAttribute Virement virement, Model model) { 

     return "options"; 
    } 

} 

我的服務的代碼:

package serviceimpl; 

import java.util.HashMap; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 
import org.springframework.stereotype.Service; 

import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; 

import dao.BanqueDAO; 
import daoimpl.BanqueDaoImpl; 
import model.Banque; 
import model.Client; 
import service.InteractionBanque; 
import utils.SendRequest; 

@Service 
public class InteractionBanqueImpl implements InteractionBanque { 
    public static final int END_ID_BANQUE = 5; 
    public static final String LOGIN_URL = "/account"; 

    public boolean connecter(Client client) { 
     some code 
    } 

} 

和接口的代碼:

package service; 

public interface InteractionBanque { 
    boolean connecter(Client client); 
} 

我的應用類中定義的@SpringBootApplication這應該是使用接線一切:

package controllers; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ComponentScan; 

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

,所以我不明白,對我來說這應該做的工作,但自動裝配Autowired不工作。

幫助,將不勝感激:)一個使用它的類內

回答

6

@SpringBootApplication僅掃描包(遞歸的)。 InteractionBanqueImpl在另一個包中。

Application類創建一個包'app',然後移動到controllers和其他包。應該沒事。

+0

(對於OP)一些閱讀:h ttps://spring.io/guides/gs/spring-boot/ –

+0

Thx爲快速回復,但似乎我不能在eclipse中創建子包,只要我嘗試將包移動到另一個包中只是在同一級別重複...有沒有辦法做到這一點? 我讀過,Java不允許子包 – Pyr0technicien

+0

嗯,我不使用eclipse,但你可以把包放到另一個。包是源代碼中的文件夾,因此您可以使用文件資源管理器訪問文件夾,並通過創建適當的文件夾結構來手動執行。就像@ predrag-maric在其他答案中建議的一樣。 – Mati

2

正如@Mati所說,你有一個軟件包的問題。

爲應用程序創建一個根包,並在其移動的一切,讓你擁有它是這樣的:

+ myapp 
    Application.java 
    + controller 
    + service 
    + serviceimpl 
+0

看來不可能通過日食,我不能移動一個包到另一個:( – Pyr0technicien

+0

它可以用很多方式完成,這裏是其中之一:右鍵單擊導航器中的一個類 - >重構 - >移動並鍵入目標包名稱 –

1

的答案您有關於把你Application類的休息父包你代碼的工作,但另一種,如果你不想改變你的封裝結構,將使用@ComponentScan註解,指定包含要自動裝配組件的軟件包,例如:

@ComponentScan(basePackages = {"serviceimpl", ...} 
+0

我無法創建子包,因此這是一個很好的解決方案,Thx man! 但是,我更願意以更好的方式組織我的包 – Pyr0technicien

相關問題