Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 733
Pyr0technicien Avatar asked Nov 21 '25 06:11

Pyr0technicien


2 Answers

@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.

like image 156
Mati Avatar answered Nov 23 '25 19:11

Mati


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
like image 21
Predrag Maric Avatar answered Nov 23 '25 19:11

Predrag Maric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!