将方面应用于我的maven项目时遇到问题.可能我错过了一些东西,所以我已经列出了一系列步骤.你能检查一下是否正确吗?
让我们说projectA是一个方面类和projectB类,应该由方面改变.
ProjectA用AspectJclass 创建maven项目Aspectj插件和依赖项ProjectA为依赖项projectB pom.xmlprojectB pom.xml插件<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<aspectLibraries>
<aspectLibrary>
<groupId>ProjectA</groupId>
<artifactId>ProjectA</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
完成所有这些步骤之后我的问题是,在编译期间,我得到:
[WARNING] advice defined in AspectE has not been applied [Xlint:adviceDidNotMatch]
Run Code Online (Sandbox Code Playgroud)
然后当我运行我的程序时:
Exception in thread "FeatureExcutionThread" java.lang.NoClassDefFoundError: AspectE
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Spring @Configurable并将@AutowireDAO注入域对象,这样他们就不需要直接了解持久层了.
我正在尝试关注http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable,但我的代码似乎没有效果.
基本上,我有:
@Configurable
public class Artist {
@Autowired
private ArtistDAO artistDao;
public void setArtistDao(ArtistDAO artistDao) {
this.artistDao = artistDao;
}
public void save() {
artistDao.save(this);
}
}
Run Code Online (Sandbox Code Playgroud)
和:
public interface ArtistDAO {
public void save(Artist artist);
}
Run Code Online (Sandbox Code Playgroud)
和
@Component
public class ArtistDAOImpl implements ArtistDAO {
@Override
public void save(Artist artist) {
System.out.println("saving");
}
}
Run Code Online (Sandbox Code Playgroud)
在application-context.xml中,我有:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" factory-method="aspectOf"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
类路径扫描和初始化由弹簧模块执行Play!框架,虽然其他autowired …
我可以对大多数Spring类进行单元测试,而无需执行Spring"东西".
我也可以在不使用Spring的情况下对@Before建议方法进行单元测试:
示例代码:
@Before("execution(* run(..)) && " + "" +
"target(target) && " +
"args(name)")
public void logName(Object target, String name) {
logger.info("{} - run: {}", target, name);
}
Run Code Online (Sandbox Code Playgroud)
示例测试:
@Test
public void testLogName() {
aspect.setLogger(mockLogger);
aspect.logName(this,"Barry");
assertTrue(mockLogger.hasLogged("TestAspect - run: Barry"));
}
Run Code Online (Sandbox Code Playgroud)
但是@Around建议处理ProceedingJoinPoint对象:
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何实例化ProceedingJoinPoint对象.如何在不启动整个Spring应用程序上下文的情况下测试此类?
我正在通过ExecutorService运行一些外部方法的多次调用.我希望能够中断这些方法,但遗憾的是它们不会自行检查中断标志.有什么办法可以强制从这些方法中引发异常吗?
我知道从任意位置抛出异常是有潜在危险的,在我的具体情况下,我愿意抓住这个机会并准备应对后果.
"外部方法"我的意思是来自外部库的一些方法,我不能修改它的代码(我可以,但是每当新版本发布时,这将使它成为维护的噩梦).
外部方法计算成本高,不受IO限制,因此它们不响应常规中断,我无法强行关闭通道或套接字等.正如我之前提到的,他们也没有检查中断标志.
代码在概念上类似于:
// my code
public void myMethod() {
Object o = externalMethod(x);
}
// External code
public class ExternalLibrary {
public Object externalMethod(Object) {
innerMethod1();
innerMethod1();
innerMethod1();
}
private void innerMethod1() {
innerMethod2();
// computationally intensive operations
}
private void innerMethod2() {
// computationally intensive operations
}
}
Run Code Online (Sandbox Code Playgroud)
Thread.stop()理论上我会做我想要的,但它不仅被弃用,而且它也只适用于实际线程,而我正在使用执行器任务(也可能与未来任务共享线程,例如在线程池中工作时) .然而,如果找不到更好的解决方案,我将转换我的代码以使用老式的Threads并使用此方法.
我尝试过的另一个选择是myMethod()使用特殊的"Interruptable"注释标记和类似的方法,然后使用AspectJ(我无疑是新手)来捕获所有方法调用 - 类似于:
@Before("call(* *.*(..)) && withincode(@Interruptable * *.*(..))")
public void checkInterrupt(JoinPoint thisJoinPoint) {
if (Thread.interrupted()) …Run Code Online (Sandbox Code Playgroud) 所以我有一个代码:
@Path("/foo")
public class Hello {
@GET
@Produces("text/html")
public String getHtml(@Context Request request, @Context HttpServletRequest requestss){
...
}
Run Code Online (Sandbox Code Playgroud)
我正在使用AspectJ来捕获对getHtml方法的所有调用.我想获得传递的参数@Produces,并@Path在我的建议,即"/foo"与"text/html"在这种情况下.我怎么能用反射来做呢?
我有一个在Java 6/Spring 3中实现的服务类,它需要一个注释来限制角色访问.
我已经定义了一个名为RequiredPermission的注释,它具有一个名为OperationType的枚举中的一个或多个值作为其value属性:
public @interface RequiredPermission {
/**
* One or more {@link OperationType}s that map to the permissions required
* to execute this method.
*
* @return
*/
OperationType[] value();}
public enum OperationType {
TYPE1,
TYPE2;
}
package com.mycompany.myservice;
public interface MyService{
@RequiredPermission(OperationType.TYPE1)
void myMethod( MyParameterObject obj );
}
package com.mycompany.myserviceimpl;
public class MyServiceImpl implements MyService{
public myMethod( MyParameterObject obj ){
// do stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
我还有以下方面定义:
/**
* Security advice around methods that are annotated with …Run Code Online (Sandbox Code Playgroud) 比方说,你有三个建议:各地,之前和之后.
1)为前/后调用时继续被称为在各地的意见,或者他们所谓的前/后的周围建议作为一个整体?
2)如果我的周围建议没有调用继续,那么是否会运行之前/之后的建议?
我想在我的所有公共方法中添加"trace"消息,如下所示:
public void foo(s:String, n:int) { // log is a log4j logger or any other library
log.trace(String.format("Enter foo with s: %s, n: %d", s, n))
...
log.trace("Exit foo")
}
现在我想log.trace用AOP(和字节码检测)自动将所有这些添加到我的方法中.我在考虑AspectJ.是否有意义?你知道任何开放源代码吗?
我有两个注解@LookAtThisMethod和@LookAtThisParameter,如果我身边有方法的切入点与@LookAtThisMethod我怎么能提取其标注了该方法的参数@LookAtThisParameter?
例如:
@Aspect
public class LookAdvisor {
@Pointcut("@annotation(lookAtThisMethod)")
public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){}
@Around("lookAtThisMethodPointcut(lookAtThisMethod)")
public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable {
for(Object argument : joinPoint.getArgs()) {
//I can get the parameter values here
}
//I can get the method signature with:
joinPoint.getSignature.toString();
//How do I get which parameters are annotated with @LookAtThisParameter?
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用方面的lib,可以通过maven获得,现在我正在尝试在Android应用程序中使用该lib.
如果我在app gradle文件中包含此插件,一切正常,但我的目标是将my classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'和apply plugin: 'android-aspectj'插件所需的(和插件所需)解压缩到my.lib gradle文件而不是在我的应用程序中声明.
那可能吗?
app gradle文件:
classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
apply plugin: 'android-aspectj'
dependencies {
compile 'my.lib:example:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)
目标:
app gradle文件:
dependencies {
compile 'my.lib:example:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)
my.lib gradle文件:
classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
apply plugin: 'android-aspectj'
dependencies {
compile 'org.aspectj:aspectjrt:1.7.3'
}
Run Code Online (Sandbox Code Playgroud) aspectj ×10
java ×9
aop ×7
spring ×3
android ×1
annotations ×1
logging ×1
maven ×1
pointcut ×1
reflection ×1
spring-aop ×1