0
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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-2.5.xsd">
<context:component-scan base-package="com.example.hibernate" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/emp" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire = "byname">
        <property name="dataSource" ref="myDataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.connection.pool_size">20</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>Departments.hbm.xml</value>
            </list>
        </property>
    </bean>
    <bean id ="employeeDAO" class = "com.example.hibernate.dao.EmployeeDAOImpl" autowire = "byname"/>
</beans>

这是Mflow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    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-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<spring:beans>
        <spring:import resource="DBConfigurations.xml" />
    </spring:beans>

    <data-mapper:config name="getEmployeeDetailsReq"
        transformationGraphPath="getEmployeeDetailsReq.grf" doc:name="getEmployeeDetailsReq" />
    <data-mapper:config name="getEmployeeDetailsResponse" transformationGraphPath="getEmployeeDetailsResponse.grf" doc:name="getEmployeeDetailsResponse"/>

    <flow name="EmployeeDetailsFlow" doc:name="EmployeeDetailsFlow">
        <http:inbound-endpoint exchange-pattern="request-response"
            address="http://localhost:9090/getEmployeeDetails" doc:name="Receive" />
        <object-to-string-transformer doc:name="Object to String" />
        <data-mapper:transform config-ref="getEmployeeDetailsReq"
            doc:name="getEmployeeDetailsReq" />
        <component doc:name="GetTemplateInfo"
            class="com.example.hibernate.dao.GetEmployeeDetails" />
        <data-mapper:transform config-ref="getEmployeeDetailsResponse" returnClass="java.lang.String" doc:name="getEmployeeDetailsResponse"/>
        <json:object-to-json-transformer
            doc:name="Object to JSON" />
    </flow>

</mule>

这是我的 Java 组件

package com.example.hibernate.dao;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.example.hibernate.Employee;

public class GetEmployeeDetails  implements Callable{

    @Autowired
    @Qualifier("employeeDAO")
    private EmployeeDAO dao;
    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        Employee emp = (Employee)eventContext.getMessage().getPayload();
        return dao.getEmployeeDetailsById(emp.getId());
    }

}

我曾尝试使用 autowire 以及使用 spring setter 注入来注入对象。在这两种情况下,通过在构造函数和 setter 方法中添加 Syso,我注意到,对象在应用程序启动时被注入。Sysos 正在控制台中打印。

当我发送请求时,对象变为 null 并通过抛出 NullPointerException 失败。
甚至在服务类中也不起作用。

谁能帮我吗?

提前致谢。

4

1 回答 1

0

您介意尝试以下方法吗?

将此添加到DBConfiguration.xml

<bean id="getEmployeeDetails"
      class="com.example.hibernate.dao.GetEmployeeDetails" />

并将流程更改为:

<component>
  <spring-object bean="getEmployeeDetails"/>
</component>
于 2014-05-19T15:47:24.457 回答