在以下情况下,我需要使用与方法匹配的切入点创建方面:
我认为前两个条件很简单,但我不知道是否有可能用Spring完成第三个条件.如果不是,也许我可以将其改为:
你认为有可能实现这个目标吗?性能会好吗?
谢谢
编辑:匹配方法的一个例子.如您所见,MyMethod没有注释(但它可以).
@Controller
public class MyClass {
public void MyMethod (String arg0, @MyParamAnnotation Object arg1, Long arg3) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我最终使用的解决方案,基于@Espen答案.正如你所看到的,我改变了我的条件:class实际上并不需要成为@Controller.
@Around("execution(public * * (.., @SessionInject (*), ..))")
public void methodAround(JoinPoint joinPoint) throws Exception {
...
}
Run Code Online (Sandbox Code Playgroud) 如何编写适用于覆盖带注释的接口方法的方法执行的aspectj切入点?例如:
interface A {
@MyAnnotation void method();
}
class B implements A {
void method();
}
Run Code Online (Sandbox Code Playgroud)
切入点execution(@MyAnnotation * *.*(..))仅在B.method()带有注释本身时匹配.还有另一种方法吗?
Spring AOP有一个名为的方法级跟踪器CustomizableTraceInterceptor.使用Spring的XML配置方法,可以像这样设置此跟踪器:
<bean id="customizableTraceInterceptor" class="
org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
我想使用Spring的JavaConfig样式设置上述配置(即利用Java注释,尤其是@EnableAspectJAutoProxy在JavaConfig中激活AspectJ).
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
什么是@EnableAspectJAutoProxy风格的等价物<aop:advisor advice-ref="customizableTraceInterceptor" ...>?
我正在将我的项目从java 7迁移到java 8,我遇到的问题与使用aspectj编织有关aspectj-maven-plugin.
根据Haus文档,我可以使用在Java 6和7上运行的这个插件成功配置编织.但问题是我还没有找到任何方法来使用(并找到)支持java 8的插件版本7.我在这里看到插件7增加了java 8支持,但找不到使用它的方法.
这是我需要的配置插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version> <!-- AspectJ weaver plugin 7 is for java 8 (version 1.6 is for java 7) -->
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我确认使用版本1.6的上述代码适用于Java 7,但没有运气试图使用1.7版.
你知道如何运行在Java 8上运行的spring + aspectj的weaver吗?
我尝试使用aspectj maven插件与aspectj编译器编译项目,然后我尝试将类打包成"war"文件.不幸的是,它不适用于以下配置(pom.xml):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<version>${liferay.maven.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
<appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
<appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
<appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
<liferayVersion>${liferay.version}</liferayVersion>
<pluginType>portlet</pluginType>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
<failOnError>true</failOnError>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilationLevel>1.7</compilationLevel>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
<type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)
之后mvn clean install我看到下列情况除外: …
我正在尝试为Custom Aspect编写Junit测试.这是Aspect Class Snippet:
@Aspect
@Component
public class SampleAspect {
private static Logger log = LoggerFactory.getLogger(SampleAspect.class);
@Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
public Object intercept(final ProceedingJoinPoint point) throws Throwable {
logger.info("invoked Cutom aspect");
return point.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,只要关节点与切入点匹配,上述方面就会截获.它的工作正常.
但我的问题是如何对该类进行单元测试.我有以下Junit测试:
@Test(expected = MongoTimeoutException.class)
public void TestWithMongoTemplate() {
//MongoDocument class
TestDocument test = new TestDocument();
ApplicationContext ctx = new AnnotationConfigApplicationContext(TestMongoConfigurationMain.class);
MongoTemplate mongoTemplate = ctx.getBean(MongoTemplate.class);
//this call is being intercepted by SampleAspect
mongoTemplate.save(test);
}
Run Code Online (Sandbox Code Playgroud)
因此,我mongoTemplate.save(test)在Junit中被拦截,SampleAspect因为它匹配切入点.但是,我应该如何确保junits(可能通过声明)SampleAspect在调用该关节点时我正在拦截?
我不能断言返回值,intercept()因为除了执行关节点之外没有什么特别之处.因此,我的Junit无法找到任何区别,无论是由方面执行还是基于返回值的常规执行. …
我有集成测试(加载上下文)和单元测试一起运行.我的代码使用spring进行编译时编织.
我的问题是我声明的建议也在我的一些单元测试中运行.这会杀死单元测试的概念,这就是我想禁用它们的原因.
有什么我可以放在切入点声明,我可以调用的一些方法,一些弹簧配置,或maven命令,禁用这些建议像所有*UnitTest.java?
谢谢您的帮助.
例:
我有以下单元测试:
@RunWith(MockitoJUnitRunner.class)
public class CompanyServiceImplTest {
@Test
public void createCampaignTest() throws Exception {
when(companyDaoMock.saveCompany(any(Campaign.class))).thenReturn(77L);
Long campaignId = companyService.createCampaign(campaignMock);
assertEquals(Long.valueOf(77L), Long.valueOf(campaignId));
}
}
Run Code Online (Sandbox Code Playgroud)
以及服务方法:
@Override
@Transactional
@EventJournal(type = EventType.CAMPAIGN_CREATE, owner = EventOwner.TERMINAL_USER)
public Long createCampaign(Campaign campaign) {
return companyDao.saveCompany(campaign);
}
Run Code Online (Sandbox Code Playgroud)
方面:
@Aspect
public class EventJournalAspect {
@Autowired
private EventJournalService eventJournalService;
@Pointcut(value="execution(public * *(..))")
public void anyPublicMethod() {}
@Pointcut("within(com.terminal.service..*)")
private void inService() {}
@AfterReturning(pointcut = "anyPublicMethod() && inService() && @annotation(eventJournal) && args(entity,..)", returning = "id")
public void …Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一个切入点表达式来匹配包含用特定注释注释的参数的方法,无论参数位于什么位置.在我的情况下,我正在寻找@Constraint注释.例如:
匹配方法:
public void method1(@Constraint Car car)
public void method2(String id, @Constraint Plane plane)
public void method3(Wheel wheel, @Constraint List<Train> trains, @Constraint Plane plane)
public void method4(Motor motor, @Constraint Set<Train> trains, Bicycle bike, Wheel wheel)
public void method5(Wing wing, Motorcycle moto, @Constraint Truck truck, Bicycle bike, Wheel wheel)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了以下表达式,没有运气:
@Before("execution(public * *.*(..)) and @args(com.example.Constraint)") // there can be only one parameter
@Before("execution(public * *.*(..)) and @args(..,com.example.Constraint)") // parameter must be in last position
@Before("execution(public * *.*(..)) and @args(com.example.Constraint,..)") …Run Code Online (Sandbox Code Playgroud) 更新:这是我的maven-compiler-plugin配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我在使用Maven构建的多项目应用程序上工作.我们决定添加AspectJ,所以我pom.xml在顶级项目中添加了以下代码:(来自官方文档)
<project>
...
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
<build>
...
</project>
Run Code Online (Sandbox Code Playgroud)
以及每个下属项目的以下片段:
</project>
...
<build>
....
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build> …Run Code Online (Sandbox Code Playgroud) 我最近开始研究AspectJ并编写了一个简单的Logging for Logging.
我已将下面的依赖项定义到我的pom文件中:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并且ear/lib文件夹中存在以下工件:
我在服务启动时遇到异常:
05:59:18,325 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-15) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'calNamingBean' defined in ServletContext resource [/WEB-INF/helixservicebeans.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:740) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at …Run Code Online (Sandbox Code Playgroud) aspectj ×10
java ×9
spring ×6
aop ×4
spring-aop ×3
maven ×2
pointcut ×2
annotations ×1
java-8 ×1
java-ee ×1
junit ×1
mockito ×1
pointcuts ×1
pom.xml ×1
unit-testing ×1