如何在另一个项目中导入包含 AspectJ 方面和注释的项目
How do I import a project containing AspectJ aspects and annotation in another project
我在将包含一些自定义方面的实用程序 jar 文件导入另一个项目时遇到了一些困难。应该注意的是,我没有在这个项目中使用Spring,因为我的客户有点反对Spring。
我已经创建了一个概念证明(下面是完整的代码示例)。当我 运行 在实用程序 jar 中测试 运行ner 时,任何用我的 AspectJ 注释注释的方法都可以很好地执行它们的方面。当我在另一个项目中使用同一个jar时,方面被忽略了。
当我在实用程序中 运行 主要 class 时,我得到:
$> java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar TestOne
AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.
Executing TestOne.aspectTestMethod()
AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.
如果我运行消费者class的主要class,我得到:
$>java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar;aop-consumer-1.0-SNAPSHOT.jar Test
Test.testAspectOne
AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.
Test.testAspectTwo
AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.
由于我对面向方面的编程还很陌生,我真的很感激能指出我所缺少的东西:)
实用程序罐
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationOne { }
看点
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;
@Aspect
public class AspectOne {
@Pointcut("@annotation(AnnotationOne)")
public void annotationPointCutDefinition(){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("@annotation(AnnotationOne) && execution(* *(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
try {
System.out.println("AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
}
finally {
System.out.println("AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.");
}
return returnObject;
}
@After("annotationPointCutDefinition() && atExecution()")
public void printNewLine(JoinPoint pointcut){
System.out.print("\n\r");
}
}
主要class
public class TestOne {
public static void main(String[] args) {
TestOne testOne = new TestOne();
testOne.aspectTestMethod();
}
@AnnotationOne
public void aspectTestMethod(){
System.out.println("Executing TestOne.aspectTestMethod()");
}
}
AOP 消费者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sandbox.aop</groupId>
<artifactId>aop-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationTwo {}
看点
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectTwo {
@Pointcut("@annotation(AnnotationTwo)")
public void annotationPointCutDefinition(){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("@annotation(AnnotationTwo) && execution(* *(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
try {
System.out.println("AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
}
finally {
System.out.println("AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.");
}
return returnObject;
}
@After("annotationPointCutDefinition() && atExecution()")
public void printNewLine(JoinPoint pointcut){
System.out.print("\n\r");
}
}
主要class
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.testAspectOne();
test.testAspectTwo();
}
@AnnotationOne
public void testAspectOne() {
System.out.println(Test.class.getName() + ".testAspectOne");
}
@AnnotationTwo
public void testAspectTwo() {
System.out.println(Test.class.getName() + ".testAspectTwo");
}
}
您需要告诉 aspectj 编织器使用 aspectLibraries 编织在您的库中定义的方面:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<aspectLibraries>
<aspectLibrary>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
我在将包含一些自定义方面的实用程序 jar 文件导入另一个项目时遇到了一些困难。应该注意的是,我没有在这个项目中使用Spring,因为我的客户有点反对Spring。
我已经创建了一个概念证明(下面是完整的代码示例)。当我 运行 在实用程序 jar 中测试 运行ner 时,任何用我的 AspectJ 注释注释的方法都可以很好地执行它们的方面。当我在另一个项目中使用同一个jar时,方面被忽略了。
当我在实用程序中 运行 主要 class 时,我得到:
$> java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar TestOne
AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.
Executing TestOne.aspectTestMethod()
AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.
如果我运行消费者class的主要class,我得到:
$>java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar;aop-consumer-1.0-SNAPSHOT.jar Test
Test.testAspectOne
AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.
Test.testAspectTwo
AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.
由于我对面向方面的编程还很陌生,我真的很感激能指出我所缺少的东西:)
实用程序罐
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationOne { }
看点
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.JoinPoint;
@Aspect
public class AspectOne {
@Pointcut("@annotation(AnnotationOne)")
public void annotationPointCutDefinition(){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("@annotation(AnnotationOne) && execution(* *(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
try {
System.out.println("AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
}
finally {
System.out.println("AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.");
}
return returnObject;
}
@After("annotationPointCutDefinition() && atExecution()")
public void printNewLine(JoinPoint pointcut){
System.out.print("\n\r");
}
}
主要class
public class TestOne {
public static void main(String[] args) {
TestOne testOne = new TestOne();
testOne.aspectTestMethod();
}
@AnnotationOne
public void aspectTestMethod(){
System.out.println("Executing TestOne.aspectTestMethod()");
}
}
AOP 消费者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sandbox.aop</groupId>
<artifactId>aop-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnotationTwo {}
看点
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectTwo {
@Pointcut("@annotation(AnnotationTwo)")
public void annotationPointCutDefinition(){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("@annotation(AnnotationTwo) && execution(* *(..))")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
try {
System.out.println("AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
}
finally {
System.out.println("AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.");
}
return returnObject;
}
@After("annotationPointCutDefinition() && atExecution()")
public void printNewLine(JoinPoint pointcut){
System.out.print("\n\r");
}
}
主要class
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.testAspectOne();
test.testAspectTwo();
}
@AnnotationOne
public void testAspectOne() {
System.out.println(Test.class.getName() + ".testAspectOne");
}
@AnnotationTwo
public void testAspectTwo() {
System.out.println(Test.class.getName() + ".testAspectTwo");
}
}
您需要告诉 aspectj 编织器使用 aspectLibraries 编织在您的库中定义的方面:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<aspectLibraries>
<aspectLibrary>
<groupId>sandbox.aop</groupId>
<artifactId>aop-util</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>