- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 Aspectj 执行 AOP,但我不知道为什么不执行我的方面,它只运行主类。这是我第一次这样做,所以我可能做错了什么。
这是我的代码:
看点:
@Aspect
public class YourAspect {
@Pointcut("@annotation(yourAnnotationVariableName)")
public void annotationPointCutDefinition(YourAnnotation yourAnnotationVariableName){
}
@Pointcut("execution(* *(..))")
public void atExecution(){}
@Around("annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint, YourAnnotation yourAnnotationVariableName) throws Throwable {
if(yourAnnotationVariableName.isRun()) {
Object returnObject = null;
try {
System.out.println("aspects.YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.");
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
throw throwable;
} finally {
System.out.println("aspects.YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.");
}
return returnObject;
}
return joinPoint.proceed();
}
@After("annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()")
public void printNewLine(JoinPoint pointcut, YourAnnotation yourAnnotationVariableName){
System.out.print("End\n\r");
}
}
注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface YourAnnotation {
public boolean isRun() default true;
}
主类:
public class MainClass{
public static void main(String[] args) {
MainClass yourClass = new MainClass ();
yourClass.yourMethodAround();
}
@YourAnnotation
public void yourMethodAround(){
System.out.println("Executing TestTarget.yourMethodAround()");
}
}
我正在使用两个模块,POM 看起来像这样:
Aspect 的 POM:
<?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>Group</groupId>
<artifactId>Aspects</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</project>
主 POM:
<?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>Group</groupId>
<artifactId>Main</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<pluginManagement>
<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>
<aspectLibraries>
<aspectLibrary>
<groupId>Group</groupId>
<artifactId>Aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>aspects.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>a-make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>aspects.MainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>Group</groupId>
<artifactId>Aspects</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
我在两个项目中都执行了 mvn clean install,然后在 Main 项目中执行了 mvn exec:java,它只运行方法,而不是方面。谁能帮帮我?
谢谢!
最佳答案
这是一个多模块解决方案。
主 POM(所有其他 POM 的父级):
这里我们定义了所有插件的基本配置(更具体的配置可以在“application”模块中找到)并管理所有的依赖版本。
<?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>de.scrum-master.stackoverflow</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.source-target.version>1.8</java.source-target.version>
<aspectj.version>1.8.10</aspectj.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo> -->
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.source-target.version}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<verbose>true</verbose> -->
<!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn> -->
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>a-make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>aspect</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>common</module>
<module>application</module>
<module>aspect</module>
</modules>
</project>
模块“通用”:
此模块包含“应用程序”和“方面”使用的代码,在本例中更具体地说是注释类。
<?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>
<parent>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>common</artifactId>
</project>
package de.scrum_master.common;
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 YourAnnotation {
boolean isRun() default true;
}
模块“方面”:
这里我们只有方面代码。
<?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>
<parent>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>aspect</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
</project>
package de.scrum_master.aspect;
import de.scrum_master.common.YourAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class YourAspect {
@Pointcut("@annotation(yourAnnotationVariableName)")
public void annotationPointCutDefinition(YourAnnotation yourAnnotationVariableName) {
}
@Pointcut("execution(* *(..))")
public void atExecution() {
}
@Around("annotationPointCutDefinition(yourAnnotationVariableName) && atExecution()")
public Object aroundAdvice(ProceedingJoinPoint thisJoinPoint, YourAnnotation yourAnnotationVariableName) throws Throwable {
if (yourAnnotationVariableName.isRun()) {
Object result;
try {
System.out.println("Before " + thisJoinPoint);
result = thisJoinPoint.proceed();
} catch (Throwable t) {
throw t;
} finally {
System.out.println("After " + thisJoinPoint);
}
return result;
}
return thisJoinPoint.proceed();
}
}
模块“应用程序”:
此模块包含应用程序代码。它配置了 Exec Maven 和 Maven Assembly 插件。此外,将“方面”模块定义为 AspectJ Maven 的方面库。
<?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>
<parent>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>main</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>application</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>de.scrum_master.app.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>de.scrum_master.app.MainClass</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>aspect</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>de.scrum-master.stackoverflow</groupId>
<artifactId>aspect</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
package de.scrum_master.app;
import de.scrum_master.common.YourAnnotation;
public class MainClass {
public static void main(String[] args) {
MainClass yourClass = new MainClass();
yourClass.yourMethodAround();
}
@YourAnnotation
public void yourMethodAround() {
System.out.println("Executing TestTarget.yourMethodAround()");
}
}
构建日志:
Alexander@Xander-PC MINGW64 ~/Documents/java-src/SO_AJ_MavenMultiModuleProblem
$ mvn clean install
(...)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] main ............................................... SUCCESS [ 0.241 s]
[INFO] common ............................................. SUCCESS [ 0.970 s]
[INFO] aspect ............................................. SUCCESS [ 1.058 s]
[INFO] application ........................................ SUCCESS [ 0.607 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.111 s
[INFO] Finished at: 2017-04-07T17:15:39+02:00
[INFO] Final Memory: 23M/378M
[INFO] ------------------------------------------------------------------------
运行日志:
Alexander@Xander-PC MINGW64 ~/Documents/java-src/SO_AJ_MavenMultiModuleProblem
$ cd application/
Alexander@Xander-PC MINGW64 ~/Documents/java-src/SO_AJ_MavenMultiModuleProblem/application
$ mvn exec:java
(...)
[INFO] ------------------------------------------------------------------------
[INFO] Building application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ application ---
Before execution(void de.scrum_master.app.MainClass.yourMethodAround())
Executing TestTarget.yourMethodAround()
After execution(void de.scrum_master.app.MainClass.yourMethodAround())
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(...)
Alexander@Xander-PC MINGW64 ~/Documents/java-src/SO_AJ_MavenMultiModuleProblem/application
$ java -jar target/application-1.0-SNAPSHOT.jar
Before execution(void de.scrum_master.app.MainClass.yourMethodAround())
Executing TestTarget.yourMethodAround()
After execution(void de.scrum_master.app.MainClass.yourMethodAround())
更新:我将整个示例项目推送到 GitHub repository .
关于java - AspectJ 建议不会在 Maven 多模块设置中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43260618/
这个问题已经有答案了: jQuery trigger click vs click ()? (3 个回答) 已关闭 5 年前。 我无法区分 trigger('click')与 trigger('cli
我正在运行 VS 2008 和 .NET 3.5 SP1。 我想在 HttpModule 中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest我的事件 Htt
这是一段代码,我收到以下错误 #1064 - You have an error in your SQL syntax; check the manual that corresponds to yo
有没有办法用任意增量触发滚轮事件。就像 jQuery 对“点击”所做的那样: $('#selector').trigger('click'); 我需要类似的东西,只需一个滚轮即可: $('#selec
我正在尝试在配音数据库中触发时间。我想检查一下在不出现角色的电影配音中不能对角色进行配音。这是PDM: 和CDM 我是SQL的初学者,但我知道表“DUBBES”中应该有一些触发器。我试图做这样的事情,
这个问题已经有答案了: jquery programmatically click on new dom element (3 个回答) 已关闭 6 年前。 我有一个 jQuery 事件定义如下: $
主菜单的点击代码适用于类更改,但不适用于子菜单...当单击食物或鞋子等子菜单项时,它不会触发警报命令...事实上,悬停非常适合子菜单但不是活跃的 HTML
问题非常简单: $('#btn1').click(function(event){ alert( "pageX: " + event.pageX + "\npa
我使用 Spring 的调度程序 (@EnableScheduling) 并具有以下 @Scheduled 方法,该方法每分钟调用一次: @Component public class Schedul
错误 SQL 查询:文档 CREATE TRIGGER `triggers_div` AFTER INSERT ON `produits` FOR EACH ROW BEGIN INSERT INTO
我想在插入另一个表时填充表中的一些列值,并为特定列设置条件。我使用触发器: CREATE TRIGGER inserttrigger AFTER INSERT ON table1 FOR EACH R
我可以在 5.6 MySQL 环境中使用一些关于触发器的指导。我想创建一个触发器,如果发现具有相同速度的电脑的价格较低,则该触发器会停止更新。 架构是产品(制造商、型号、类型)PC(型号、速度、内
背景:我们有一个 completed_flag,默认为 0,当有人完成调查时更新为 1。我想记录这次更新发生的时间戳 在编写了这个触发器/函数以在标志从 0 触发到 1 时更新时间戳后,我怀疑我这样做
数据库中有两个表 KistStatus和 LastKistStatus .后者将保存 KistStatus 的所有“最新”值。 . KistStatus有大约 174.000 条记录,LastKist
我正在开发一个使用 APNS 的 iPhone 应用程序。我很清楚实现 APNS、创 build 备 token 的过程,等等等等……我不知道如何通过 Web 服务从提供商端触发和启动 APNS。任何
我有这个 javascript,当数量更改时会触发 update_cart... jQuery('div.woocommerce').on('change', '.qty', function
当我单击任何按钮时,click 事件不会被触发。艰难的是,我使用 $("div").on("click", "button", function () { 让它工作,但我想看到它使用 .class 工
如何在我的代码中触发 Android onCreateOptionsMenu 函数,即无需用户单击手机上的选项菜单按钮? 最佳答案 Activity.openOptionsMenu(); 就可以了 关
我将表单包装在 中然后我设置 list android:windowSoftInputMode="adjustResize" (默认 react native )。现在,当我用手指触摸事件手动聚焦一
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
我是一名优秀的程序员,十分优秀!