SCA(Tuscany) implementation.spring 源码分析

本文探讨了如何在SCA(Tuscany)项目中集成Spring,并解决SCA独立容器与Spring容器间访问Beans的问题。通过修改Tuscany源码,直接访问Spring容器中的Beans,并展示了生成类文件的方法。尽管直接修改源码可能不是最佳实践,但这一解决方案满足了当前项目的特定需求。
项目要求集成SCA(Tuscany),还要结合Spring使用(把Spring容器管理的ServiceBean公开为SCA服务),问题是SCA有自己独立的容器,直接访问Spring容器内部的Beans比较困难,因为它们不在同一容器内。所以简单研究了下代码,改一下Tuscany源码,直接访问Spring容器中的Beans,这样的话 事务包装 也可以直接通过Spring解决了

图:
[img]http://dl.iteye.com/upload/attachment/0067/8979/193c7f0c-f021-3f41-9485-3936ce2a0c50.gif[/img]

修改的源码片段:

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值