【发布时间】:2019-02-20 04:11:48
【问题描述】:
在运行我的启动器类时,我收到以下错误
线程“main”org.springframework.beans.factory.NoSuchBeanDefinitionException 中的异常:没有可用的名为“roles”的 bean
我的 RoleLauncher 类 // 读取spring config 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") 进行了注释。它在 包来.rahul.spring.configuartion;
我的角色类是
package come.rahul.spring.entity;
@Component
public class Roles {
private Long roleId;
private String roleName;
//getter and setter omitted for brevity
我有一个 Dao,它也可以实现
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");
}
}
【问题讨论】:
-
请将您的 RolesConfig.class 添加到问题中。