<dependencies> <!-- AspectJ dependencies --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.7</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> </dependencies> import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.After; @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } Bundle-Activator: com.example.Activator Export-Package: com.example.service Import-Package: org.aspectj.lang org.aspectj.lang.annotation import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.aspectj.weaver.loadtime.definition.Definition; public class Activator implements BundleActivator { public void start(BundleContext bundleContext) throws Exception { Definition aspectjDefinition = new Definition(getClass().getResourceAsStream("/path/to/aspectj.xml")); AspectJWeaverService.start(aspectjDefinition); } public void stop(BundleContext bundleContext) throws Exception { AspectJWeaverService.stop(); } }


上一篇:
下一篇:
切换中文