gpt4 book ai didi

java - 无法让我的方面运行

转载 作者:行者123 更新时间:2023-12-02 13:31:46 27 4
gpt4 key购买 nike

基本上我有一个普通的 Java 应用程序,带有 main.我使用 Intelij Ultimate。我有以下 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>88</groupId>
<artifactId>SpaceX</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>



</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

所以我还有一个 LoggingAspect,我只是不知道如何运行。我也试过了http://confluence.jetbrains.com/display/~roman.shevchenko/AspectJ+Support+Plugin

并手动下载了 jar,但我还下载了 Aspects 的 Intelij 插件。就像 AspectJ 支持和 Spring Aspect 一样。

我的方面类如下所示:

public aspect LoggingAspect {
pointcut tracing():call(public * com.company..*.*(..)) && !within(LoggingAspect);

private Logger logger= Logger.getLogger("com.company");

public LoggingAspect() {
PropertyConfigurator.configure("logging.properties");
}

before(): tracing(){
logger.info("Entering: "+ thisJoinPointStaticPart.getSignature());
}

after():tracing(){
logger.info("Exiting: "+ thisJoinPointStaticPart.getSignature());
}
}
正如你所看到的。我想使用 java.util.logging.Logger,并且我有一个logging.properties 文件,我在其中设置输出文件。我尝试像上面粘贴的链接一样编译应用程序,正常运行应用程序,但似乎没有任何效果。我的方面根本不起作用/它没有被使用。有什么建议吗?我错过了什么吗?

我还不想将 Spring Aspect 与注释一起使用。我不知道如何让这个首先工作

我将编译器更改为ajc并测试了连接,一切正常。我已将 Aspectjrt 添加到依赖项中...当我尝试运行该程序时它仍然没有执行任何操作。它只是正常运行而不应用方面。有什么想法吗?

最佳答案

这是编译时编织的解决方案。因为所有主要 IDE 都可以从 Maven 导入和更新项目,所以它可以在 Eclipse、IntelliJ IDEA 中工作,也可能在 NetBeans 中工作(从未尝试过)。

Maven POM:

<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>88</groupId>
<artifactId>SpaceX</artifactId>
<version>1.0-SNAPSHOT</version>

<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.5.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>
</plugins>
</pluginManagement>

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

</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>

</project>

Log4J 配置文件:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

驱动程序应用程序:

package com.company.app;

public class Application {
public static void main(String[] args) {
new Application().doSomething("foo", 11);
}

public String doSomething(String string, int i) {
return "blah";
}
}

改进方面:

加载 logging.properties 的方式仅适用于 JAR 中,但不适用于从 IDE 运行代码时。我建议您依赖从 Maven 项目正确导入的类路径,将配置文件放在 src/main/resources 下,然后通过 ClassLoader.getResourceAsStream(..) 打开它>.

package com.company.aspect;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public aspect LoggingAspect {
private static final Logger logger = Logger.getLogger("com.company");

public LoggingAspect() {
PropertyConfigurator.configure(
Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream("logging.properties")
);
}

pointcut tracing() :
call(public * com.company..*.*(..)) &&
!within(LoggingAspect);

before() : tracing() {
logger.info("Entering: " + thisJoinPointStaticPart.getSignature());
}

after() : tracing() {
logger.info("Exiting: " + thisJoinPointStaticPart.getSignature());
}
}

控制台日志:

2017-04-02 17:58:06 INFO  company:23 - Entering: String com.company.app.Application.doSomething(String, int)
2017-04-02 17:58:06 INFO company:27 - Exiting: String com.company.app.Application.doSomething(String, int)

关于java - 无法让我的方面运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43162772/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com