osgi(equinox) aspect 应用

喜欢 OSGI 可以做到很好的模块化  自由加模块拓展业务逻辑  至于热插拔水份太重 如果是干净的没有export包的工程可热插

 

也喜欢用spring 的 aop...不在动原有代码的基础上拓展功能

 

首先普及spring 切面知识 下面这片贴子介绍得很到位

http://www.iteye.com/topic/487828

 

 

下面是osgi(equinox)整合aspect的记录

 

首先看官方的说明:

http://www.eclipse.org/equinox/incubator/aspects/index.php   首页

http://www.eclipse.org/equinox/incubator/aspects/equinox-aspects-quick-start.php  例子

 

说明:官方这demo让我有点郁闷 。怎么跑就是跑不成功  照着官方的思路尝试

 

1.下载equinox-SDK-3.6.2.zip   虽然只用到几个包    怕包不兼容  虽然用了equinox时间不短还是觉得它比较娇气

 

2.找到 aspectjrt.jar,aspectjweaver.jar(本人用的是官方demo里边的D:\aspect\org.eclipse.equinox.weaving.demo.target\bundles-1.0.0-3.4.0-1.6.1\org.aspectj.runtime_1.6.1.20080703120000aspectjrt.jar,D:\aspect\org.eclipse.equinox.weaving.demo.target\bundles-1.0.0-3.4.0-1.6.1\org.aspectj.weaver_1.6.1.20080703120000\aspectjweaver.jar)

这里要注意 equinox-SDK-3.6.2.zip 中 需要的aspectj.weaver版本是1.6.3 (如果版本不对会有java.lang.ClassNotFoundException: org.aspectj.weaver.loadtime.definition.Definition 异常

 查看需要的版本

 

osgi> diag 5
initial@reference:file:org.eclipse.equinox.weaving.aspectj_1.0.0.v20100503.jar/ [5]
  Direct constraints which are unresolved:
    Missing optionally imported package org.aspectj.weaver_1.6.3.
    Missing optionally imported package org.aspectj.weaver.loadtime_1.6.3.
    Missing optionally imported package org.aspectj.weaver.loadtime.definition_1.6.3.
    Missing optionally imported package org.aspectj.weaver.tools_1.6.3.
)

 

3.普通的插件工程cn.tsoft.demo.hello  只有一个Activator   start方法里边System.out.println("Hello world!");

 

4.切面工程 cn.tsoft.demo.hello.aspects 也是普通插件工程

4.1 Aspect  (官方demo 中的 HelloAspect.aj 没看明白)

 

package cn.tsoft.demo.hello.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class DemoAspect{
//	@Pointcut("execution(* cn.tsoft.service.ServiceA.*(..))")  
	@Pointcut("execution(* *.*(..))")//什么方法都会切入
	public void log(){
	}
	
	@After("log()")  
    public void logAfter(JoinPoint joinPoint) {  
        System.out.println("执行后!");  
    }  
	
    @Before("log()")  
    public void logBefore(JoinPoint joinPoint) {  
        System.out.println("执行前!"); 
    }  
}
 

4.2  在切面工程的META-INF下建 aop.xml 文件

 

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <aspects>
        <aspect
            name="cn.tsoft.demo.hello.aspects.DemoAspect" />
    </aspects>
</aspectj>

 

4.3 改切面工程mainfest.mf (官方都有说明)

加 Require-Bundle: org.aspectj.runtime;bundle-version="1.6.1";visibility:=reexport

加 Eclipse-SupplementBundle: cn.tsoft.demo.hello

 

5.配置启动参数

-Declipse.ignoreApp=true -Dosgi.noShutdown=true -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook -Daj.weaving.verbose=true -Dorg.aspectj.weaver.showWeaveInfo=true -Dorg.aspectj.osgi.verbose=true

(osgi.framework.extensions=org.eclipse.equinox.weaving.hook 拓展,-Dorg.aspectj.weaver.showWeaveInfo=true 详细日志信息)

 

6.勾选必要的插件

cn.tsoft.demo.hello

cn.tsoft.demo.hello.aspects

org.aspectj.runtime

org.aspectj.weaver

org.eclipse.equinox.weaving.hook_1.0.0.v20100503.jar

org.eclipse.equinox.weaving.caching_1.0.0.v20100503.jar

org.eclipse.equinox.weaving.aspectj_1.0.0.v20100503.jar

 

将org.eclipse.equinox.weaving.aspectj_1.0.0.v20100503.jar启动顺序设为最先启动 设置为1

官方有说明

The AspectJ weaving service (bundle org.eclipse.equinox.weaving.aspectj ) must be started before any classes are loaded from any bundles targeted for weaving

 

7.一切ok 执行得到结果

 

 

[org.eclipse.equinox.weaving.hook] info adding AspectJ hooks ...

osgi> [org.eclipse.equinox.weaving.aspectj] info Starting AspectJ weaving service ...
[org.eclipse.equinox.weaving.caching] info starting standard caching service ...
[cn.tsoft.demo.hello.aspects] info AspectJ Weaver Version 1.6.1rc1 built on Thursday Jul 3, 2008 at 20:33:51 GMT
[cn.tsoft.demo.hello.aspects] info register aspect cn.tsoft.demo.hello.aspects.DemoAspect
[org.eclipse.equinox.weaving.aspectj] info weaving bundle 'cn.tsoft.demo.hello.aspects'
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(org.osgi.framework.BundleContext cn.tsoft.demo.hello.aspects.Activator.getContext())' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:11) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(org.osgi.framework.BundleContext cn.tsoft.demo.hello.aspects.Activator.getContext())' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:11) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.Activator.start(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:19) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.Activator.start(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:19) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.Activator.stop(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:27) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.Activator.stop(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.aspects.Activator' (Activator.java:27) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.DemoAspect.log())' in Type 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java:14) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello.aspects] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.aspects.DemoAspect.log())' in Type 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java:14) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
执行前!
执行后!
[cn.tsoft.demo.hello] info AspectJ Weaver Version 1.6.1rc1 built on Thursday Jul 3, 2008 at 20:33:51 GMT
[cn.tsoft.demo.hello] info register aspect cn.tsoft.demo.hello.aspects.DemoAspect
[org.eclipse.equinox.weaving.aspectj] info weaving bundle 'cn.tsoft.demo.hello'
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(org.osgi.framework.BundleContext cn.tsoft.demo.hello.Activator.getContext())' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:11) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(org.osgi.framework.BundleContext cn.tsoft.demo.hello.Activator.getContext())' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:11) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.Activator.start(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:19) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.Activator.start(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:19) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.Activator.stop(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:28) advised by after advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
[cn.tsoft.demo.hello] weaveinfo Join point 'method-execution(void cn.tsoft.demo.hello.Activator.stop(org.osgi.framework.BundleContext))' in Type 'cn.tsoft.demo.hello.Activator' (Activator.java:28) advised by before advice from 'cn.tsoft.demo.hello.aspects.DemoAspect' (DemoAspect.java)
执行前!
Hello world!
执行后!
 

 

你可能感兴趣的:(java,eclipse,spring,AOP,osgi)