spring - 如何解决spring bean不可用错误?
问题描述
在运行我的启动器类时,我收到以下错误
线程“主”org.springframework.beans.factory.NoSuchBeanDefinitionException 中的异常:没有名为“角色”的 bean 可用
我的 RoleLauncher 类 // 读取 spring 配置 java 类
package come.rahul.spring.launcher;
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(RolesConfig.class);
Roles roles = context.getBean("roles", Roles.class);
我的 RolesConfig.class 仅使用 @Configuration 和 @ComponentScan("com.rahul.spring") 进行了注释。它在 包 come.rahul.spring.configuartion;
我的角色类是
package come.rahul.spring.entity;
@Component
public class Roles {
private Long roleId;
private String roleName;
//getter and setter omitted for brevity
我有一个道,它也是实施
package come.rahul.spring.dao;
public interface RolesDao
{
//List<Roles> getRoles(); omitted for brevity
void print() ;
}
它的实现如下:
package come.rahul.spring.dao;
@Repository
public class RolesDaoImpl implements RolesDao
public void print() {
System.out.println( " Inside Print method of RolesDaoImpl");
}
}
解决方案
您使用@ComponentScan("com.rahul.spring")
但无处不在,您使用package come.rahul.spring;
. 您需要在任何地方使用 com 而不是 come
推荐阅读
- r - Add variable with summarise but keep all other variables in R
- generics - Why do I have to destruct and reconstruct the non-generic variant of a generic enum when mapping from one generic to another?
- python - No module named numpy but requirements are already satisfied (Anaconda)
- node.js - How to display the data from MongoDB in Reactjs?
- css - 为什么没有应用矩形上的样式?
- oracle - where is the Azure runtime Integration has to be installed?
- python - 如何在 sqlalchemy 中获得循环关系?
- junit5 - Does JUnit5 test result contains "Error", if it does, how to mark a test as "Error"?
- php - phpunit: Avoid printing very long output
- javascript - 如何修复 validateDOMNesting(...): 不能作为 . 并且列表中的每个孩子都应该有一个唯一的“关键”道具