基本上我有一个普通的Java应用程序,有一个主要的。我使用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添加到依赖项中......当我尝试运行该程序时,它仍然没有做任何事情。它只是在没有应用方面的情况下正常运行。 有什么想法吗?
答案 0 :(得分:1)
这是编译时编织的解决方案。因为所有主要的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)