dubbo获取ApplicationContext

dubbo自定义的扩展中如何获取当前的ApplicationContext,并且获取对应的类和接口以及方法。


1.自定义扩展步骤

@在xml中,provider标签中加入filter="TEST";

@同时文件目录如下配置:

src
  |-main
     |-java
         |-com
             |-xxx
                 |-Test.java (实现Validation接口)
     |-resources
         |-META-INF
             |-dubbo
                 |-com.xxx.Test (纯文本文件,内容为:xxx=com.xxx.Test)

2.具体的实现

import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.sohu.service.TmallOrderService;

@Activate("TEST")
public class TestFilter implements Filter{
	
	private Logger logger =LoggerFactory.getLogger(TestFilter.class);

	
	@Override
	public Result invoke(Invoker invoker, Invocation invocation)
			throws RpcException {
		logger.info("test filter");
		Class serviceType = invoker.getInterface();
		
		Method method =null;
        try {
        	method= invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        ApplicationContext context=ServiceBean.getSpringContext();
        TmallOrderService toc=context.getBean(TmallOrderService.class);
		logger.info(serviceType.getName());
		logger.info(method.getName());
        logger.info(toc.toString());
		return invoker.invoke(invocation);
	}

}

3.如何获取context呢

有个ServiceBean可以直接获取当前环境的springContext.


你可能感兴趣的:(dubbo)