图:
修改的源码片段:
org.apache.tuscany.sca.implementation.spring.runtime.context ... public SpringContextTie(SpringImplementationStub implementation, List<URL> resource, boolean annotationSupport, String versionSupported) throws Exception { ... // edit by fengjc // springContext = createApplicationContext(scaParentContext, resource); springContext = (AbstractApplicationContext) org.springframework.web.context.ContextLoader.getCtx(); // end }
或许改源码不是好的解决方式,但是项目需求就这样,没办法,暂时找不到更好的解决办法
另外,在Tuscany中取得ApplicationContext的代码:
org.springframework.web.context.ContextLoader.getCtx();
也是修改了Spring的ContextLoader.java的代码
getCtx()返回ApplicationContext的引用,罪过,这块不是我改的,我实在觉得改源码不好
补充:修改org.apache.tuscany.sca.implementation.spring.invocation.SpringInvoker
中的如下代码可以生成class文件(只适合JDK Proxy的情况),使用反编译软件查看Spring生成的代理类
// Lazy-load the method to avoid timing problems with the Spring Context private void setupMethod() throws SpringInvocationException{ try { bean = springContext.getBean(beanElement.getId()); // add by fengjc for test { byte[] proxyClassFile = sun.misc.ProxyGenerator.generateProxyClass( bean.getClass().getName(), bean.getClass().getInterfaces()); System.out.println(bean.getClass().getName()); try { FileOutputStream fos = new FileOutputStream(new File(bean.getClass().getName() + ".class")); fos.write(proxyClassFile, 0, proxyClassFile.length); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // end Class<?> beanClass = bean.getClass();
如果Spring是使用CGLIB生成的代理类,请参考CGLIB源码,net.sf.cglib.core.AbstractClassGenerator类中的如下代码:
byte[] b = strategy.generate(this);