簡體   English   中英

Spring Autowired服務和控制器無法正常工作

[英]Spring Autowired service and controller not working

我在這里閱讀了很多關於這種問題的內容,但似乎我的代碼很好但是autowire不能正常工作:

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);
}

我的Application類定義了應該用於連接所有內容的@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);
    }
}

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

幫助將不勝感激:)

@SpringBootApplication僅在使用它的類中掃描包(遞歸)。 InteractionBanqueImpl位於另一個包中。

使用Application類創建一個包'app',然后移動到它的controllers和其他包。 應該沒事。

正如@Mati所說,你的包有問題。

為您的應用程序創建一個root包並移動它下面的所有內容,所以你有這樣的東西:

+ myapp
  Application.java
  + controller
  + service
  + serviceimpl

您將Application類放在其余代碼的父包中的答案是可行的,但如果您不想更改包結構,則另一種方法是使用@ComponentScan注釋,指定包包含您要自動裝配的組件,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM