我在使用spring时遇到问题,我是Hibernate,SQL和Maven一起使用此框架的新手,我正在学习教程,但是在服务器中启动应用程序时出现此错误消息。

Spring控制台

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>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:Factory-ref="sessionFactory">
</bean>
</beans>

最佳答案

看来您的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?


为您的数据源尝试以下操作:

<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>


如果您不使用MySQL作为DBS,则必须更改driverClassName和url。

编辑

就像预期的那样。您将数据源配置错误:


<property name="username" value="root" />


代替

<property name="root" value="root" />

关于java - 创建名称为'empresasDAO'的bean时出错:自动连接依赖项的注入(inject)失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23828640/

10-11 10:44