春季现场服务需要以下类型的bean

时间:2018-09-13 23:06:51

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

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


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 

2 个答案:

答案 0 :(得分:0)

只需在您的{{1}中包括com.project.abhishek.location.service或简单地删除@ComponentScan(这样,它将扫描spring boot应用程序软件包@ComponentScan及其下面的所有软件包)。

通过定义com.project.abhishek.location,您将覆盖默认的组件扫描路径@ComponentScan(basePackages= {"com.project.abhishek.location.controller"}),并告诉spring仅扫描com.project.abhishek.location及其下面的软件包,因此它不扫描com.project.abhishek.location.controller。 / p>

选中此https://dzone.com/articles/spring-spring-boot-and-component-scan,以获取有关定义com.project.abhishek.location.service的更多详细信息。这与您的情况类似:

  

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

     

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

     

您有两个选择:

     

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

     

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

答案 1 :(得分:0)

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

只需进行以下更改,您就会很高兴..

主应用程序类别:

@SpringBootApplication
public class StudentApplication {

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

控制器类: @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 {