当我不使用@CompomentScan时,我的程序不会收到任何错误,但是它会显示错误404,并且我看不到邮递员的任何rest调用。当我使用复合扫描,我得到以下错误,我应该做什么来使我的程序工作?我对春天很陌生,也许我的问题有点傻,但请你帮帮忙。
使用@ComponentScan( WarehouseRestController.class): =basePackageClasses)时的
Description:
Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.
当我不使用@CompomentScan时 :
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jul 08 15:13:32 EEST 2021
There was an unexpected error (type=Not Found, status=404).
No message available
WarehouseEntity:
package com.example.entity;
import javax.persistence.*;
@Entity
@Table(name="warehouses")
//@Access(AccessType.FIELD)
public class Warehouse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_warehouse")
private Long id_warehouse;
@Column(name="description_warehouse")
private String description_warehouse;
public Warehouse() {
}
public Warehouse(Long id_warehouse, String description_warehouse) {
this.id_warehouse = id_warehouse;
this.description_warehouse = description_warehouse;
}
public Long getId_warehouse() {
return id_warehouse;
}
public void setId_warehouse(Long id_warehouse) {
this.id_warehouse = id_warehouse;
}
public String getDescription_warehouse() {
return description_warehouse;
}
public void setDescription_warehouse(String description_warehouse) {
this.description_warehouse = description_warehouse;
}
@Override
public String toString() {
return "Warehouses{" +
"id_warehouse=" + id_warehouse +
", description_warehouse='" + description_warehouse + '\'' +
'}';
}
}
WarehouseDTO:
package com.example.dto;
public class WarehouseDTO {
private Long id_warehouse;
private String description_warehouse;
public WarehouseDTO() {
}
public WarehouseDTO(Long id_warehouse, String description_warehouse) {
this.id_warehouse = id_warehouse;
this.description_warehouse = description_warehouse;
}
public Long getId_warehouse() {
return id_warehouse;
}
public void setId_warehouse(Long id_warehouse) {
this.id_warehouse = id_warehouse;
}
public String getDescription_warehouse() {
return description_warehouse;
}
public void setDescription_warehouse(String description_warehouse) {
this.description_warehouse = description_warehouse;
}
}
WarehouseRepository:
package com.example.repository;
import com.example.entity.Warehouse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface WarehouseRepository extends JpaRepository<Warehouse, Long>{
}
WarehouseRepositoryImpl:
package com.example.repositoryImpl;
import com.example.entity.QWarehouse;
import com.example.entity.Warehouse;
import com.example.repository.WarehouseRepositoryCustom;
import com.querydsl.jpa.impl.JPAQuery;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class WarehouseRepositoryImpl {
@PersistenceContext
private EntityManager entityManager;
public Warehouse findByDescription(String description) {
JPAQuery<Warehouse> warehouseJPAQuery = new JPAQuery<>();
QWarehouse warehouse = QWarehouse.warehouse;
Warehouse warehouse1 = warehouseJPAQuery.select(warehouse)
.from(warehouse)
.where(warehouse.description_warehouse.eq(description))
.fetchOne();
return warehouse1;
}
}
WarehouseService:
package com.example.service;
import com.example.dto.WarehouseDTO;
import java.util.List;
public interface WarehouseService {
List<WarehouseDTO> findAll();
void create(WarehouseDTO warehouseDTO);
void update(WarehouseDTO warehouseDTO);
void deleteById(Long id);
}
WarehouseServiceImpl:
package com.example.serviceImpl;
import com.example.dto.WarehouseDTO;
import com.example.entity.Warehouse;
import com.example.repository.WarehouseRepository;
import com.example.service.WarehouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class WarehouseServiceImpl implements WarehouseService {
@Autowired
private WarehouseRepository warehousesRepository;
public Warehouse toEntity(WarehouseDTO warehouseDTO) {
Warehouse warehouse = new Warehouse();
BeanUtils.copyProperties(warehouseDTO, warehouse);
return warehouse;
}
public WarehouseDTO toDTO(Warehouse warehouse) {
WarehouseDTO warehouseDTO = new WarehouseDTO();
BeanUtils.copyProperties(warehouse, warehouseDTO);
return warehouseDTO;
}
@Override
public List<WarehouseDTO> findAll() {
List<Warehouse> warehouseList = this.warehousesRepository.findAll();
List<WarehouseDTO> warehouseDTOList = new ArrayList<>();
for (Warehouse warehouse : warehouseList) {
warehouseDTOList.add(this.toDTO(warehouse));
}
return warehouseDTOList;
}
@Override
public void create(WarehouseDTO warehouseDTO) {
if (warehouseDTO != null && warehouseDTO.getId_warehouse() == null) {
this.warehousesRepository.save(this.toEntity(warehouseDTO));
}
}
@Override
public void update(WarehouseDTO warehouseDTO) {
if (warehouseDTO != null && warehouseDTO.getId_warehouse() != null) {
this.warehousesRepository.save(this.toEntity(warehouseDTO));
}
}
@Override
public void deleteById(Long id) {
if (id != null) {
this.warehousesRepository.deleteById(id);
}
}
}
WarehouseRestController:
package com.example.rest;
import com.example.dto.WarehouseDTO;
import com.example.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@ResponseBody
@Component
@RequestMapping("/warehouse")
public class WarehouseRestController {
@Autowired
private WarehouseService warehouseService;
@GetMapping("/all")
private List<WarehouseDTO> findAll(){
return warehouseService.findAll();
}
@PostMapping("/create")
private void create(@RequestBody WarehouseDTO warehouseDTO){
this.warehouseService.create(warehouseDTO);
}
@PutMapping("/update")
private void update(@RequestBody WarehouseDTO warehouseDTO){
this.warehouseService.create(warehouseDTO);
}
@DeleteMapping("/delete/(id)")
private void delete(@PathVariable (value = "id") Long id){
this.warehouseService.deleteById(id);
}
}
SpringStage3Application :
package com.example.Spring_Stage_3;
import com.example.rest.WarehouseRestController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackageClasses = WarehouseRestController.class)
public class SpringStage3Application {
public static void main(String[] args) {
SpringApplication.run(SpringStage3Application.class, args);
}
}
错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.
发布于 2021-07-08 12:32:20
在spring引导中不需要注释@componentScan。问题在于项目的文件夹结构。添加所有其他文件夹(控制器、服务、回购.)在您的主文件夹Spring_Stage_3下。
文件夹结构示例
发布于 2021-07-08 13:07:51
@ComponentScan(basePackageClasses = WarehouseRestController.class)只扫描WarehouseRestController.class包和子包中的组件。您的服务位于不同的包中:com.example.serviceImpl
试着使用:
@SpringBootApplication
@ComponentScan(basePackageClasses = {WarehouseRestController.class,
WarehouseServiceImpl.class,
WarehouseRepositoryImpl.impl
} )
public class SpringStage3Application
PS @SpringBootApplication
还可以作为一种简化的@ComponentScan
,即扫描SpringStage3Application包的所有子包。由于WarehouseRestController在不同的包中,它不会加载,因此您将得到404。
https://stackoverflow.com/questions/68301548
复制相似问题