spring bean的bean ID是什么实现了一个接口
What is the bean ID of spring bean implements an interface
当为 class 作为 MyBean
创建 bean 时,bean id 是 myBean
但是 如果我创建服务 bean,bean ID 是什么来自如下界面?
@Service
public class ProfileServiceImpl implements ProfileService
当我尝试访问 bean 时 @profileService
thymeleaf 给出了以下错误。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'profileService' is defined
一直以来,我都通过 autowiring
将这个 bean 用于控制器。但目前我需要从 thymeleaf
.
访问它
我的 thymeleaf 代码段
<div th:unless="${@profileService.isMe(user)}">
for (String name : context.getBeanDefinitionNames()){
System.out.println(name);
}
这个测试揭示了一些有趣的结果。
服务bean
服务 bean 以具体 class 名称命名,与接口无关。
@Service
public class ProfileServiceImpl implements ProfileService
即。 profileServiceImpl
在上面的问题中。
存储库 bean
更多的 Repository beans 更有趣。下面是我的 crud 存储库界面,没有任何注释。
public interface UserRepository extends CrudRepository<User, Long>, DatatablesCriteriasRepository<User>{
我为 DatatablesCriteriasRepository
创建了 UserRepositoryImpl
的实现,如下所示,没有任何注释。
public class UserRepositoryImpl implements DatatablesCriteriasRepository<User>
这两个包含两个 ID 分别为 userRepository
userRepositoryImpl
的 bean。
当Spring 从@Service 或@Component 注释创建Bean 定义时,默认情况下它将通过小写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
。 UserRepsitoryImpl
扩展 DatatablesCriteriasRepository
表明设计存在问题。
当为 class 作为 MyBean
创建 bean 时,bean id 是 myBean
但是 如果我创建服务 bean,bean ID 是什么来自如下界面?
@Service
public class ProfileServiceImpl implements ProfileService
当我尝试访问 bean 时 @profileService
thymeleaf 给出了以下错误。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'profileService' is defined
一直以来,我都通过 autowiring
将这个 bean 用于控制器。但目前我需要从 thymeleaf
.
我的 thymeleaf 代码段
<div th:unless="${@profileService.isMe(user)}">
for (String name : context.getBeanDefinitionNames()){
System.out.println(name);
}
这个测试揭示了一些有趣的结果。
服务bean
服务 bean 以具体 class 名称命名,与接口无关。
@Service
public class ProfileServiceImpl implements ProfileService
即。 profileServiceImpl
在上面的问题中。
存储库 bean
更多的 Repository beans 更有趣。下面是我的 crud 存储库界面,没有任何注释。
public interface UserRepository extends CrudRepository<User, Long>, DatatablesCriteriasRepository<User>{
我为 DatatablesCriteriasRepository
创建了 UserRepositoryImpl
的实现,如下所示,没有任何注释。
public class UserRepositoryImpl implements DatatablesCriteriasRepository<User>
这两个包含两个 ID 分别为 userRepository
userRepositoryImpl
的 bean。
当Spring 从@Service 或@Component 注释创建Bean 定义时,默认情况下它将通过小写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
。 UserRepsitoryImpl
扩展 DatatablesCriteriasRepository
表明设计存在问题。