gpt4 book ai didi

java - @Repository @Component 注释的 NoSuchBeanDefinitionException

转载 作者:行者123 更新时间:2023-12-02 05:07:49 27 4
gpt4 key购买 nike

我正在从事 Spring Hibernate 项目。如果我没有将 bean 名称与 @Repository 或 @Component 一起提及,我会收到错误“NoSuchBeanDefinitionException”。例如: @Repository("HibernateDaoImpl") 工作正常。 @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 默认 bean 名称基于 on uncapitalized short class names 。因此,要么使用 context.getBean("hibernateDaoImpl") 要么指定每个 @Repository("HibernateDAOImpl") 所需的 bean 名称 explicitly .

关于java - @Repository @Component 注释的 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com