我在其他站点中搜索了Baeldung和MKyong。一切看起来不错,但是我的建议没有执行。我读过某处关于在一个方面有多个切入点的知识,因此我为每个切入点创建了单独的方面,但仍然没有用。我正在尝试进行加载时编织。
我的一个方面类
@Aspect
public class AuditUpdateAspect {
@Before("execution(* com.kable.newsstand.kdsejb.session.KdsSessionUpdate.update*(..))")
public void beforeUpdate(JoinPoint jp) {
System.out.println("Update");
Object o = jp.getArgs()[0];
if (o instanceof Auditable) {
Auditable auditable = (Auditable)o;
auditable.setOsUserName(AuditHelper.getAuditName());
System.out.println("Done");
}
}
}
客户代码
// SessionHelper is a helper class with static methods to get stateless EJBs from JNDI
// getUpdate() returns an instance of KdsSessionUpdateBean which implements the KdsSessionUpdate interface
// updateIssue(Issue issue) is a method in KdsSessionUpdateBean
SessionHelper.getUpdate().updateIssue(issue);
编织发生在下面,您将看到:这是程序启动
[AppClassLoader@4d885088] info AspectJ Weaver Version 1.8.2 built on Thursday Aug 14, 2014 at 21:45:02 GMT
[AppClassLoader@4d885088] info register classloader sun.misc.Launcher$AppClassLoader@4d885088
[AppClassLoader@4d885088] info using configuration /C:/Users/bray/eclipse-workspaces/combined-workspace/audit/target/classes/META-INF/aop.xml
[AppClassLoader@4d885088] info register aspect com.kable.newsstand.knet.audit.AuditCreateAspect
[AppClassLoader@4d885088] info register aspect com.kable.newsstand.knet.audit.AuditRemoveAspect
[AppClassLoader@4d885088] info register aspect com.kable.newsstand.knet.audit.AuditUpdateAspect
[MethodUtil@6b429a7d] info AspectJ Weaver Version 1.8.2 built on Thursday Aug 14, 2014 at 21:45:02 GMT
[MethodUtil@6b429a7d] info register classloader sun.reflect.misc.MethodUtil@6b429a7d
[MethodUtil@6b429a7d] info using configuration /C:/Users/bray/eclipse-workspaces/combined-workspace/audit/target/classes/META-INF/aop.xml
[MethodUtil@6b429a7d] info register aspect com.kable.newsstand.knet.audit.AuditCreateAspect
[MethodUtil@6b429a7d] info register aspect com.kable.newsstand.knet.audit.AuditRemoveAspect
[MethodUtil@6b429a7d] info register aspect com.kable.newsstand.knet.audit.AuditUpdateAspect
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
[AppClassLoader@4d885088] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
这里是对SessionHelper.getUpdate().updateIssue(issue);
的呼叫。这是缩写,因为发帖时间太长。
[AppClassLoader@305f387c] weaveinfo Join point 'method-execution(void com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean.updatePublisherRemitTransRemitIdById(java.lang.Integer, java.lang.Integer))' in Type 'com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean' (KdsSessionUpdateBean.java:64) advised by before advice from 'com.kable.newsstand.knet.audit.AuditUpdateAspect' (AuditUpdateAspect.java)
[AppClassLoader@305f387c] weaveinfo Join point 'method-execution(void com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean.updateIssue(com.kable.newsstand.kdsejb.entity.Issue))' in Type 'com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean' (KdsSessionUpdateBean.java:641) advised by before advice from 'com.kable.newsstand.knet.audit.AuditUpdateAspect' (AuditUpdateAspect.java)
所以看起来好像是在加载时进行了编织,但是两个System.out.println("Update");
和System.out.println("Done");
没有执行。
编辑:根据要求包含aop.xml。
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="com.kable.newsstand.knet.audit.AuditCreateAspect"/>
<aspect name="com.kable.newsstand.knet.audit.AuditRemoveAspect"/>
<aspect name="com.kable.newsstand.knet.audit.AuditUpdateAspect"/>
<weaver options="-verbose -showWeaveInfo">
<include within="com.kable.newsstand.kdsejb.session.*"/>
</weaver>
</aspects>
</aspectj>
编辑:MCVE的测试代码
public AuditTestApp() {
WholesalerGroup wholesalerGroup = new WholesalerGroup();
wholesalerGroup.setCorporationGroupId(new Integer(1000));
wholesalerGroup.setDescription("Test Wholesaler Group For Auditing");
wholesalerGroup.setOldCode("OLD_CODE");
System.out.println(String.format("Audit before create (%s)", wholesalerGroup.getOsUserName()));
WholesalerGroup addedWholesalerGroup = SessionHelper.getCreate().createWholesalerGroup(wholesalerGroup);
System.out.println(String.format("Audit after create (%s)", addedWholesalerGroup.getOsUserName()));
addedWholesalerGroup.setCorporationGroupId(new Integer(2000));
SessionHelper.getUpdate().updateWholesalerGroup(addedWholesalerGroup);
System.out.println(String.format("Audit after update (%s)", addedWholesalerGroup.getOsUserName()));
SessionHelper.getRemove().removeWholesalerGroup(addedWholesalerGroup);
System.out.println("Finishing");
System.exit(0);
}
编辑:用简单代码替换EJB后的输出
[AppClassLoader@3b756db3] info AspectJ Weaver Version 1.8.2 built on Thursday Aug 14, 2014 at 21:45:02 GMT
[AppClassLoader@3b756db3] info register classloader sun.misc.Launcher$AppClassLoader@3b756db3
[AppClassLoader@3b756db3] info using configuration /C:/Users/bray/eclipse-workspaces/combined-workspace/audit/target/classes/META-INF/aop.xml
[AppClassLoader@3b756db3] info register aspect com.kable.newsstand.knet.audit.AuditCreateAspect
[AppClassLoader@3b756db3] info register aspect com.kable.newsstand.knet.audit.AuditRemoveAspect
[AppClassLoader@3b756db3] info register aspect com.kable.newsstand.knet.audit.AuditUpdateAspect
Audit before create (null)
[AppClassLoader@3b756db3] weaveinfo Join point 'method-execution(com.kable.newsstand.kdsejb.entity.WholesalerGroup com.kable.newsstand.kdsejb.session.KdsSessionCreateBean.createWholesalerGroup(com.kable.newsstand.kdsejb.entity.WholesalerGroup))' in Type 'com.kable.newsstand.kdsejb.session.KdsSessionCreateBean' (KdsSessionCreateBean.java:8) advised by before advice from 'com.kable.newsstand.knet.audit.AuditCreateAspect' (AuditCreateAspect.java)
[AppClassLoader@3b756db3] weaveinfo Join point 'method-execution(void com.kable.newsstand.kdsejb.session.KdsSessionRemoveBean.removeWholesalerGroup(com.kable.newsstand.kdsejb.entity.WholesalerGroup))' in Type 'com.kable.newsstand.kdsejb.session.KdsSessionRemoveBean' (KdsSessionRemoveBean.java:8) advised by before advice from 'com.kable.newsstand.knet.audit.AuditRemoveAspect' (AuditRemoveAspect.java)
[AppClassLoader@3b756db3] weaveinfo Join point 'method-execution(void com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean.updateWholesalerGroup(com.kable.newsstand.kdsejb.entity.WholesalerGroup))' in Type 'com.kable.newsstand.kdsejb.session.KdsSessionUpdateBean' (KdsSessionUpdateBean.java:8) advised by before advice from 'com.kable.newsstand.knet.audit.AuditUpdateAspect' (AuditUpdateAspect.java)
Create
Done
Audit after create (:BRay:knc3940:192.168.1.7)
Update
Done
Audit after update (:BRay:knc3940:192.168.1.7)
Remove
Done
Finishing