简体   繁体   中英

Spring Autowired service and controller not working

I read a lot about this kind of problem here, but it seems my code is good but the autowire is not working :

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

Here is the code of my Controller :

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

}

And the Code of my Service :

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
    }

}

And The code of the interface :

package service;

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

And my Application class define the @SpringBootApplication which should be use to wire everything :

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

SO I don't get it, for me this should do the work but the autowired is not working.

Help would be appreciated :)

@SpringBootApplication scans only package (recursively) within a class that uses it. InteractionBanqueImpl is in another package.

Create a package 'app' with Application class, and then move to it controllers and and other packages. Should be fine.

As @Mati said, you have a problem with packages.

Create a root package for your application and move everything under it, so you have it something like this:

+ myapp
  Application.java
  + controller
  + service
  + serviceimpl

您将Application类放在其余代码的父包中的答案是可行的,但如果您不想更改包结构,则另一种方法是使用@ComponentScan注释,指定包包含您要自动装配的组件,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM