简体   繁体   English

@Repository @Component批注的NoSuchBeanDefinitionException

[英]NoSuchBeanDefinitionException for @Repository @Component annotations

Im working on spring hibernate project. 我正在从事春季休眠项目。 Im getting an error "NoSuchBeanDefinitionException" if I didnt mention bean name together with @Repository or @Component. 如果我没有与@Repository或@Component一起提及bean名称,我将收到错误消息“ NoSuchBeanDefinitionException”。 For example : @Repository("HibernateDaoImpl") works fine. 例如:@Repository(“ HibernateDaoImpl”)可以正常工作。 @Repository gives an error. @Repository给出错误。

INFO: Building new Hibernate SessionFactory
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'HibernateDaoImpl' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
    at com.amal.springdb.JdbcDemo.main(JdbcDemo.java:23)

package com.amal.springdb.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Circle {

    @Id
    private int id;
    private String name;

    public Circle(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Circle() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

package com.amal.springdb.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository  //("HibernateDaoImpl")
public class HibernateDaoImpl {

    @Autowired
    private SessionFactory sessionFactory;

    public int getCircleCount(){
        String hql = "select count(*) from Circle";
        Query query = getSessionFactory().openSession().createQuery(hql);
        int i =  ((Long)query.uniqueResult()).intValue();
        return i;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

package com.amal.springdb;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amal.springdb.dao.HibernateDaoImpl;
import com.amal.springdb.dao.JdbcDaoImpl;
import com.amal.springdb.dao.SimpleJdbcDaoImpl;
import com.amal.springdb.model.Circle;

public class JdbcDemo {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");


        HibernateDaoImpl hibernateDaoImpl = (HibernateDaoImpl) context.getBean("HibernateDaoImpl");
        System.out.println(hibernateDaoImpl.getCircleCount());  
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config></context:annotation-config>

    <context:component-scan base-package="com.amal.springdb"></context:component-scan>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"></property>
        <property name="url" value="jdbc:derby://localhost:1527/db;create=true"></property>
        <property name="initialSize" value="2"></property>
        <property name="maxActive" value="5"></property>
    </bean>

    <bean id="SimpleJdbcDaoImpl" class="com.amal.springdb.dao.SimpleJdbcDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>    
        <property name="packagesToScan" value="com.amal.springdb.model"></property>
        <property name="hibernateProperties">   
            <props>
                 <prop key="dialect">org.hibernate.dialect.DerbyDialect</prop>
            </props>        
        </property>
    </bean>
</beans>

Spring default bean names are based on on uncapitalized short class names . Spring缺省Bean名称基于没有大写字母的短类名称 Therefore, either use context.getBean("hibernateDaoImpl") or specify the desired bean name per @Repository("HibernateDAOImpl") explicitly . 因此,可以使用context.getBean("hibernateDaoImpl")或通过@Repository("HibernateDAOImpl") 显式指定所需的bean名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在使用所有注释和组件扫描之后也获得NoSuchBeanDefinitionException - Getting NoSuchBeanDefinitionException even after using all annotations and component scan NoSuchBeanDefinitionException库中的存储库 - NoSuchBeanDefinitionException for a repository in a library Spring NoSuchBeanDefinitionException 存储库进入服务 - Spring NoSuchBeanDefinitionException repository into service Spring注释:@Component工作,@ Repository没有 - Spring annotations: @Component works, @Repository doesn't NoSuchBeanDefinitionException:没有类型合格的bean(存储库) - NoSuchBeanDefinitionException: No qualifying bean of type (repository) 如何处理@Component和@Repository / @Service注释之间的区别是什么? - What's the difference between how @Component and @Repository / @Service annotations are processed? Spring 中的@Component、@Repository 和@Service 注解有什么区别? - What's the difference between @Component, @Repository & @Service annotations in Spring? 使用 Repository 注释时 Spring NoSuchBeanDefinitionException - Spring NoSuchBeanDefinitionException while using Repository annotation Spring-将存储库自动装配到服务中时出现NoSuchBeanDefinitionException - Spring - NoSuchBeanDefinitionException when autowiring Repository into Service Spring.NoSuchBeanDefinitionException干扰 <pre-post-annotations=“enabled”/> ? - Spring.NoSuchBeanDefinitionException intefering with <pre-post-annotations=“enabled”/>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM