【问题标题】:spring field service required a bean of typespring field service 需要一个 bean 类型
【发布时间】:2019-02-18 17:32:17
【问题描述】:

我正在开发 Spring over Hibernate 项目,而我才刚刚开始。我试图有一个 SpringBootApplication 写入 MySql 一些 Location 对象。但每次我都面临一些错误。我从头到尾做了所有事情,但一切仍然无法解决。请帮帮我。 这是我的错误


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.

应用类

@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
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
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
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 + "]";
}

我的包结构是

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注解实现
  • 感谢您的建议,但遇到同样的错误。
  • 好的,那么可能尝试在@ComponentScan(basePackages= {"com.project.abhishek.location.controller"})中包含com.project.abhishek.location.service
  • 感谢兄弟,它工作了,但我可以知道为什么它不能更早工作,因为我的服务包在应用程序包下。对于每项服务,我是否每次都必须将其包含在 @component 扫描中。
  • 我在下面添加了一些解释的答案。

标签: java spring spring-mvc spring-boot spring-data-jpa


【解决方案1】:

只需在您的@ComponentScan 中包含com.project.abhishek.location.service 或直接删除@ComponentScan(这样它将扫描Spring Boot 应用程序包com.project.abhishek.location 及其下面的所有包)。

通过定义@ComponentScan(basePackages= {"com.project.abhishek.location.controller"}),你覆盖了默认的组件扫描路径com.project.abhishek.location,并告诉spring只扫描com.project.abhishek.location.controller和它下面的包,所以它没有扫描com.project.abhishek.location.service

查看https://dzone.com/articles/spring-spring-boot-and-component-scan 了解有关定义@ComponentScan 的更多详细信息。这与您的情况类似:

但是,假设其中一个组件是在包中定义的 com.in28minutes.springboot.somethingelse

在这种情况下,您需要将新包添加到组件中 扫描。

你有两个选择:

Define @ComponentScan(“com.in28minutes.springboot”) 这将扫描 com.in28minutes.springboot 的整个父树。

或者定义两个 使用数组进行特定组件扫描。 @ComponentScan({“com.in28minutes.springboot.basics.springbootin10steps”,”com.in28minutes.springboot.somethingelse”})

【讨论】:

  • 但如果我没有在 @ComponentScan 中定义我的控制器包,我与 dispatcherServlet 的映射没有完成,我得到“Whitelabel Error Page This application has no explicit mapping for /error,所以你看到这是一个后备。”
【解决方案2】:

Spring Boot 中不需要任何东西,它已经存在,@SpringBootApplication 就足够了。

只需进行以下更改,您就可以开始了。

主应用类:

@SpringBootApplication
public class StudentApplication {

public static void main(String[] args) {
    SpringApplication.run(StudentApplication.class, args);
}}

Controller 类: @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";
}

服务等级的变化:

@Service("locationService)
public class LocationServiceImpl implements LocationService {

【讨论】:

  • 感谢您的回答,但如果我这样做,我会遇到同样的错误。正如 Sergei Sirik 所说,如果我在 @ComponentScan 中包含我的服务类包,我的问题就解决了。但只想知道假设我的应用程序包是 com.abc 和服务包 com.abc.service 那么应用程序类必须扫描它。为什么我必须在 ComponentScan 中明确定义它们。
猜你喜欢
  • 1970-01-01
  • 2019-02-09
  • 2018-09-03
  • 2023-03-17
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多