Spring Boot 应用程序因 bean 问题而无法启动
Springboot Application Failed to start because of a bean problem
我正在学习基础开发实践。
创建 CRUD 服务并尝试抽象该服务时,发生了与 bean 相关的错误。
这是我的代码
1.CrudInterface
public interface CrudInterface<Req, Res> {
Header<Res> create(Header<Req> request);
Header<Res> read(Long id);
Header<Res> update(Header<Req> request);
Header delete(Long id);
}
2.CrudController.java
@Component
public class CrudController<Req,Res,Entity> implements
CrudInterface<Req,Res> {
@Autowired(required = false)
protected BaseService<Req,Res,Entity> entityBaseService;
@Override
@PostMapping("")
public Header<Res> create(@RequestBody Header<Req> request) {
return entityBaseService.create(request);
}
@Override
@GetMapping("{id}")
public Header<Res> read(@PathVariable Long id) {
return entityBaseService.read(id);
}
@Override
@PutMapping("")
public Header<Res> update(@RequestBody Header<Req> request) {
return entityBaseService.update(request);
}
@Override
@DeleteMapping("{id}")
public Header delete(@PathVariable Long id) {
return entityBaseService.delete(id);
}
}
3.UserApiController.java
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApiController extends
CrudController<UserApiRequest, UserApiResponse, User> {
}
4.BaseService.java
@Component
public abstract class BaseService<Req,Res,Entity> implements
CrudInterface<Req,Res> {
@Autowired(required = false)
protected JpaRepository<Entity,Long> baseRepository;
}
5.UserApiLogicService
@Service
public class UserApiLogicService extends
BaseService<UserApiRequest, UserApiResponse,User> {
@Override
public Header<UserApiResponse> create(Header<UserApiRequest> request) {
UserApiRequest body = request.getData();
User user = User.builder()
.account(body.getAccount())
.password(body.getPassword())
.status(UserStatus.REGISTERED)
.phoneNumber(body.getPhoneNumber())
.email(body.getEmail())
.registeredAt(LocalDateTime.now())
.build();
User newUser = baseRepository.save(user);
return response(newUser);
}
@Override
public Header<UserApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(user -> response(user))
.orElseGet(
() -> Header.ERROR("데이터 없음")
);
}
@Override
public Header<UserApiResponse> update(Header<UserApiRequest> request) {
UserApiRequest body = request.getData();
Optional<User> optional = baseRepository.findById(body.getId());
return optional.map(user -> {
user.setAccount(body.getAccount())
.setPassword(body.getPassword())
.setStatus(body.getStatus())
.setPhoneNumber(body.getPhoneNumber())
.setEmail(body.getEmail())
.setRegisteredAt(body.getRegisteredAt())
.setUnregisteredAt(body.getUnregisteredAt());
return user;
})
.map(user -> baseRepository.save(user)) // update -> newUser
.map(updateUser -> response(updateUser)) // userApiResponse
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
Optional<User> optional = baseRepository.findById(id);
// 2. repository -> delete
return optional.map(user -> {
baseRepository.delete(user);
return Header.OK();
})
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
private Header<UserApiResponse> response(User user) {
UserApiResponse userApiResponse = UserApiResponse.builder()
.id(user.getId())
.account(user.getAccount())
.password(user.getPassword())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.status(user.getStatus())
.registeredAt(user.getRegisteredAt())
.unregisteredAt(user.getUnregisteredAt())
.build();
return Header.OK(userApiResponse);
}
}
6.CategoryApiLogicService.java
@Service
public class CategoryApiLogicService extends
BaseService<CategoryApiRequest, CategoryApiResponse,Category> {
@Override
public Header<CategoryApiResponse> create(Header<CategoryApiRequest> request) {
CategoryApiRequest body = request.getData();
Category category = Category.builder()
.type(body.getType())
.title(body.getTitle())
.createdAt(body.getCreatedAt())
.createdBy(body.getCreatedBy())
.build();
Category newCategory = baseRepository.save(category);
return response(newCategory);
}
@Override
public Header<CategoryApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(category -> response(category))
.orElseGet(()-> Header.ERROR("데이터 없음"));
}
@Override
public Header<CategoryApiResponse> update(Header<CategoryApiRequest> request) {
CategoryApiRequest body = request.getData();
return baseRepository.findById(body.getId())
.map(category -> {
category
.setType(body.getType())
.setTitle(body.getTitle())
.setCreatedAt(body.getCreatedAt())
.setCreatedBy(body.getCreatedBy());
return category;
})
.map(changeCategory -> baseRepository.save(changeCategory))
.map(newCategory -> response(newCategory))
.orElseGet(()->Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
return baseRepository.findById(id)
.map(category -> {
baseRepository.delete(category);
return Header.OK();
})
.orElseGet(()->Header.ERROR("데이터 없음"));
}
private Header<CategoryApiResponse> response(Category category){
CategoryApiResponse body = CategoryApiResponse.builder()
.id(category.getId())
.type(category.getType())
.title(category.getTitle())
.createdAt(category.getCreatedAt())
.createdBy(category.getCreatedBy())
.build();
return Header.OK(body);
}
}
8.ItemApiLogicService.java
@Service
public class ItemApiLogicService extends
BaseService<ItemApiRequest,ItemApiResponse,Item> {
@Autowired
private PartnerRepository partnerRepository;
@Override
public Header<ItemApiResponse> create(Header<ItemApiRequest> request) {
ItemApiRequest body = request.getData();
Item item = Item.builder()
.status(body.getStatus())
.name(body.getName())
.title(body.getTitle())
.content(body.getContent())
.price(body.getPrice())
.brandName(body.getBrandName())
.registeredAt(LocalDateTime.now())
.partner(partnerRepository.getOne(body.getPartnerId()))
.build();
Item newItem = baseRepository.save(item);
return response(newItem);
}
@Override
public Header<ItemApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(user -> response(user))
.orElseGet(
() -> Header.ERROR("데이터 없음")
);
}
@Override
public Header<ItemApiResponse> update(Header<ItemApiRequest> request) {
ItemApiRequest body = request.getData();
return baseRepository.findById(body.getId())
.map(entityItem -> {
entityItem
.setStatus(body.getStatus())
.setName(body.getName())
.setTitle(body.getTitle())
.setContent(body.getContent())
.setPrice(body.getPrice())
.setBrandName(body.getBrandName())
.setRegisteredAt(body.getRegisteredAt())
.setUnregisteredAt(body.getUnregisteredAt());
return entityItem;
})
.map(newEntityItem -> baseRepository.save(newEntityItem))
.map(item -> response(item))
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
return baseRepository.findById(id)
.map(item -> {
baseRepository.delete(item);
return Header.OK();
})
.orElseGet(() -> Header.ERROR("데이터없음"));
}
private Header<ItemApiResponse> response(Item item) {
ItemApiResponse body = ItemApiResponse.builder()
.id(item.getId())
.status(item.getStatus())
.name(item.getName())
.title(item.getTitle())
.content(item.getContent())
.price(item.getPrice())
.brandName(item.getBrandName())
.registeredAt(item.getRegisteredAt())
.unregisteredAt(item.getUnregisteredAt())
.partnerId(item.getPartner().getId())
.build();
return Header.OK(body);
}
}
这是我的错误信息
ERROR 4516 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
Field entityBaseService in com.example.admin.controller.CrudController required a single bean, but 6 were found:
- categoryApiLogicService: defined in file [D:\~\service\CategoryApiLogicService.class]
- itemApiLogicService: defined in file [D:\~\service\ItemApiLogicService.class]
- orderDetailApiLogicService: defined in file [D:\~\service\OrderDetailApiLogicService.class]
- orderGroupApiLogicService: defined in file [D:\~\service\OrderGroupApiLogicService.class]
- partnerApiLogicService: defined in file [D:\~\service\PartnerApiLogicService.class]
- userApiLogicService: defined in file [D:\~\service\UserApiLogicService.class]
感谢您阅读这个长篇故事。
希望您能发现错误。
可能有 6 个 classes 实现 BaseService
并且您决定使用名称 entityBaseService
的抽象 class,因此 Spring 无法准确绑定您的 bean想要。
您可以在 class 上为默认 bean 使用 @primary 掩码,或者使用 @qualifier 与 bean 名称绑定
更喜欢:http://zetcode.com/springboot/qualifier
我正在学习基础开发实践。
创建 CRUD 服务并尝试抽象该服务时,发生了与 bean 相关的错误。
这是我的代码
1.CrudInterface
public interface CrudInterface<Req, Res> {
Header<Res> create(Header<Req> request);
Header<Res> read(Long id);
Header<Res> update(Header<Req> request);
Header delete(Long id);
}
2.CrudController.java
@Component
public class CrudController<Req,Res,Entity> implements
CrudInterface<Req,Res> {
@Autowired(required = false)
protected BaseService<Req,Res,Entity> entityBaseService;
@Override
@PostMapping("")
public Header<Res> create(@RequestBody Header<Req> request) {
return entityBaseService.create(request);
}
@Override
@GetMapping("{id}")
public Header<Res> read(@PathVariable Long id) {
return entityBaseService.read(id);
}
@Override
@PutMapping("")
public Header<Res> update(@RequestBody Header<Req> request) {
return entityBaseService.update(request);
}
@Override
@DeleteMapping("{id}")
public Header delete(@PathVariable Long id) {
return entityBaseService.delete(id);
}
}
3.UserApiController.java
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApiController extends
CrudController<UserApiRequest, UserApiResponse, User> {
}
4.BaseService.java
@Component
public abstract class BaseService<Req,Res,Entity> implements
CrudInterface<Req,Res> {
@Autowired(required = false)
protected JpaRepository<Entity,Long> baseRepository;
}
5.UserApiLogicService
@Service
public class UserApiLogicService extends
BaseService<UserApiRequest, UserApiResponse,User> {
@Override
public Header<UserApiResponse> create(Header<UserApiRequest> request) {
UserApiRequest body = request.getData();
User user = User.builder()
.account(body.getAccount())
.password(body.getPassword())
.status(UserStatus.REGISTERED)
.phoneNumber(body.getPhoneNumber())
.email(body.getEmail())
.registeredAt(LocalDateTime.now())
.build();
User newUser = baseRepository.save(user);
return response(newUser);
}
@Override
public Header<UserApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(user -> response(user))
.orElseGet(
() -> Header.ERROR("데이터 없음")
);
}
@Override
public Header<UserApiResponse> update(Header<UserApiRequest> request) {
UserApiRequest body = request.getData();
Optional<User> optional = baseRepository.findById(body.getId());
return optional.map(user -> {
user.setAccount(body.getAccount())
.setPassword(body.getPassword())
.setStatus(body.getStatus())
.setPhoneNumber(body.getPhoneNumber())
.setEmail(body.getEmail())
.setRegisteredAt(body.getRegisteredAt())
.setUnregisteredAt(body.getUnregisteredAt());
return user;
})
.map(user -> baseRepository.save(user)) // update -> newUser
.map(updateUser -> response(updateUser)) // userApiResponse
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
Optional<User> optional = baseRepository.findById(id);
// 2. repository -> delete
return optional.map(user -> {
baseRepository.delete(user);
return Header.OK();
})
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
private Header<UserApiResponse> response(User user) {
UserApiResponse userApiResponse = UserApiResponse.builder()
.id(user.getId())
.account(user.getAccount())
.password(user.getPassword())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.status(user.getStatus())
.registeredAt(user.getRegisteredAt())
.unregisteredAt(user.getUnregisteredAt())
.build();
return Header.OK(userApiResponse);
}
}
6.CategoryApiLogicService.java
@Service
public class CategoryApiLogicService extends
BaseService<CategoryApiRequest, CategoryApiResponse,Category> {
@Override
public Header<CategoryApiResponse> create(Header<CategoryApiRequest> request) {
CategoryApiRequest body = request.getData();
Category category = Category.builder()
.type(body.getType())
.title(body.getTitle())
.createdAt(body.getCreatedAt())
.createdBy(body.getCreatedBy())
.build();
Category newCategory = baseRepository.save(category);
return response(newCategory);
}
@Override
public Header<CategoryApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(category -> response(category))
.orElseGet(()-> Header.ERROR("데이터 없음"));
}
@Override
public Header<CategoryApiResponse> update(Header<CategoryApiRequest> request) {
CategoryApiRequest body = request.getData();
return baseRepository.findById(body.getId())
.map(category -> {
category
.setType(body.getType())
.setTitle(body.getTitle())
.setCreatedAt(body.getCreatedAt())
.setCreatedBy(body.getCreatedBy());
return category;
})
.map(changeCategory -> baseRepository.save(changeCategory))
.map(newCategory -> response(newCategory))
.orElseGet(()->Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
return baseRepository.findById(id)
.map(category -> {
baseRepository.delete(category);
return Header.OK();
})
.orElseGet(()->Header.ERROR("데이터 없음"));
}
private Header<CategoryApiResponse> response(Category category){
CategoryApiResponse body = CategoryApiResponse.builder()
.id(category.getId())
.type(category.getType())
.title(category.getTitle())
.createdAt(category.getCreatedAt())
.createdBy(category.getCreatedBy())
.build();
return Header.OK(body);
}
}
8.ItemApiLogicService.java
@Service
public class ItemApiLogicService extends
BaseService<ItemApiRequest,ItemApiResponse,Item> {
@Autowired
private PartnerRepository partnerRepository;
@Override
public Header<ItemApiResponse> create(Header<ItemApiRequest> request) {
ItemApiRequest body = request.getData();
Item item = Item.builder()
.status(body.getStatus())
.name(body.getName())
.title(body.getTitle())
.content(body.getContent())
.price(body.getPrice())
.brandName(body.getBrandName())
.registeredAt(LocalDateTime.now())
.partner(partnerRepository.getOne(body.getPartnerId()))
.build();
Item newItem = baseRepository.save(item);
return response(newItem);
}
@Override
public Header<ItemApiResponse> read(Long id) {
return baseRepository.findById(id)
.map(user -> response(user))
.orElseGet(
() -> Header.ERROR("데이터 없음")
);
}
@Override
public Header<ItemApiResponse> update(Header<ItemApiRequest> request) {
ItemApiRequest body = request.getData();
return baseRepository.findById(body.getId())
.map(entityItem -> {
entityItem
.setStatus(body.getStatus())
.setName(body.getName())
.setTitle(body.getTitle())
.setContent(body.getContent())
.setPrice(body.getPrice())
.setBrandName(body.getBrandName())
.setRegisteredAt(body.getRegisteredAt())
.setUnregisteredAt(body.getUnregisteredAt());
return entityItem;
})
.map(newEntityItem -> baseRepository.save(newEntityItem))
.map(item -> response(item))
.orElseGet(() -> Header.ERROR("데이터 없음"));
}
@Override
public Header delete(Long id) {
return baseRepository.findById(id)
.map(item -> {
baseRepository.delete(item);
return Header.OK();
})
.orElseGet(() -> Header.ERROR("데이터없음"));
}
private Header<ItemApiResponse> response(Item item) {
ItemApiResponse body = ItemApiResponse.builder()
.id(item.getId())
.status(item.getStatus())
.name(item.getName())
.title(item.getTitle())
.content(item.getContent())
.price(item.getPrice())
.brandName(item.getBrandName())
.registeredAt(item.getRegisteredAt())
.unregisteredAt(item.getUnregisteredAt())
.partnerId(item.getPartner().getId())
.build();
return Header.OK(body);
}
}
这是我的错误信息
ERROR 4516 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
Field entityBaseService in com.example.admin.controller.CrudController required a single bean, but 6 were found:
- categoryApiLogicService: defined in file [D:\~\service\CategoryApiLogicService.class]
- itemApiLogicService: defined in file [D:\~\service\ItemApiLogicService.class]
- orderDetailApiLogicService: defined in file [D:\~\service\OrderDetailApiLogicService.class]
- orderGroupApiLogicService: defined in file [D:\~\service\OrderGroupApiLogicService.class]
- partnerApiLogicService: defined in file [D:\~\service\PartnerApiLogicService.class]
- userApiLogicService: defined in file [D:\~\service\UserApiLogicService.class]
感谢您阅读这个长篇故事。 希望您能发现错误。
可能有 6 个 classes 实现 BaseService
并且您决定使用名称 entityBaseService
的抽象 class,因此 Spring 无法准确绑定您的 bean想要。
您可以在 class 上为默认 bean 使用 @primary 掩码,或者使用 @qualifier 与 bean 名称绑定
更喜欢:http://zetcode.com/springboot/qualifier