java根据环境变量或属性执行切入点
我开发了一个不错的Spring Aspect
,可以用来监控我的服务操作性能。如果执行某些操作需要很长时间,它会记录这些操作
@Aspect
public class PerformanceMonitorAspect {
private Logger logger = LoggerFactory.getLogger("performance");
@Pointcut("execution(* com.company.MyService+.*(..))")
public void pointCut(){
}
@Around("pointCut()")
public Object profileServiceMethods(ProceedingJoinPoint thisJoinPoint) throws Throwable {
MethodSignature ms = (MethodSignature) thisJoinPoint.getSignature();
Method m = ms.getMethod();
long t1 = System.nanoTime();
Object result = thisJoinPoint.proceed();
long t2 = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(t2 - t1);
if (millis < 1000) {
logger.trace("Execution time for {}: {} ms", m.getName(), millis);
} else {
logger.warn("Substantial execution time for {}: {} ms", m.getName(),
millis);
}
return result;
}
}
然而,由于它是一个@Around
建议,Spring控制了我所有的方法调用,以便对它们进行分析。调试时这有点不舒服(它甚至误导了Eclipse本身),所以我希望切入点只在应用程序处于生产阶段时执行。我可以根据环境变量、Java属性或类似属性将切入点配置为有条件地执行吗
Documentation仅指方法变量条件。。。提前谢谢
编辑
根据@DavidL的建议,我将切入点改为:
@Pointcut("execution(* com.tadic.module.TadicModuleGeneric+.*(..)) && if()")
public static boolean pointCut() {
return true;
}
这就是Eclipse不发出任何警告的方式。然而,我在运行时得到了以下结果:
GRAVE: Critical error during deployment:
java.lang.VerifyError: Expecting a stackmap frame at branch target 7
Exception Details:
Location:
com/mycompany/aspects/AuditAspect.<clinit>()V @1: invokestatic
Reason:
Expected stackmap frame at this location.
Bytecode:
0000000: 00b8 0134 a700 084b 2ab3 012f b1
Exception Handler Table:
bci [1, 7] => handler: 7
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getDeclaredMethods(Class.java:1860)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:474)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:458)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1350)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:355)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:434)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
看来织工做得不好。有什么建议吗
编辑2 当我通过基于Spring AOP代理的方面使用AspectJ时,就会出现这个问题。我正在使用Maven构建这个项目,并使用Eclipse AspectJ插件将其集成到我的工作区中。AspectJ版本是1.8.2,Spring AOP版本是3.2.8。我正在用Java7.0.75JDK构建它
以下是正在使用的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>com.tesicnor.test</groupId>
<artifactId>aspect-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aspect-test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<aspectj.version>1.8.2</aspectj.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
这个POM是我创建的一个功能测试用例的POM。我目前的项目规模很大,似乎还有其他依赖性造成了麻烦
# 1 楼答案
您可以获得对表示当前阶段的属性的引用(可能是静态的)。根据它的值,可以绕过
profileServiceMethods
方法内的代码(提前返回)以一种不那么美观的方式,您可以在aspect中声明一个布尔变量,用于相同的目的:
编辑:
看看医生。也许你可以这样做:
# 2 楼答案
终于,我成功了。似乎Eclipse在我的类路径中造成了一些混乱,导致了上述问题。我删除了
aspectjweaver
,根据@DavidL的答案,我可以获得以下代码:然后,让Spring来管理这个方面: