Dependency Injection is working at Mule application startup. Objects are getting null, when a request received and Failing by throwing NullEx

StackOverflow https://stackoverflow.com/questions/23684519

Question

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

And here is 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>

Here is my Java Component

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());
    }

}

I have tried to inject object using autowire and also by using spring setter injection. In both the scenarios by adding Syso in constructor and setter method I have noticed, that the object is getting injected at the time of application start up. Sysos are getting printed in the console.

When I send a request, object is getting null and failing by throwing NullPointerException.
Even the same is not working in Service classes as well.

Can any one help me out?

Thanks in advance.

Était-ce utile?

La solution

Do you mind trying the following?

Add this to DBConfiguration.xml:

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

And change the flow to:

<component>
  <spring-object bean="getEmployeeDetails"/>
</component>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top