使用spring

时间:2015-11-17 11:18:29

标签: spring wso2esb

在wso2carbob ESB 4.8.0

上设置弹簧时遇到问题

这是我运行的代码

public MyMediator(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);

org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) msgCntx)
                .getAxis2MessageContext();
        @SuppressWarnings("unchecked")
        Map<String, String> headersMap = (Map<String, String>) msgContext
                .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
...
}
@Autowired 
private MyObject myObject;

这是错误

java.lang.NoClassDefFoundError: org/apache/synapse/core/axis2/Axis2MessageContext
    at eu.dedalus.x1v1.policymanager.pep.mediator.PepMediator.mediate(PepMediator.java:57)
    at org.apache.synapse.mediators.spring.SpringMediator.mediate(SpringMediator.java:94)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
    at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
    at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
    at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:166)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
    at org.apache.synapse.transport.nhttp.ServerWorker.processEntityEnclosingMethod(ServerWorker.java:459)
    at org.apache.synapse.transport.nhttp.ServerWorker.run(ServerWorker.java:279)
    at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.synapse.core.axis2.Axis2MessageContext
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

如果我注释掉这一行:

ApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);

一切运行良好,并且 org.apache.synapse.core.axis2.Axis2MessageContext 类已正确加载。

我将jar放在repository / components / lib下,并在wso2存储库中使用了ESB附带的默认spring版本,这是maven依赖项:

<dependency>
        <groupId>org.springframework.ws.wso2</groupId>
        <artifactId>spring.framework</artifactId>
        <version>3.1.0.wso2v1</version>
        <scope>provided</scope>
</dependency>

在这种情况下,我的目标是获取邮件的http标头。 但在更通用的情况下,使用带有弹簧注释的插件下的库。 我怎样才能查看该课程?

2 个答案:

答案 0 :(得分:0)

要使用注释基础弹簧配置,您需要有spring aop.jar文件。但是wso2 esb spring mediator不支持注释基本配置。你需要使用基于xml的配置。

答案 1 :(得分:0)

实际上可能,但不是在类构造函数中,我认为这是因为 osgi框架类加载结构

Wso2实例化你声明的一堆所有中介,但我认为它是在系统init 的一个阶段中实现的,所以并非所有类都已加载,或者可能在初始化时它使用不同的类加载器......这些只是假设。

但是,您可以在 mediate 方法中创建应用程序上下文,如下例所示:

 public MyObject myObject;

 public boolean mediate(MessageContext mc) {
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);
    myObject = context.getBean(MyObject.class);


}

这并不是真正有效,因为 mediate 方法总是在运行时调用,所以我真的不干净的解决方案是将 MyObject 声明为 static < / strong>像这样:

 public static MyObject myObject;

 public boolean mediate(MessageContext mc) {
    if(myObject == null){
       ApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);
       myObject = context.getBean(MyObject.class);
    }


}

所以现在我有 myObject 单身,这正是我想要的和春天的作用