引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为' mesarimLogService'被定义为

时间:2014-04-28 08:16:56

标签: java spring hibernate jpa

我在尝试从独立java应用程序的spring上下文中获取bean时遇到此错误。我正在使用jdk 1.6.0_37和hibernate-jpa-2.0-api-1.0.0.Final.jar

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mesarimLogService' is defined
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
log-config.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Repository and Service layers -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/jdbc
                 http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:annotation-config/>

    <context:property-placeholder
            location="classpath:calypsox/infra/hib/resources/database.properties"
            system-properties-mode="OVERRIDE"/>

    <!-- DataSource -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
          p:username="${jdbc.username}" p:password="${jdbc.password}"/>

    <jdbc:initialize-database data-source="dataSource"
                              enabled="${jdbc.init}" ignore-failures="ALL">
        <jdbc:script location="${jdbc.initLocation}"/>
    </jdbc:initialize-database>

    <!-- === Hibernate Session Factory === -->

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

        <property name="annotatedClasses">
            <list>
                <value>calypsox.infra.hib.model.MesarimDBLogRecord</value>
            </list>
        </property>

    </bean>

    <context:component-scan base-package="calypsox.infra.hib.service"/>

</beans>

,代码是:

MesarimLogDaoImpl.java

package calypsox.infra.hib.service;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.calypso.engine.risk.Log;

import calypsox.infra.hib.model.MesarimDBLogRecord;


@Repository("logDao")
public class MesarimLogDaoImpl extends HibernateDaoSupport implements MesarimLogDao {

    @Override
    public void save(MesarimDBLogRecord msg) {
        try {
            //must handle exceptions internally!
            getHibernateTemplate().save(msg);
        } catch (Exception e) {
            Log.error(Log.ERROR, "CANT LOG!!!! Failed to write DB log:", e);
        }
    }

    @Autowired
    public void anyMethodName(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

}

MesarimLogServiceImpl.java

package calypsox.infra.hib.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import calypsox.infra.hib.model.MesarimDBLogRecord;


@Service("mesarimLogService")
public class MesarimLogServiceImpl implements MesarimLogService {

    @Autowired
    MesarimLogDao logDao;

    @Override
    public void log(MesarimDBLogRecord msg) {
        logDao.save(msg);
    }

    @Override
    public void error(MesarimDBLogRecord msg) {
        if (msg.getError() == null) msg.setError("Unexpected error!");
        log(msg);
    }

}



package calypsox.infra.hib.model;

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

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class BaseLogRecord implements Serializable {

        public BaseLogRecord() {
         setTimestamp(new Date());
    }

    @Column(name = "TIME_STAMP")
        private Date timestamp;
        @Column(name = "ERROR")
    private String error;

        public Date getTimestamp() {
                return timestamp;
        }

        public void setTimestamp(Date timestamp) {
                this.timestamp = timestamp;
        }

        public String getError() {
                return error;
        }

        public void setError(String error) {
                this.error = error;
        }

}


package calypsox.infra.hib.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import ext.mesarim.out.MesarimXmlData;
import ext.mesarim.out.MesarimXmlFormatter;

/**
* POJO object - represent log record that will be stored to DB table using hibernate.
* Contain message id, meta data, error and final xml (without pdf content)
*
*/
@Entity
@Table(name = "MESARIM_LOG")
public class MesarimDBLogRecord extends BaseLogRecord {

        @Id
        @Column(name = "MESSAGE_ID")
        private int messageId;
        //Meta:
        //BRANCH_NUMBER_CUSTOM
    @Column(name = "BRANCH_NUMBER_CUSTOM")
        private String branchNumber;
        //ACCOUNT_NUMBER_CUSTOM
    @Column(name = "ACCOUNT_NUMBER_CUSTOM")
        private String accountNumber;
        //ACCOUNT_TYPE_CUSTOM
    @Column(name = "ACCOUNT_TYPE_CUSTOM")
        private String accountType;
        //LE_Cust_Type
    @Column(name = "LE_CUST_TYPE")
        private String custType;
        //LE_CUSTOMER_INDEX
    @Column(name = "LE_CUSTOMER_INDEX")
        private String custIndex;
        //LE_ROTEM_NUMBER
    @Column(name = "LE_ROTEM_NUMBER")
        private String rotemIndex;
        //SENDER_FULL_NAME
    @Column(name = "SENDER_FULL_NAME")
        private String senderName;
        //BROKER_FULL_NAME
    @Column(name = "BROKER_FULL_NAME")
        private String brokerName;
        //fax
    @Column(name = "FAX")
        private String fax;

