它报的错误代码是:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDAOImpl' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
at com.hibernate.demo.Test.main(Test.java:16)
它的配置是:applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="autodetect">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/hibernate"></property>
<property name="username" value="sa"></property>
<property name="password" value="song"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.hibernate.model.User</value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value>com.hibernate.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
<context:component-scan base-package ="com.hibernate.model.UserDAOImpl"/>
</beans>
我的源代码是:UserDAOImpl.javapackage com.hibernate.dao;
import org.springframework.stereotype.Component;
import com.hibernate.model.User;
@Component("userDAOImpl")
public class UserDAOImpl extends SuperDAO implements UserDAO{
public void save(User user) {
this.save(user);
}
}
SuperDAO.java@Component
public class SuperDAO extends HibernateDaoSupport {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
this.setSuperSessionFactory(sessionFactory);
}
}调用:Test.javapublic class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDAOImpl userDAOImpl=(UserDAOImpl)context.getBean("userDAOImpl");
User user=new User();
user.setId(1);
user.setName("和三");
user.setAge(11);
user.setSex("男");
userDAOImpl.save(user);
}
}
请大家帮忙指出错误!
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
at com.hibernate.demo.Test.main(Test.java:16)
它的配置是:applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="autodetect">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/hibernate"></property>
<property name="username" value="sa"></property>
<property name="password" value="song"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.hibernate.model.User</value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value>com.hibernate.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
<context:component-scan base-package ="com.hibernate.model.UserDAOImpl"/>
</beans>
我的源代码是:UserDAOImpl.javapackage com.hibernate.dao;
import org.springframework.stereotype.Component;
import com.hibernate.model.User;
@Component("userDAOImpl")
public class UserDAOImpl extends SuperDAO implements UserDAO{
public void save(User user) {
this.save(user);
}
}
SuperDAO.java@Component
public class SuperDAO extends HibernateDaoSupport {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
this.setSuperSessionFactory(sessionFactory);
}
}调用:Test.javapublic class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDAOImpl userDAOImpl=(UserDAOImpl)context.getBean("userDAOImpl");
User user=new User();
user.setId(1);
user.setName("和三");
user.setAge(11);
user.setSex("男");
userDAOImpl.save(user);
}
}
请大家帮忙指出错误!
解决方案 »
- ssh,dwr的问题???
- Spring与Hibernate整合,session不关闭问题
- 请问我的xml文件哪里出问题了。。
- 如何在WEB端显示struts2传过来的对象
- 高手谈谈GBK和gb2313
- hibernate输出sql如何显示参数的实际值而不是?号
- ********杨辉三角形*********一维数组来实现*************我算法对的,就是不能对齐*************
- 高手们,说明一下什么是WebLogic。有什么用。如何使用,安装和配置。
- 求助,WEBLOGIC8安装完,环境变量应该怎么配置啊
- 一个关于处理XML文件的需求,请教大家。。。急
- 请八路神仙解解难题……本人不甚感激……当以以身相许……
- ssh项目action不能注入
其属已经说了是 base-PACKAGE , 怎么你还写成类的全限定类名呢?? 开启了 Classpath 扫描后, Spring 会扫描配置的包下面所有的子包,
所以你的配置改成 base-package="com.hibernate.model" /> 即可
另外好像还少了一项:
<context:annotation-config />
不然@Resource没法用。
Tag name: component-scan Description : Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected. Note: This tag implies the effects of the 'annotation-config' tag, activating @Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit其实开启了 Classpath 扫描前者就可以不用了, 这个也是我一次配置错误的时候发现同样可以使用的时候
偶然发现的 :)
@Component("userDAOImpl")
public class UserDAOImpl extends SuperDAO implements UserDAO{}
@Component换成@Repository试试,另外用接口名作bean名,即:
@Repository("userDAO")使用时: UserDAO userDAO = (UserDAO)context.getBean("userDAO");
最好是这种习惯,因为已经定制了接口。
另外谢谢4楼的提醒,我抽空做下实验。
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
俺平常都是这么配的,你这种配法没研究过
改为
<context:component-scan base-package ="com.hibernate"/>
jar包下载
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOImpl' defined in file [D:\workspace\SSH\WebRoot\WEB-INF\classes\com\hibernate\dao\UserDAOImpl.class]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'superSessionFactory' threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.hibernate.demo.Test.main(Test.java:11)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'superSessionFactory' threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:104)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
... 16 more
是不是有死循环了?既然:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
那么就可以在SuperDAO中这样取:
@Resource protected SessionFactory sessionFactory;
(setter && getter可要可不要)注意变量修饰权限为protected,这样可以在子类中:
Session session = sessionFactory.getCurrentSession();那么就可以:
session.save(user);而不能用getHibernateTemplate().save(user)了,因为HibernateTemplate根本没有在容器中装配。最后注意事务控制。
可以SuperDAO上配上注解:@Transactional。但提前是xml中要声明事务。