spring field service required a bean of type
spring field service required a bean of type
I'm working on Spring over Hibernate project an i'm only in the beginning. I'm trying to have a SpringBootApplication which writes to MySql some Location objects. But every time i'm faceing some error. I did everything from my end refer everything still can't resolve it. Please help me out.
This is my error
APPLICATION FAILED TO START
***************************
Description:
Field service in com.project.abhishek.location.controller.LocationController
required a bean of type'com.project.abhishek.location.service.LocationService' that could not be
found.
Action:
Consider defining a bean of type'com.project.abhishek.location.service.LocationService' in your configuration.
Application class
@SpringBootApplication
@ComponentScan(basePackages= "com.project.abhishek.location.controller")
@EntityScan(value ="com.project.abhishek.location.entity")
@EnableJpaRepositories(basePackages = "com.project.abhishek.location.repos")
public class StudentApplication
public static void main(String args)
SpringApplication.run(StudentApplication.class, args);
Controller
@Controller
public class LocationController {
@Autowired
private LocationService service;
@RequestMapping("/saveLoc")
public String saveLocation(@ModelAttribute("location") Location location, ModelMap modelMap)
Location locationSaved = getService().saveLocation(location);
String msg = "Location save with id:" +locationSaved.getId();
modelMap.addAttribute("msg", msg);
return "createLocation";
public LocationService getService()
return service;
public void setService(LocationService service)
this.service = service;
Service Class
@Service
public class LocationServiceImpl implements LocationService
@Autowired
private LocationRepos locationRepos;
@Override
public Location saveLocation(Location location)
return locationRepos.save(location);
@Override
public Location updateLocation(Location location)
return locationRepos.save(location);
@Override
public void deleteLocation(Location location)
locationRepos.delete(location);
@Override
public Location getLocation(int id)
Optional<Location> optional = locationRepos.findById(id);
Location location = optional.get();
return location;
@Override
public List<Location> getAllLocation()
return locationRepos.findAll();
public LocationRepos getLocationRepos()
return locationRepos;
public void setLocationRepos(LocationRepos locationRepos)
this.locationRepos = locationRepos;
Entity
@Entity
public class Location {
@Id
private int id;
private String code;
private String name;
private String type;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getCode()
return code;
public void setCode(String code)
this.code = code;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getType()
return type;
public void setType(String type)
this.type = type;
@Override
public String toString()
return "Location [id=" + id + ", code=" + code + ", name=" + name + ", type=" + type + "]";
My packages structure are
com.project.abhishek.location---application classs
com.project.abhishek.location.controller---controller class
com.project.abhishek.location.entity---entity class
com.project.abhishek.location.repos---repository class
com.project.abhishek.location.service---service class
@Service
LocationService
@Component
Thanks for ur suggestion but getting same error.
– Abhishek Kumar
Sep 13 '18 at 23:26
Ok, then probably try to include
com.project.abhishek.location.service in @ComponentScan(basePackages= "com.project.abhishek.location.controller")– Sergei Sirik
Sep 13 '18 at 23:30
com.project.abhishek.location.service
@ComponentScan(basePackages= "com.project.abhishek.location.controller")
thanks bro it worked, But may i know the reason why it was not working earlier as my service package is under application package. And for every service do i have to include it in @component scan everytime is there any other way.
– Abhishek Kumar
Sep 13 '18 at 23:54
I've added the answer with some explanations below.
– Sergei Sirik
Sep 14 '18 at 0:12
2 Answers
2
Just include com.project.abhishek.location.service in your @ComponentScan or simply delete @ComponentScan(this way it will scan spring boot application package com.project.abhishek.location and all packages below it).
com.project.abhishek.location.service
@ComponentScan
@ComponentScan
com.project.abhishek.location
By defining @ComponentScan(basePackages= "com.project.abhishek.location.controller") you overrode default component scan path com.project.abhishek.location and told spring to only scan com.project.abhishek.location.controller and packages below it, so it didn't scan com.project.abhishek.location.service.
@ComponentScan(basePackages= "com.project.abhishek.location.controller")
com.project.abhishek.location
com.project.abhishek.location.controller
com.project.abhishek.location.service
Check this https://dzone.com/articles/spring-spring-boot-and-component-scan for more details on defining @ComponentScan. This is similar to your situation:
@ComponentScan
However, let’s say one of the components is defined in package
com.in28minutes.springboot.somethingelse
In this case, you would need to add the new package into Component
Scan.
You have two options:
Define @ComponentScan(“com.in28minutes.springboot”) This would scan
the entire parent tree of com.in28minutes.springboot.
Or define two
specific Component Scans by using an array.
@ComponentScan(“com.in28minutes.springboot.basics.springbootin10steps”,”com.in28minutes.springboot.somethingelse”)
but if i'm not defining my controller package in @ComponentScan my mapping with dispatcherServlet was not done and i was getting "Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback."
– Abhishek Kumar
Sep 14 '18 at 5:29
Nothing is required in spring boot, its already there, @SpringBootApplication is sufficient.
@SpringBootApplication
just make below changes and you will be good to go..
Main Application Class:
@SpringBootApplication
public class StudentApplication
public static void main(String args)
SpringApplication.run(StudentApplication.class, args);
Controller Class: @Autowired is sufficient load your dependency, dont need to set, just see change you need to make in @Service notation where you need to name the interface name so dependency name can be locationService when you auto wired.
@Autowired
@Service
locationService
@RestController
public class LocationController {
@Autowired
private LocationService service;
@RequestMapping("/saveLoc")
public String saveLocation(@ModelAttribute("location") Location location, ModelMap modelMap)
Location locationSaved = getService().saveLocation(location);
String msg = "Location save with id:" +locationSaved.getId();
modelMap.addAttribute("msg", msg);
return "createLocation";
Change in Service Class:
@Service("locationService)
public class LocationServiceImpl implements LocationService {
Thanks for answer, but if i do so i'm getting same error. As told by Sergei Sirik if i include my service class package in @ComponentScan my problem is resolved. But just want to know suppose my application package is com.abc and service package com.abc.service then application class must scan this. Why explicitly i have to defined them in ComponentScan.
– Abhishek Kumar
Sep 14 '18 at 5:23
thanks i read the article now i understood. Thank you
– Abhishek Kumar
Sep 14 '18 at 5:38
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Put your
@Serviceannotation onLocationServiceinterface and annotate implementation with@Component– Sergei Sirik
Sep 13 '18 at 23:23