Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在没有Spring AOP的情况下开始使用aspectj_Java_Aspectj - Fatal编程技术网

Java 在没有Spring AOP的情况下开始使用aspectj

Java 在没有Spring AOP的情况下开始使用aspectj,java,aspectj,Java,Aspectj,我想在我的应用程序中加入AspectJ,以了解它是如何工作的。我 我不想使用SpringAOP,而是“纯”的aspectj 以下是我所拥有的: <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> 当我执行

我想在我的应用程序中加入AspectJ,以了解它是如何工作的。我 我不想使用SpringAOP,而是“纯”的aspectj

以下是我所拥有的:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.11</version>
</dependency>
当我执行应用程序时,不会打印消息

按照我的理解,
beforeMethod
应该在整个项目中任何类的任何方法之前调用

我猜我忘记了一些东西,但我还没有找到一个好的教程,我很清楚它是如何工作的


我从这里到哪里去?

在Eclipse中,项目类型必须更改为aspectj项目(用鼠标右键单击project->aspectj)

您需要在pom.xml中使用

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            </plugin>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        aspectj-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.7,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>test-compile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

org.aspectj
aspectjrt
org.codehaus.mojo
aspectj maven插件
1.7
1.7
1.7
编译
测试编译
org.eclipse.m2e
生命周期映射
1.0.0
org.codehaus.mojo
aspectj maven插件
[1.7,)
编译
测试编译
org.codehaus.mojo
aspectj maven插件

将切入点更改为“!within(tmp.loggingspect)&&execution(*.(..)”,以避免捕捉他自己。

在Eclipse项目类型中必须更改为aspectj项目(在project->aspectj上用鼠标右键)

您需要在pom.xml中使用

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            </plugin>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        aspectj-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.7,)
                                    </versionRange>
                                    <goals>
                                        <goal>compile</goal>
                                        <goal>test-compile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

org.aspectj
aspectjrt
org.codehaus.mojo
aspectj maven插件
1.7
1.7
1.7
编译
测试编译
org.eclipse.m2e
生命周期映射
1.0.0
org.codehaus.mojo
aspectj maven插件
[1.7,)
编译
测试编译
org.codehaus.mojo
aspectj maven插件

将切入点更改为“!within(tmp.loggingspect)&&execution(*.(..)”,以避免抓到他自己。

在我看来,从13版开始,它就已经内置在aspectj的插件中了。下一步应该做的是更改Java编译器的设置

Project Setting –> Compiler –> Java Compiler
“使用编译”:更改为Ajc

“Ajc编译器路径”:aspjectjtools.jar

如果你没有罐子,请点击以得到你想要的


现在,运行你的程序,它会工作的。祝你好运!

在我看来,从13版开始,它已经在aspectj的插件中构建了。下一步你应该做的是更改Java编译器的设置

Project Setting –> Compiler –> Java Compiler
“使用编译”:更改为Ajc

“Ajc编译器路径”:aspjectjtools.jar

如果你没有罐子,请点击以得到你想要的


现在,运行您的程序,它会工作的。祝您好运!

如果您刚刚开始,请使用最新的aspectj版本(目前为1.8.9),而不是多年前的版本。如果您刚刚开始,请使用最新的aspectj版本(目前为1.8.9),不是很多年前的版本。AspectJ Maven插件的当前版本是1.9。它还可以与Eclipse一起工作,而1.8出现了一个问题,使它与(re-)不兼容将项目导入Eclipse。因此不再需要使用旧的1.7。我无法用常规maven项目实现这一点吗?为什么我需要将其转换为aspectj项目?@user3629892 aspectj需要在编译后调用编织逻辑。您的项目可以是maven项目。但编译类型必须更改。当前版本的spectJ Maven插件是1.9。它也可以与Eclipse一起工作,而1.8出现了一个问题,使得它与(re-)不兼容将项目导入Eclipse。因此不再需要使用旧的1.7。我无法用常规maven项目实现这一点吗?为什么我需要将其转换为aspectj项目?@user3629892 aspectj需要调用weav