Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed up vote 0
来源:互联网 发布:淘宝助理水印图片大小 编辑:程序博客网 时间:2024/05/16 14:42
Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed
up vote0down votefavorite
I am having a problem with spring , I am a newbie using this framework with Hibernate , SQL and Maven , I am following a tutorial but when launching the app in the server I have this error message.
Spring Console
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.hibernate.SessionFactory com.altobri.conta.dao.EmpresasDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'root' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'root' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)at com.altobri.conta.App.main(App.java:13)
EmpresasDAO
package com.altobri.conta.dao;import com.altobri.conta.model.Empresas;public interface EmpresasDAO{ void persistEmpresas(Empresas empresas);Empresas findEmpresasById(int clave);void updateEmpresas(Empresas empresas);void deleteEmpresas(Empresas empresas); }
EmpresasDAOImpl
package com.altobri.conta.dao;import org.hibernate.SessionFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.altobri.conta.model.Empresas;@Repository("empresasDAO")public class EmpresasDAOImpl implements EmpresasDAO {@Autowiredpublic SessionFactory sessionFactory;@Overridepublic void persistEmpresas(Empresas empresas) { sessionFactory.getCurrentSession().persist(empresas);}@Overridepublic Empresas findEmpresasById(int clave) { return (Empresas) sessionFactory.getCurrentSession().get( Empresas.class, clave);}@Overridepublic void updateEmpresas(Empresas empresas) { sessionFactory.getCurrentSession().update(empresas);}@Overridepublic void deleteEmpresas(Empresas empresas) { sessionFactory.getCurrentSession().delete(empresas); } }
App.java
package com.altobri.conta;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.altobri.conta.model.Empresas;import com.altobri.conta.service.EmpresasService; public class App {public static void main(String[] args) { System.out.println("load context"); ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Empresas em = new Empresas(); em.setClave(123); em.setNombre("John"); EmpresasService emService = (EmpresasService) context .getBean("empresasService"); emService.persistEmpresas(em); System.out.println("Updated age :" + emService.findEmpresasById(123).getNombre()); emService.updateEmpresas(em); System.out.println("Updated age :" + emService.findEmpresasById(123).getClave()); emService.deleteEmpresas(em); context.close(); } }
EmpresasService
package com.altobri.conta.service;import com.altobri.conta.model.Empresas;public interface EmpresasService {void persistEmpresas(Empresas empresas);Empresas findEmpresasById(int clave);void updateEmpresas(Empresas empresas);void deleteEmpresas(Empresas empresas); }
EmpresasServiceImpl
package com.altobri.conta.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.altobri.conta.dao.EmpresasDAO;import com.altobri.conta.model.Empresas;@Service("empresasService")public class EmpresasServiceImpl implements EmpresasService{@AutowiredEmpresasDAO empresasDAO;@Override@Transactionalpublic void persistEmpresas(Empresas empresas) { empresasDAO.persistEmpresas(empresas);}@Override@Transactionalpublic void updateEmpresas(Empresas empresas) { empresasDAO.updateEmpresas(empresas);}@Override@Transactionalpublic Empresas findEmpresasById(int clave) { return empresasDAO.findEmpresasById(clave);}@Override@Transactionalpublic void deleteEmpresas(Empresas empresas) { empresasDAO.deleteEmpresas(empresas);} }
applicationContext.xml
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:component-scan base-package="com.altobri.conta.*" /> <context:component-scan base-package="com.springHibernate" /> <context:annotation-config /> <tx:annotation-driven/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroymethod="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/empresas" /><property name="root" value="root" /><property name="password" value="" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property> <property name="annotatedClasses"> <list> <value>com.altobri.conta.model.Empresas</value> </list> </property><property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> </props></property>
java sql hibernate spring-mvc
Post the applicationContext.xml. It seems something wrong with root bean there. According to your error message – StanislavL May 23 '14 at 12:03
can you show the config, where you configure your dataSource? – chresse May 23 '14 at 12:07
I just added my applicationContext.xml thank you. – ProSyth May 23 '14 at 12:25
add a comment
1 Answer
activeoldestvotes
up vote3down vote
It seems you have a problem with your dataSource
...Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'root' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'root' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Try something like this for your datasource:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://[HostAndPort]/[DatabaseName]" /> <property name="username" value="[yourUsernameGoesHere]" /> <property name="password" value="[yourPasswordGoesHere]" /> <property name="initialSize" value="[initialSize]" /> <property name="maxActive" value="[maxActiveConnectionsGoesHere]" /></bean>
If you don't use MySQL as DBS, you have to change the driverClassName and the url of course.
EDIT
It's like expected. You configured your datasource wrong: Write
<property name="username" value="root" />
instead of
<property name="root" value="root" />
0 0
- Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed up vote 0
- Error creating bean with name '****': Injection of autowired dependencies failed
- bean依赖注入错误:Error creating bean with name 'itemService': Injection of autowired dependencies failed
- Error creating bean with name 'adminUserController': Injection of autowired dependencies failed;
- Error creating bean with name 'xx': Injection of autowired dependencies failed
- Error creating bean with name 'xxxx': Injection of autowired dependencies failed;
- Error creating bean with name 'XXX': Injection of autowired dependencies failed解决办法
- Java-4-解决报错:Error creating bean with name 'globalAppObj': Injection of autowired dependencies failed
- 出现:Error creating bean with name 'xxxxxController': Injection of autowired dependencies failed;
- Error creating bean with name '***(类名)': Injection of autowired dependencies failed;
- Error creating bean with name 'assetQueryController': Injection of autowired dependencies failed; ne
- Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed
- rror creating bean with name 'adminController': Injection of autowired dependencies failed;
- Error creating bean with name 'com.you.user.dao.StudentDaoTest': Injection of autowired dependencies
- spring问题:Error creating bean with name 'articleController': Injection of autowired dependencies fail
- jboss服务启动时报Error creating bean with name'addressService': Injection of autowired dependencies faile
- Error creating bean with name 'contentTypeAdminController': Injection of autowired dependencies fail
- 【SPRINGMVC】Error creating bean with name 'projectController': Injection of autowired dependencies
- 更改Appsettings
- Qt归纳(三)
- 面试准备---计算机网络(一)
- LeetCode之Merge Two Sorted Lists
- xcode RTImageAssets插件(将@3x图片转换为@2x和@1x)
- Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed up vote 0
- 创业之技术合伙人--明道,取势,优术
- XPath选择节点
- eclipse 无线调试
- Qt归纳(四)
- 【Android本地开发技术:编译脚本】Application.mk
- 网站广告弹出层(每天弹出一次)
- awk学习
- 【LeetCode】Word Break