在其他项目中将AspectJ项目用作jar

时间:2018-08-03 07:04:00

标签: java aop aspectj spring-aop

作为AspectJ和Spring AOP的新手,我有一个其中定义了Aspects的Spring AOP项目。切入点在注释Loggable上定义。每当执行带有@Loggable的方法时,建议都应执行。

注释:

@Target(ElementType.METHOD)
@Retention(RUNTIME)
public @interface Loggable {

    String customMessage() default "";

    String[] properties() default {};

    String type();
}

下面是Aspect类:
LoggingAspect:

@Aspect
@Component
public class LoggingAspect {

    private final Logger LOGGER = Logger.getLogger(this.getClass());

    @Around("loggablePointcut(loggableAnnotation)")
    public Object beforeDebug(ProceedingJoinPoint joinpoint, Loggable loggableAnnotation) throws Throwable {
        final String logType = loggableAnnotation.type();

        LOGGER.log(Level.toLevel(logType), joinpoint.getSignature().toString() + " - " + loggableAnnotation.customMessage());
        System.out.println("in here");

        // Default Object that we can use to return to the consumer
        Object returnObject = null;

        try {
            returnObject = joinpoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        }

        return returnObject;
    }

    @Pointcut("@annotation(loggableAnnotation)")
    public void loggablePointcut(Loggable loggableAnnotation) {
    }
}

现在使应用程序可运行并在本地进行一次测试:
Application.java:

@SpringBootApplication
public class Application {

    @Loggable(type = "info")
    public void testing() throws NoSuchMethodException, SecurityException {
        System.out.println("testing");
    }

    public static void main(String[] args) throws NoSuchMethodException, SecurityException {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        Application app = (Application) ctx.getBean("application");
        app.testing();
        System.err.println("Executed from project A");
    }
}

直到这里一切都正常。现在,我的实际要求是在我的其他Spring Boot项目中使用这些方面。我读过this article here,但似乎听不懂。

我对以下问题感到困惑:

Q1。如何将AspectJ项目导出为其他项目中的jar?我右键单击并将其导出为jar,然后导入生成的jar,一切正常吗? (我尝试过,但是没用)

Q2。我可以在这里使用其他任何选项吗?

编辑:

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>com.example</groupId>
    <artifactId>Spring_AOP</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Spring_AOP</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> </dependency> -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>fully.qualified.MainClass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

这些是我仅有的文件。

0 个答案:

没有答案