0

I am working on compile team weaving using AspectJ as Load time Weaving for the same is causing extra overhead on server startup.so the issue is at compile all the classes is being weaved. However when running application on server it is never coming to any of the Aspect class.

So as I have some classes that are using lombok so I have done like this and added compile time maven plugin

<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.x.rgx</groupId>
    <artifactId>web</artifactId>
    <version>10.0</version>
    <packaging>war</packaging>

    <properties>
        <runSuite>**/AllTests.class</runSuite>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring-framework.version>5.0.4.RELEASE</spring-framework.version>
        <lombok.version>1.18.2</lombok.version>
        <aspectj.version>1.8.13</aspectj.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

   <dependencies>
       <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <compilerArguments>
                                <d>${project.build.directory}/classes</d>
                            </compilerArguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <complianceLevel>${maven.compiler.target}</complianceLevel>
                    <source>${maven.compiler.target}</source>
                    <target>${maven.compiler.target}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources />
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                    </weaveDirectories>

                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>

                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <includes>
                        <include>${runSuite}</include>
                    </includes>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

package com.x.aspect.config;

@Configuration
@ComponentScan(basePackages = { "com.x" })
public class AspectConfig {
}
package com.x.login;

@Component
@Scope("session")
public class LoginMBean extends AbstractMbean {
    @Autowired
    LoginService loginService ;

    public void loginUserData(){
    LoginInfo info= new LoginInfo();
    //setter for info object
    //some nested method calls
    loginService.insertLoginData(info);

   }
}
package com.x.aspects; 

@Component 
@Aspect
public class Aspects {
    private static Logger Logger= LoggerFactory.getLogger(Aspects.class);

    @Pointcut("execution(* *(..)) && cflow(execution(* com.x.login..*(..)))")
    public void methodsToBeProfiled() {}

    @Around("methodsToBeProfiled()")
    public Object methodsToBeProfiled(ProceedingJoinPoint point) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getSimpleName());
        try {
            sw.start(point.getSignature().getName());
            return point.proceed();
        } finally {
            sw.stop();
            Logger.info("Elapsed Time, Package Name, Method Name");
            Logger.info(sw.prettyPrint());
            Logger.info("Package Name: " + point.getStaticPart());
        }
    }
}

[INFO] Join point 'method-execution(java.lang.String com.x.login.LoginMBean.getArisgPersistenceUnitName(java.lang.String))' in Type 'com.x.login.LoginMBean' (LoginMBean.java:258) advised by around advice from 'com.x.aspects.Aspects' (Aspects.class(from Aspects.java)) [with runtime test]
[INFO] Join point 'method-execution(java.lang.String com.x.login.LoginMBean.getMultiDb())' in Type 'com.x.login.LoginMBean' (LoginMBean.java:269) advised by around advice from 'com.x.aspects.Aspects' (Aspects.class(from Aspects.java)) [with runtime test]
[INFO] Join point 'method-execution(void com.x.login.LoginMBean.setMultiDb(java.lang.String))' in Type 'com.x.login.LoginMBean' (LoginMBean.java:273) advised by around advice from 'com.x.aspects.Aspects' (Aspects.class(from Aspects.java)) [with runtime test]
[INFO] Join point 'method-execution(boolean com.x.login.LoginMBean.isDbListStatus())' in Type 'com.x.login.LoginMBean' (LoginMBean.java:277) advised by around advice from 'com.x.aspects.Aspects' (Aspects.class(from Aspects.java)) [with runtime test]

So now as in the compile time it has weaved all the classes. But at the runtime it not coming to Aspects.java. Anything else i need to add up for configuration.? Do i need configuration added in spring-config.xml?

kriegaex
  • 63,017
  • 15
  • 111
  • 202

1 Answers1

0
  • List item

It has worked by changing Pointcut to:

@Pointcut("execution(* *(..)) && cflow(execution(* com.x.login.LoginMBean.*(..)))")
  • Either you pasted the wrong pointcut and forgot a `cflow()` somewhere or your pointcut is redundant and should just be `execution(* com.x.login.LoginMBean.*(..))`. Please fix your answer, otherwise it will not help others. – kriegaex Aug 15 '19 at 02:42
  • cflow() has to be added in it. – shubhra thakur Aug 19 '19 at 12:22
  • I am getting one more issue, I want to exclude some classes from being weaved ,exclude tag is there but how to use it here ? – shubhra thakur Aug 19 '19 at 12:29
  • What do you mean by "exclude tag" and "there" is where? Please elaborate and be more precise, please. Ideally, edit your question or create a new one, as your own answer obviously does not really solve your problem. – kriegaex Aug 19 '19 at 15:02