package id.web.mige.wlsp.bbs; import org.aspectj.lang.ProceedingJoinPoint; public class SecurityAspect { public Object aroundFunction(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Before " + pjp.getSignature().getName()); Object retVal = pjp.proceed(); System.out.println("After " + pjp.getSignature().getName()); return retVal; } }
Setting untuk AOP bisa di masukkan ke dalam berkas XML untuk servlet context, misalnya :
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="id.web.mige.wlsp.bbs" /> <beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <beans:property name="jndiName" value="jdbc/localtestdb"/> </beans:bean> <beans:bean id="articleJDBCTemplate" class="id.web.mige.wlsp.bbs.ArticleJDBCTemplate"> <beans:property name="dataSource" ref="dataSource" /> </beans:bean> <aop:aspectj-autoproxy/> <beans:bean id="articleController" class="id.web.mige.wlsp.bbs.ArticleController"/> <beans:bean id="myAspect" class="id.web.mige.wlsp.bbs.SecurityAspect"> <!-- configure properties of aspect here as normal --> </beans:bean> <aop:config proxy-target-class="true"> <aop:aspect ref="myAspect"> <aop:pointcut id="mypointcut" expression="execution(* id.web.mige.wlsp.bbs.ArticleController.*(..))"/> <aop:around pointcut-ref="mypointcut" method="aroundFunction"/> </aop:aspect> </aop:config> </beans:beans>
Atau jika lebih menyukai bentuk annotation, kelas Aspect dapat diubah menjadi seperti ini :
@Aspect public class SecurityAspect { @Around("execution(* id.web.mige.wlsp.bbs.ArticleController.*(..))") public Object aroundFunction(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Before " + pjp.getSignature().getName()); Object retVal = pjp.proceed(); System.out.println("After " + pjp.getSignature().getName()); return retVal; } }
dan setting/config XML untuk tag