Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed up vote 0

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 {

@Autowired
public SessionFactory sessionFactory;

@Override
public void persistEmpresas(Empresas empresas) {
    sessionFactory.getCurrentSession().persist(empresas);
}

@Override
public Empresas findEmpresasById(int clave) {
    return (Empresas) sessionFactory.getCurrentSession().get(
            Empresas.class, clave);
}

@Override
public void updateEmpresas(Empresas empresas) {
    sessionFactory.getCurrentSession().update(empresas);

}

@Override
public 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{

@Autowired
EmpresasDAO empresasDAO;

@Override
@Transactional
public void persistEmpresas(Empresas empresas) {
    empresasDAO.persistEmpresas(empresas);

}

@Override
@Transactional
public void updateEmpresas(Empresas empresas) {
    empresasDAO.updateEmpresas(empresas);

}
@Override
@Transactional
public Empresas findEmpresasById(int clave) {
    return empresasDAO.findEmpresasById(clave);
}

@Override
@Transactional
public 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>

share improve this question
 
 
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

1 Answer

up vote 3 down vote accepted

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值