我的印象是Spring AOP最适合用于特定于应用程序的任务,例如安全性,日志记录,事务等,因为它使用自定义Java5注释作为框架.然而,AspectJ似乎更加友好的设计模式.
任何人都可以强调在Spring应用程序中使用Spring AOP和AspectJ的各种优缺点吗?
我想监视具有指定注释的所有类的所有公共方法(比如@Monitor)(注意:注释在类级别).什么可能是一个可能的切入点?注意:我使用的是@AspectJ样式的Spring AOP.
我是Spring Transaction的新手.我发现的一些事情很奇怪,可能我确实理解了这一点.我希望在方法级别有一个事务处理,并且我在同一个类中有一个调用方法,看起来它不喜欢它,它必须从单独的类调用.我不明白这是怎么可能的.如果有人知道如何解决这个问题,我将不胜感激.我想使用相同的类来调用带注释的事务方法.
这是代码:
public class UserService {
@Transactional
public boolean addUser(String userName, String password) {
try {
// call DAO layer and adds to database.
} catch (Throwable e) {
TransactionAspectSupport.currentTransactionStatus()
.setRollbackOnly();
}
}
public boolean addUsers(List<User> users) {
for (User user : users) {
addUser(user.getUserName, user.getPassword);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在学习面向方面的编程概念和Spring AOP.我无法理解Pointcut和Joinpoint之间的区别 - 它们对我来说似乎都是一样的.Pointcut是您应用建议的地方,Joinpoint也是我们可以应用我们建议的地方.那有什么区别?
切入点的一个例子可以是:
@Pointcut("execution(* * getName()")
Run Code Online (Sandbox Code Playgroud)
什么是Joinpoint的例子?
问题很简单:有没有办法从apsectj ProceedingJoinPoint获取Method对象?
目前我在做
Class[] parameterTypes = new Class[joinPoint.getArgs().length];
Object[] args = joinPoint.getArgs();
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
parameterTypes[i] = args[i].getClass();
}
else {
parameterTypes[i] = null;
}
}
String methodName = joinPoint.getSignature().getName();
Method method = joinPoint.getSignature()
.getDeclaringType().getMethod(methodName, parameterTypes);
Run Code Online (Sandbox Code Playgroud)
但我不认为这是要走的路......
首先让我注意一下,我使用AspectJ,我喜欢它,但我还能做些什么呢.
我知道AspectJ可以用于日志记录.在某些情况下,它用于事务控制 - 主要与注释一起实现.AspectJ也可以用于增强类(代码生成)方法,就像Spring Roo一样.
但我相信AspectJ和AOP一般可以用于:日志记录,事务控制和模拟部分类.
那么AspectJ和AOP的其他有用用例是什么?
我正在研究的项目与DAOs下面的项目有类似的结构:
/**
* Base DAO class
*/
@Transactional
public class JPABase {
@PersistenceContext
private EntityManager entityManager;
public void persist(Object entity) {
entityManager.persist(entity);
}
//some more methods in here
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* Generic DAO class implementation
*/
@Transactional
public abstract class GenericDao extends JpaBase {
//some methods in here
}
Run Code Online (Sandbox Code Playgroud)
和
/**
* Specialized DAO class
*/
@Repository
@Transactional
public class PersonDao extends GenericDao {
//some methods in here
}
Run Code Online (Sandbox Code Playgroud)
到现在为止,该项目使用的编译时编织,但而配置也改为使用<context:load-time-weaver/>与-javaagent:/opt/tomcat7-1/lib/spring-instrument.jar.
由于已经应用了这种改变,因此不再编织JpaBase's和 …
我用Spring AOP写了一个非常简单的Aspect.它有效,但我在理解真正发生的事情时遇到了一些问题.我不明白为什么我要添加aspectjweaver.jar?Spring-AOP文档明确指出,只要我使用Spring-AOP,我就不需要aspectj编译器或weaver:
AOP运行时仍然是纯Spring AOP,并且不依赖于AspectJ编译器或weaver.
我的配置如下所示:
<aop:aspectj-autoproxy />
@Aspect
@Service
public class RemoteInvocationAspect {
@Before("execution(* at.test.mypackage.*.*(..))")
public void test() {
System.out.println("test");
}
...
Run Code Online (Sandbox Code Playgroud)
我也尝试过XML配置,虽然没有改变任何东西.也许我可以放手,但我真的想了解为什么使用aspectj-weaver?如果我没有在maven中添加依赖项,我会得到 java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
我有以下弹簧配置:
<context:component-scan base-package="uk.co.mysite.googlecontactsync.aop"/>
<bean name="simpleEmailSender" class="uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation"/>
<aop:aspectj-autoproxy/>
Run Code Online (Sandbox Code Playgroud)
然后我有一个方面:
@Aspect
public class SyncLoggingAspect {
@Autowired
private SimpleEmailSender simpleEmailSender
@AfterReturning(value="execution(* uk.co.mysite.datasync.polling.Poller+.doPoll())", returning="pusher")
public void afterPoll(Pusher pusher) {
simpleEmailSender.send(new PusherEmail(pusher));
}
}
Run Code Online (Sandbox Code Playgroud)
这个方面有效(我可以在afterPoll上找到断点)但simpleEmailSender为null.不幸的是,我找不到明确的文件说明原因.(为了记录,我的simpleEmailSender bean存在并正确连接到其他类)以下事情使我感到困惑:
显然你可以告诉我,我不了解事情应该如何从头开始.
将方面应用于我的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)