我们正在使用liferay tomcat中的JSR-286 portlet开发门户应用程序。我们正在创建扩展Generic Portlet的portlet。现在我想使用AspectJ在doView()和doModify()方法上应用日志。我试过Spring AspectJ。但Spring AspectJ仅适用于Spring托管bean。
任何运气
答案 0 :(得分:1)
我解决了上述问题。
这里有一些解决方案可以完成它。
创建一个方面
@Aspect
public class TestAspect {
TestAspect (){
System.out.println("TestAspect Initialized");
}
@Pointcut( "execution(public void doView*(javax.portlet.RenderRequest,javax.portlet.RenderResponse) throws java.io.IOException, javax.portlet.PortletException)" )
private void testDoView() {
}
@Around("testDoView()")
public void logAround(ProceedingJoinPoint joinPoint)
{
System.out.println("AROUND ADVICE START");
try{
joinPoint.proceed(joinPoint.getArgs());
}catch(PortletException portletException){
System.out.println("[AROUND ADVICE] :"+portletExceptionio.getMessgae());
}catch(IOException ioException){
System.out.println("[AROUND ADVICE] :"+ioException.getMessgae());
}
catch(Throwable throwable){
System.out.println("[AROUND ADVICE] :"+throwable.getMessage();
}
System.out.println("AROUND ADVICE EXIT");
}
}
一些* .jar文件是AspectJ二进制文件:
aspectjrt.jar - 在运行时为正确的方面处理所必需的;
aspectjtools.jar - 包含aspectj编译器的实现;
aspectjweaver.jar - aspectj逻辑和java检测之间的桥梁;
1. 在命令行环境(使用Ant-build.xml)
**There are three ways to inject instructions implied by AspectJ aspects:**
<project name="aspectj-example" xmlns:aspectj="antlib:org.aspectj">
<property name="src.dir" value="src/main/java"/>
<property name="resource.dir" value="src/main/resources"/>
<property name="target.dir" value="target"/>
<property name="classes.dir" value="${target.dir}/classes"/>
<taskdef uri="antlib:org.aspectj"
resource="org/aspectj/antlib.xml"
classpath="${resource.dir}/aspectjtools.jar"/>
<path id="aspectj.libs">
<fileset dir="${resource.dir}"/>
</path>
<target name="clean">
<delete dir="${target.dir}"/>
<mkdir dir="${target.dir}"/>
<mkdir dir="${classes.dir}"/>
</target>
way 1: compile-time weaving
<target name="compile-time" depends="clean">
<aspectj:iajc source="1.5" srcdir="${src.dir}" classpathref="aspectj.libs" destDir="${classes.dir}"/>
<java classname="com.aspectj.TestTarget" fork="true">
<classpath>
<path refid="aspectj.libs"/>
<pathelement path="${classes.dir}"/>
</classpath>
</java>
</target>
方式2:编译后编织
<target name="post-compile" depends="clean">
<echo message="Compiling..."/>
<javac debug="true" srcdir="${src.dir}" classpathref="aspectj.libs" destdir="${classes.dir}"/>
<echo message="Weaving..."/>
<aspectj:iajc classpathref="aspectj.libs" inpath="${classes.dir}" aspectpath="${src.dir}" outJar="${classes.dir}/test.jar"/>
</target>
方式3:加载时编织
<target name="load-time" depends="clean">
<echo message="Compiling..."/>
<javac debug="true" srcdir="${src.dir}" classpathref="aspectj.libs" destdir="${classes.dir}"/>
</target>
</project>
2.在Ecilpse(IDE环境)
select your project,than select configure->convert to AspectJ Project.