依赖注入正在Mule应用程序启动时工作。当接收到请求时,对象变为null并通过抛出NullEx失败

G楼
 <?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失败。
甚至在Service类中也不起作用。

谁能帮我吗?

提前致谢。

戴维·多索特(David Dossot)

您介意尝试以下吗?

将此添加到DBConfiguration.xml

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

并将流更改为:

<component>
  <spring-object bean="getEmployeeDetails"/>
</component>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

应用程序启动时 DbContext 依赖注入与 IHostedService 冲突

如何在启动时使应用程序失败

从finder启动时Python构建应用程序失败

启动时启动应用程序

启动时启动应用程序

是否可以在应用程序启动时不使用服务定位器来实现依赖项注入?

使用Play Framework和Guice进行依赖注入会在应用程序启动时导致NullPointerException

添加依赖项时,SpringBoot应用程序启动失败

从angular 4应用程序接收时表示接收到空的请求正文

如何解决在应用程序启动时由SAP云SDK抛出DestinationAccessException

C# 应用程序在 InitializeComponent() 处启动时崩溃,抛出 CultureNotFoundException

当消费者在 spring-cloud-stream 应用程序中启动时接收消息

在启动时赢得应用程序会杀死其他正在运行的应用程序

在启动器中通过按图标启动时,应用程序完全重启

当应用程序通过启动应用程序启动时没有按钮点击声音

自定义请求处理程序在启动时失败

Anypoint Studio 7.7.0失败,并在启动时出现致命的Windows应用程序错误

Qt应用程序断言在第二次启动时失败

设备启动时如何启动应用程序

在Windows启动时启动WPF应用程序

尝试在启动时以全屏模式启动应用程序

启动时启动Java应用程序

iPhone应用程序可以在启动时启动吗?

bashrc 别名在终端启动时启动应用程序

在启动时启动X应用程序

在启动时自动启动后台应用程序

应用程序在启动时显示白屏,然后启动

如何防止Spring Boot Web应用程序仍在启动时接受HTTP请求?

如何在应用程序启动时获取请求URL