创建bean时出错。自动连接依赖项的注入失败。无法自动装配

时间:2014-12-16 12:21:16

标签: spring hibernate

你好我是春天mvc的新手,我正在使用@autowired但我不知道解决方案。求求你。对不起,但我的英语,我正在学习它。 我有两个类Empresa.java和Ldiario.java。 Empresa.java它的工作原理。

更新 我更新了以下代码,但我有同样的错误。 应用-config.xml中

 <!-- Hibernate Session Factory -->
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan" value="com.atorresbr.springapp.**"/>

我添加了一个豆。

<bean id="diarioBean" class="com.atorresbr.springapp.bean.DiarioBean"/>

错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'diarioBean': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atorresbr.springapp.model.Ldiario com.atorresbr.springapp.bean.DiarioBean.ldiario; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.atorresbr.springapp.model.Ldiario] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    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)

DiarioBean.java

@ManagedBean(name="diarioBean")
@SessionScoped
@Component
public class DiarioBean {

    @Autowired
    private Ldiario ldiario;
    @Autowired
    private Ldiario selectedLdiario;

    public DiarioBean(){

    }
    public Ldiario getLdiario() {
        return ldiario;
    }
    public void setLdiario(Ldiario ldiario) {
        this.ldiario = ldiario;
    }
    public Ldiario getSelectedLdiario() {
        return selectedLdiario;
    }
    public void setSelectedLdiario(Ldiario selectedLdiario) {
        this.selectedLdiario = selectedLdiario;
    }   

}

Ldiario.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import java.io.Serializable;
import java.sql.Blob;
import java.util.Date;

@Entity
@Table(name = "LDIARIO2014")
public class Ldiario implements Serializable{


@Id 
@Column(name = "CLAVE")
@GeneratedValue private Integer CLAVE;  

@ManyToOne  
@JoinColumn (name = "empresa") 
private Empresa empresa;



@Column(name = "ASIENTO")
private Integer ASIENTO;

@Column(name = "ORDEN")
private Integer ORDEN;

@Column(name = "FECHA")
private Date FECHA;

应用-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- Scans the classpath of this application for @Components @Service @Repository to deploy as 
        beans -->
    <context:component-scan base-package="com.atorresbr.springapp" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

 <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

        <bean id="diarioBean" class="com.atorresbr.springapp.bean.DiarioBean"/>

  <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
       username root and blank password. Change below if it's not the case -->

       <bean id="myDataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}"
        p:password="${jdbc.password}" 
        p:validationQuery="${jdbc.validationQuery}" />

 <!--       
  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/pizzashop"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    <property name="validationQuery" value="SELECT 1"/>
  </bean>   -->

  <!-- Hibernate Session Factory -->
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan" value="com.atorresbr.springapp.**"/>

    <property name="hibernateProperties">
    <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop> 
            </props>

    </property>
  </bean>

  <!-- Hibernate Transaction Manager -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>

  <!-- Activates annotation based transaction management -->
  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

0 个答案:

没有答案