无法在Mule组件中自动装配对象

时间:2015-04-30 09:57:04

标签: eclipse spring spring-mvc mule mule-component

我正在尝试在mule流中自动装配服务类的对象。代码是:

public class SignatureValidator implements Callable 
{
@Autowired
private TriggerHostServiceImpl triggerHostServiceImpl;

@Override
public Object onCall(MuleEventContext eventContext) throws Exception 
{
    MuleMessage message = eventContext.getMessage();

    message = fetchPropertiesAndValidateMessageSignature(message);

    return message.getPayload();
}

private MuleMessage fetchPropertiesAndValidateMessageSignature(MuleMessage message) throws GeneralSecurityException, IOException 
{
    String muleWSTriggerLabel = message.getInboundProperty("triggerLabel");
    String muleWSSignature    = message.getInboundProperty("signature");
    String muleWSExpiresOn    = message.getInboundProperty("expiresOn");
    String xmlData            = message.getInboundProperty("xmlData");
    String appHostName        = InitConfigurationLoader.getConfigSetting("applicationHostingName");

    Trigger triggerJaxbObject = (Trigger) message.getPayload();
    String applicationIdentifier = triggerJaxbObject.getApplicationIdentifier();

    TriggerMapper triggerMapper  = FetchConfigurationEntities.getTriggerMapper(applicationIdentifier, muleWSTriggerLabel);
    String reportEmail           = FetchConfigurationEntities.getReportEmail(triggerMapper);
    ImportDetails importInstance = FetchConfigurationEntities.getImport(triggerMapper);

    String importInstanceURL = importInstance.getWebserviceURL();

    message.setInvocationProperty("triggerJaxbObject", triggerJaxbObject);
    message.setInvocationProperty("importInstance", importInstance);
    message.setInvocationProperty("reportEmail", reportEmail);
    message.setInvocationProperty("appIdentifier", applicationIdentifier);
    message.setInvocationProperty("importHost", importInstanceURL.substring(importInstanceURL.lastIndexOf('/')+1, importInstanceURL.length()));

    setPayloadAfterValidation(message, muleWSTriggerLabel, xmlData, muleWSSignature, appHostName, muleWSExpiresOn);

    return message;
}

我的服务类是:

package com.catalystone.csi.service;

import java.util.Map;
import java.util.Map.Entry;

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

import com.catalystone.csi.core.cache.UpdateCacheable;
import com.catalystone.csi.dao.TriggerHostDao;
import com.catalystone.csi.model.TriggerHost;

@Service
public class TriggerHostServiceImpl implements TriggerHostService 
{
@Autowired
private TriggerHostDao triggerHostDao;

@Autowired
private UpdateCacheable updateCacheable;

/**
 * Method to save mule configurations i.e. TriggerHosts
 */
@Override
@Transactional
public boolean saveTriggerHost(TriggerHost triggerHost)
{
    if(triggerHostDao.saveTriggerHost(triggerHost))
    {
        Map<String, TriggerHost> allTriggerHosts = getAllTriggerHosts();
        allTriggerHosts.put(triggerHost.getTriggerIdentifier(), triggerHost);

        updateCacheable.updateAllTriggerHostCache(allTriggerHosts);

        return true;
    }
    else
        return false;
}

/**
 * Method to fetch all the configurations
 */
@Override
@Transactional//this annotation is used to maintain transaction
public Map<String, TriggerHost> getAllTriggerHosts() 
{
    return triggerHostDao.getAllTriggerHosts();
}


/**
 * Method to delete mule configuration for triggerHost
 * @return - true if delete configuration is successfully done
 */
@Override
@Transactional//this annotation is used to maintain transaction
public Boolean deleteConfiguration(TriggerHost triggerHost, boolean isMultipleImportOccurrence) 
{
    Boolean isDeleteSuccessful = triggerHostDao.deleteConfiguration(triggerHost, isMultipleImportOccurrence);

    //Getting all the configurations from cache
    Map<String, TriggerHost> allTriggerHosts = getAllTriggerHosts();

    //check if delete configuration successful then remove that configuration from cache
    if(isDeleteSuccessful)
    {
        for(Entry<String, TriggerHost> triggerHostEntry : allTriggerHosts.entrySet())
        {
            if(triggerHostEntry.getValue().getTriggerIdentifier().equals(triggerHost.getTriggerIdentifier()))
            {
                allTriggerHosts.remove(triggerHostEntry.getKey());
                break;
            }
        }
        //update cache 
        updateCacheable.updateAllTriggerHostCache(allTriggerHosts);
        return true;
    }

    return false;
}

@Override
@Transactional
public Boolean updateConfiguration(TriggerHost triggerHost)
{
    if(triggerHostDao.updateConfiguration(triggerHost))
    {
        Map<String, TriggerHost> allTriggerHosts = getAllTriggerHosts();
        allTriggerHosts.put(triggerHost.getTriggerIdentifier(), triggerHost);

        updateCacheable.updateAllTriggerHostCache(allTriggerHosts);

        return true;
    }

    return false;
}

@Override
@Transactional
public Boolean deleteConfiguration(String existingImportIdentifier)
{
    return triggerHostDao.deleteConfiguration(existingImportIdentifier);
}

}

当我运行此代码时,triggerHostServiceImpl的值始终为null。如何自动装配?我还尝试了一个链接Dependency Injection is working at Mule application startup. Objects are getting null, when a request received and Failing by throwing NullExSpring3 Dependency Injection not working with mule 但是它给了我很多例外,我无法得到。

1 个答案:

答案 0 :(得分:1)

你必须自动连接接口而不是实现

@Autowired
private TriggerHostService triggerHostService;

并添加triggerHostService

的setter和getter