简体   繁体   English

spring 现场服务需要一个 bean 类型

[英]spring field service required a bean of type

I'm working on Spring over Hibernate project an i'm only in the beginning.我正在研究 Spring over Hibernate 项目,而我才刚刚开始。 I'm trying to have a SpringBootApplication which writes to MySql some Location objects.我正在尝试使用 SpringBootApplication 将一些 Location 对象写入 MySql。 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 

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).只需在您的@ComponentScan包含com.project.abhishek.location.service或简单地删除@ComponentScan (这样它将扫描 Spring Boot 应用程序包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并告诉 spring 只扫描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 .查看此https://dzone.com/articles/spring-spring-boot-and-component-scan以获取有关定义@ComponentScan更多详细信息。 This is similar to your situation:这与您的情况类似:

However, let's say one of the components is defined in package com.in28minutes.springboot.somethingelse但是,假设其中一个组件在包 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.定义@ComponentScan(“com.in28minutes.springboot”) 这将扫描com.in28minutes.springboot 的整个父树。

Or define two specific Component Scans by using an array.或者使用数组定义两个特定的组件扫描。 @ComponentScan({“com.in28minutes.springboot.basics.springbootin10steps”,”com.in28minutes.springboot.somethingelse”}) @ComponentScan({“com.in28minutes.springboot.basics.springbootin10steps”,”com.in28minutes.springboot.somethingelse”})

Nothing is required in Spring Boot, it's already there, @SpringBootApplication is sufficient. Spring Boot 不需要任何东西,它已经存在, @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, don't 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 {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 使用组件扫描配置 bean - 服务中的字段 userRepository.UserService 需要类型为“repository”的 bean - Spring configuring bean with component scan - Field userRepository in service.UserService required a bean of type 'repository xx服务中的字段xx需要一个类型为bean的bean - Field xx in xx service required a bean of type Spring Field需要一个找不到Spring JPA类型的bean - Spring Field required a bean of type that could not be found Spring JPA 服务(java文件)中的字段需要类型为Repository的bean - Field in a Service(java file) required a bean of type Repository Spring boot 字段需要一个找不到类型的 bean - Spring boot Field required a bean of type that could not be found Openfeign + Spring cloud:字段需要一个无法找到的类型的bean - Openfeign + Spring cloud : Field required a bean of type that could not be found Spring需要一个&#39;AuthenticationManager&#39;类型的bean - Spring required a bean of type 'AuthenticationManager' Spring:带有@Service注释的服务Class得到“找不到'abcService'类型的必需bean” - Spring: Service Class with @Service annotation gets "required bean of type 'abcService' could not be found" 字段ClientService需要在Spring中找不到的bean - Field ClientService required a bean that could not be found in Spring Service 中构造函数的参数 0 需要一个 Repository 类型的 bean - Parameter 0 of constructor in Service required a bean of Repository type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM