首页 > 解决方案 > 如何解决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");
        }

}

标签: springhibernate

解决方案


您使用@ComponentScan("com.rahul.spring")但无处不在,您使用package come.rahul.spring;. 您需要在任何地方使用 com 而不是 come


推荐阅读