如何解决spring bean不可用的错误?

时间:2018-09-15 17:49:56

标签: spring hibernate

在运行启动器类时,出现以下错误

线程“主”中的异常org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“角色”的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”)进行注释。它在 软件包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");
        }

}

1 个答案:

答案 0 :(得分:0)

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

相关问题