Maven + AspectJ - all steps to configure it

前端 未结 5 414
迷失自我
迷失自我 2020-12-07 16:52

I have a problem with applying aspects to my maven project. Probably I am missing something, so I\'ve made a list of steps. Could you please check if it is correct?

<
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 17:25

    I traced some of your older questions to try to find out what you actually are trying to do. I have come up with the following structure and code and for me it works well.

    $ tree .
    .
    ├── pom.xml
    ├── ProjectA
    |   ├── pom.xml
    |   └── src
    |       └── main
    |           └── aspect
    |               └── com
    |                   └── stackoverflow
    |                       └── aspects
    |                           ├── AspectL.java
    |                           └── Trace.aj
    └── ProjectB
        ├── pom.xml
        └── src
            └── main
                └── java
                    └── com
                        └── stackoverflow
                            └── App.java
    

    pom.xml

    
        4.0.0
    
        org.stackoverflow
        Q12423965
        pom
        1.0-SNAPSHOT
    
        ${project.artifactId}-${project.version}
    
        
            UTF-8
            1.6
            1.6
        
    
        
            ProjectA
            ProjectB
        
    
        
            
                
                    org.stackoverflow
                    Q12423965-ProjectA
                    ${project.version}
                
            
        
    
        
            
                
                    
                        org.codehaus.mojo
                        aspectj-maven-plugin
                        1.4
                        
                            
                                
                                    compile
                                    test-compile
                                
                            
                        
                        
                            ${maven.compiler.source}
                            ${maven.compiler.target}
                        
                    
                    
                        org.apache.maven.plugins
                        maven-compiler-plugin
                        2.5.1
                        
                            ${maven.compiler.source}
                            ${maven.compiler.target}
                        
                    
                
            
        
    
    

    ProjectA/pom.xml

    
        4.0.0
    
        
            org.stackoverflow
            Q12423965
            1.0-SNAPSHOT
        
    
        Q12423965-ProjectA
    
        ${project.artifactId}-${project.version}
    
        
            
                org.aspectj
                aspectjrt
                1.6.11
            
        
    
        
            
                
                    org.codehaus.mojo
                    aspectj-maven-plugin
                
            
        
    
    

    I have created two differenct aspects. One that uses @AspectJ annotations and another one that is defined as a classic AspectJ aspect.

    AspectL.java

    package com.stackoverflow.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;
    
    /**
     * @author maba, 2012-09-18
     */
    @Aspect
    public class AspectL {
    
        @Pointcut("execution(* main(..))")
        public void defineEntryPoint() {
        }
    
        @Before("defineEntryPoint()")
        public void aaa(JoinPoint joinPoint) {
            System.out.println("aspect before");
        }
    
        @After("defineEntryPoint()")
        public void bbb(JoinPoint joinPoint) {
            System.out.println("aspect after");
        }
    }
    

    Trace.aj

    package com.stackoverflow.aspects;
    
    public aspect Trace {
        pointcut publicMethodExecuted(): execution(public !static * *(..));
    
        after(): publicMethodExecuted() {
            System.out.printf("Enters on method: %s. \n", thisJoinPoint.getSignature());
    
            Object[] arguments = thisJoinPoint.getArgs();
            for (int i =0; i < arguments.length; i++){
                Object argument = arguments[i];
                if (argument != null){
                    System.out.printf("With argument of type %s and value %s. \n", argument.getClass().toString(), argument);
                }
            }
            System.out.printf("Exits method: %s. \n", thisJoinPoint.getSignature());
        }
    }
    

    Those two files are part of the ProjectA module and are part of a jar.

    Now we want to use these aspects and weave them into the code of ProjectB.

    ProjectB/pom.xml

    
        4.0.0
    
        
            org.stackoverflow
            Q12423965
            1.0-SNAPSHOT
        
    
        Q12423965-ProjectB
    
        ${project.artifactId}-${project.version}
    
        
            
                org.aspectj
                aspectjrt
                1.6.11
            
            
                org.stackoverflow
                Q12423965-ProjectA
            
        
    
        
            
                
                    org.codehaus.mojo
                    aspectj-maven-plugin
                    
                        
                            
                                org.stackoverflow
                                Q12423965-ProjectA
                            
                        
                    
                
                
                    org.codehaus.mojo
                    exec-maven-plugin
                    1.2.1
                    
                        
                            
                                java
                            
                        
                    
                    
                        com.stackoverflow.App
                    
                
            
        
    
    

    App.java

    package com.stackoverflow;
    
    /**
     * @author maba, 2012-09-17
     */
    public class App {
    
        public void hello(String name) {
        }
    
        public static void main(String[] args) {
            App app = new App();
            app.hello("world");
        }
    }
    

    I build everything from top pom:

    mvn clean install
    

    And then go into the ProjectB directory and run the app:

    mvn exec:java
    

    The result is:

    aspect before
    Enters on method: void com.stackoverflow.App.hello(String). 
    With argument of type class java.lang.String and value world. 
    Exits method: void com.stackoverflow.App.hello(String). 
    aspect after
    

    So to conclude both aspects are working and the maven setup also works.

提交回复
热议问题