spring bean实现接口的bean ID是什么 [英] What is the bean ID of spring bean implements an interface

查看:32
本文介绍了spring bean实现接口的bean ID是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当为 MyBean 类创建 bean 时,bean id 是 myBean 但是 如果我从界面如下图?

@Service公共类 ProfileServiceImpl 实现 ProfileService

当我尝试以 @profileService 身份访问 bean 时,thymeleaf 出现以下错误.

org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有定义名为profileService"的 bean

一直以来我都通过autowiring 使用这个bean 到控制器.但目前我需要从 thymeleaf 访问它.

我的百里香代码段

 

解决方案

当 Spring 从 @Service 或 @Component 注解创建 Bean Definition 时,它会默认通过小写 Class 的第一个字母来为 bean 创建 id姓名.如果您想覆盖该行为,您可以在注释中提供一个替代 id,例如.@Service("profileService").

关于您对存储库的体验 - 默认情况下,Spring 通过将Impl"附加到存储库接口名称来查找存储库的自定义实现.如果找到它,它不会创建默认实现.因此,如果您使用 UserRepositoryImpl extends UserRepository 而不是 UserRepositoryImpl extends DatatablesCriteriasRepository,那么 Spring 就不会创建 userRepository bean.此外,如果您将 @NoRepositoryBean 注释添加到 UserRepository 接口,这将抑制 userRepository bean 的创建.

然而,UserRepositoryImpl 确实应该实现 UserRepository.如果它真的打算扩展DatatablesCriteriasRepository,那么它应该被命名为DatatablesCriteriasRepositoryImpl.让 UserResitoryImpl 扩展 DatatablesCriteriasRepository 表明设计存在问题.

When the bean is createt for a class as MyBean the bean id is myBean but what will be the bean ID if I create the service bean from an interface like below?

@Service
public class ProfileServiceImpl implements ProfileService

When I try to access the bean as @profileService thymeleaf gives the below error.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'profileService' is defined

All this time I'm using this bean by autowiring to the controller. But at the moment I need to access this from the thymeleaf.

My thymeleaf code segment

 <div th:unless="${@profileService.isMe(user)}">

解决方案

When Spring creates a Bean Definition from a @Service or @Component annotation, it will by default create an id for the bean by lowercasing the first letter of the Class Name. If you want to override that the behavior, you can provide an alternative id in the annotation, eg. @Service("profileService").

Regarding what you are experiencing with the Repository - by default Spring looks for a custom implementation of a Repository by appending "Impl" to the Repository Interface name. If it finds it, it will not create a default implementation. So, if you had UserRepositoryImpl extends UserRepository instead of UserRepositoryImpl extends DatatablesCriteriasRepository than Spring wouldn't have created the userRepository bean. Addtionally, if you add @NoRepositoryBean annotation to the UserRepository interface, that will suppress the creation of the userRepository bean.

However, UserRepositoryImpl really should be implementing UserRepository. If it really is intended to extend DatatablesCriteriasRepository, than it should be nameed DatatablesCriteriasRepositoryImpl. Having UserRepsitoryImpl extend DatatablesCriteriasRepository is indication of a problem in the design.

这篇关于spring bean实现接口的bean ID是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