1

I am new to spring boot & facing one issue. I have a class that I want to @Autowired it in controller. But I am getting an error.

Description:

Field sessionService in co.business.controller.SessionController required a bean of type 'co.business.service.AppSessionService' that could not be found.

Action:

Consider defining a bean of type 'co.business.service.AppSessionService' in your configuration.

Here is my component class.

package co.business.service;
...


@Component
public class AppSessionService {
    @Autowired
    public AppSessionService() {
        super();
    }
   ...
}

Here is my controller.

package co.business.controller;
...

@RestController
@RequestMapping("/session")
public class SessionController {

    @Autowired
    private AppSessionService sessionService;
    @PostMapping(path = "/cfck", consumes = "application/json", produces = "application/json")
    public Map<String, Boolean> isRegister(@RequestBody AppSession session){

        // Some stuff here

        return map;
    }
   ...
}

I applied solution provided in this question. but could not work for me still having same error.
Main class.


@ComponentScan({"co.business.service", "co.business.controller"})
@SpringBootApplication
public class BusinessMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(BusinessMainApplication.class, args);
    }
}

It could be a duplicate question but I tried to apply solutions from some previous questions but none could solve my problem as I explained above.
Can any one explain the complete solution to solve this problem.

Afsar edrisy
  • 1,985
  • 12
  • 28
  • Initial I did not have `@Autowired` in constructor but I seen it in one solution so I was trying to apply but error is same with or without it. – Afsar edrisy Feb 06 '20 at 10:22
  • `co.introtuce.service.` is the package that @Autowired is looking for which is not the package that your Service resides in. Is it possible there is another AppSessionService in your app that you do not initialize as a bean? – jBuchholz Feb 06 '20 at 10:29
  • try by annotate your component class with either `@Service` or `@Repository ` and just comment your contructor. for me worked this `@SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"})` – naveenkumar Feb 06 '20 at 10:34
  • @Scorpioo590 that is type mistake, I updated question , I don't have any other class with name `AppSessionService` – Afsar edrisy Feb 06 '20 at 10:35
  • Hey @naveenkumar It worked with `@Service` I don't the reason but is working now Thank you – Afsar edrisy Feb 06 '20 at 10:38
  • 1
    Your `@ComponentScan` is erroneous. It expects a list of strings, what you provided was a single string with multiple packages. That is not understood by spring. See here: https://stackoverflow.com/questions/10794587/how-to-scan-multiple-paths-using-the-componentscan-annotation – jBuchholz Feb 06 '20 at 10:41
  • 1
    @Afsaredrisy just refer annotation and DI in spring boot document you will get greater understading. – naveenkumar Feb 06 '20 at 10:44
  • Your code works in my spring app. @Autowired and all below in service are superfluous. It is meant that the service is initialized in the service itself. it's pointless Also super() constructor is superfluous because the service doesn't have parents. – Valeriy K. Feb 06 '20 at 13:21

0 Answers0