依赖注入正在Mule应用程序启动时工作.当接收到请求时,对象变为null并通过抛出NullEx失败 [英] Dependency Injection is working at Mule application startup. Objects are getting null, when a request received and Failing by throwing NullEx
问题描述
<?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());
}
}
我尝试使用自动装配以及使用弹簧塞进样器来注入对象. 在这两种方案中,我都注意到在构造函数和setter方法中添加Syso时,该对象在应用程序启动时被注入. Sysos正在控制台中打印.
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.
当我发送请求时,对象变为null并通过抛出NullPointerException失败.
甚至在Service类中也不起作用.
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.
有人可以帮我吗?
提前谢谢.
推荐答案
您介意尝试以下操作吗?
Do you mind trying the following?
将此添加到DBConfiguration.xml
:
<bean id="getEmployeeDetails"
class="com.example.hibernate.dao.GetEmployeeDetails" />
并将流程更改为:
<component>
<spring-object bean="getEmployeeDetails"/>
</component>
这篇关于依赖注入正在Mule应用程序启动时工作.当接收到请求时,对象变为null并通过抛出NullEx失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!