0

我正在使用 AspectJ 作为加载时间编织来编译团队编织,因为同样的编织会导致服务器启动时产生额外的开销。所以问题在于编译时正在编织所有类。但是,当在服务器上运行应用程序时,它永远不会进入任何 Aspect 类。

因此,由于我有一些使用 lombok 的类,所以我这样做并添加了编译时 maven 插件

<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]

所以现在就像在编译时一样,它已经编织了所有的类。但是在运行时它不会出现在 Aspects.java 中。还有什么我需要添加配置的。?我需要在 spring-config.xml 中添加配置吗?

4

1 回答 1

0
  • 项目清单

它通过将 Pointcut 更改为:

@Pointcut("execution(* *(..)) && cflow(execution(* com.x.login.LoginMBean.*(..)))")
于 2019-08-14T11:23:52.627 回答