Java 创建名为“empresasDAO”的 bean 时出错:注入自动装配的依赖项失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23828640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating bean with name 'empresasDAO': Injection of autowired dependencies failed
提问by ProSyth
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 问题,我是一个新手,在 Hibernate、SQL 和 Maven 中使用此框架,我正在学习教程,但是在服务器中启动应用程序时出现此错误消息。
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.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
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
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
应用程序.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
Empresas服务
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
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
应用上下文.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>
采纳答案by chresse
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 driverClassNameand the urlof course.
如果你不使用 MySQL 作为 DBS,你当然必须更改driverClassName和url。
EDIT
编辑
It's like expected. You configured your datasource wrong: Write
这就像预期的那样。您配置的数据源错误:写入
<property name="username" value="root" />
instead of
代替
<property name="root" value="root" />