【问题标题】:How to resolve spring bean unavailable error?如何解决spring bean不可用错误?
【发布时间】: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 添加到问题中。

标签: spring hibernate


【解决方案1】:

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

【讨论】:

  • 谢谢。我有 Roles pojo,它反映了 DB 中的 Roles Table,我有 DAO,我有用于 Roles Pojo 的 DaoImpl。我想调用 DaoImpl 的方法。
  • 我并没有真正理解这与您最初的问题有什么关系。你试过重命名你的包吗?你仍然遇到同样的错误吗?
猜你喜欢
  • 2022-07-17
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多