    @Column(name = "XML")
        private String xmlStr;

    public MesarimDBLogRecord(int msgId) {
         super();
         setMessageId(msgId);
         setError("Mesarim Gateway: start parsing meta data.");
    }

    public int getMessageId() {
                return messageId;
        }

        public void setMessageId(int messageId) {
                this.messageId = messageId;
        }

        public String getBranchNumber() {
                return branchNumber;
        }

        public void setBranchNumber(String branchNumber) {
                this.branchNumber = branchNumber;
        }

        public String getAccountNumber() {
                return accountNumber;
        }

        public void setAccountNumber(String accountNumber) {
                this.accountNumber = accountNumber;
        }

        public String getAccountType() {
                return accountType;
        }

        public void setAccountType(String accountType) {
                this.accountType = accountType;
        }

        public String getCustType() {
                return custType;
        }

        public void setCustType(String custType) {
                this.custType = custType;
        }

        public String getCustIndex() {
                return custIndex;
        }

        public void setCustIndex(String custIndex) {
                this.custIndex = custIndex;
        }

        public String getRotemIndex() {
                return rotemIndex;
        }

        public void setRotemIndex(String rotemIndex) {
                this.rotemIndex = rotemIndex;
        }

        public String getSenderName() {
                return senderName;
        }

        public void setSenderName(String senderName) {
                this.senderName = senderName;
        }

        public String getBrokerName() {
                return brokerName;
        }

        public void setBrokerName(String brokerName) {
                this.brokerName = brokerName;
        }

        public String getFax() {
                return fax;
        }

        public void setFax(String fax) {
                this.fax = fax;
        }

        public String getXmlStr() {
                return xmlStr;
        }

        public void setXmlStr(String xmlStr) {
                this.xmlStr = xmlStr;
        }

        public MesarimDBLogRecord updateMetaData(MesarimXmlData xmlData) {
                if(xmlData == null) {
                        setError("Xml data is null - failed to parse meta data.");
                }
                else {
                        setBranchNumber(xmlData.getBranchNumber());
                        setAccountNumber(xmlData.getAccountNumber());
                        setAccountType(xmlData.getAccountType());
                        setCustType(xmlData.getCustType());
                        setCustIndex(xmlData.getCustomersIndex());
                        setRotemIndex(xmlData.getRotem());
                        setSenderName(xmlData.getSenderFullName());
                        setBrokerName(xmlData.getBroker());
                        setFax(xmlData.getFaxesAsString());
                        setError("Meta data was successfully parsed.");
                }
                return this;
        }

        public MesarimDBLogRecord setXmlWithoutPdfsContent(StringBuffer xml) {
                setXmlStr(MesarimXmlFormatter.removePdfContent(xml));
                setError("Mesarim xml was successfully created and sent.");
                return this;
        }

        public MesarimDBLogRecord setFailed(String e) {
                setError("ERROR! " + e);
                return this;
        }
}

客户端部分:

public class GatewayMesarimDocumentSender extends GatewayCAMELDocumentSender {

        public static Properties props;
        public static MesarimLogService dbLogger;
        static {
                try {
                        // get properties on static phase
                        props = GatewayUtil.readPropertyFile("BLLresources/gateway/mesarim_config.properties");
                        // init spring application context for logger
                        ApplicationContext appCtx = new ClassPathXmlApplicationContext("calypsox/infra/hib/resources/log-config.xml");
                        // get db logger
                        dbLogger = (MesarimLogService) appCtx.getBean(MesarimLogService.class);
                        Log.debug(Log.CALYPSOX, "GatewayMesarimDocumentSender:dbLogger" + dbLogger);
                        if (dbLogger == null)
                                Log.error(Log.ERROR, "Failed to retrieve db logger reference.");
                } catch (IOException e) {
                        Log.error(Log.ERROR, "Unexpected error reading property file:", e);
                }
        }

        private StringBuffer outputXml;

        public static boolean isTest = Boolean.valueOf(props.getProperty("TEST_MODE.IS_TESTING"));

        public GatewayMesarimDocumentSender() {
                super();
                Log.info(this, "Starting " + getClass().getName());
        }
你能帮忙吗?谢谢

1 个答案:

答案 0 :(得分:0)

应该按照名称查找bean:

 dbLogger = (MesarimLogService) appCtx.getBean("mesarimLogService");

实际上你不应该使用getBean,因为它是服务定位器模式,而不是控制反转。