我看了很多關於這樣的問題在這裏,但似乎我的代碼是好的,但自動裝配不工作:彈簧自動裝配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不工作。
幫助,將不勝感激:)一個使用它的類內
(對於OP)一些閱讀:h ttps://spring.io/guides/gs/spring-boot/ –
Thx爲快速回復,但似乎我不能在eclipse中創建子包,只要我嘗試將包移動到另一個包中只是在同一級別重複...有沒有辦法做到這一點? 我讀過,Java不允許子包 – Pyr0technicien
嗯,我不使用eclipse,但你可以把包放到另一個。包是源代碼中的文件夾,因此您可以使用文件資源管理器訪問文件夾,並通過創建適當的文件夾結構來手動執行。就像@ predrag-maric在其他答案中建議的一樣。 – Mati