<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- bean definition & AOP specific configuration --> </beans>
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect"> .... </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> .... </aop:aspect> </aop:config>Here "loggingAspect" will be configured and dependency injected just like any other Spring bean as you have seen in previous chapters.
<aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <aop:pointcut expression="execution(* com.sdnext.aop.service.*.*(..))" id="businessService"> </aop:pointcut> ... </aop:aspect> </aop:config> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect"> .... </bean>The following example defines a pointcut named 'businessService' that will match the execution of all getter as like getName() etc method available in Circle class under the package com.sdnext.aop.tutorial:
<aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <aop:pointcut expression="execution(* com.sdnext.aop.tutorial.Circle.getName(..))" id="businessService"></aop:pointcut> .... </aop:aspect> </aop:config> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect"> .... </bean>
<aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <aop:pointcut expression="execution(* com.sdnext.aop.service.*.*(..))" id="businessService"> </aop:pointcut> <!-- a before advice definition --> <aop:before method="someRequiredTask" pointcut-ref="businessService"> ..... </aop:before> <!-- an after advice definition --> <aop:after method="someRequiredTask" pointcut-ref="businessService"> ...... </aop:after> <!-- an after-returning advice definition --> <!--The someRequiredTask method must have parameter named retVal --> <aop:after-returning method="someRequiredTask" pointcut-ref="businessService" returning="retVal"> ..... </aop:after-returning> <!-- an after-throwing advice definition --> <!--The someRequiredTask method must have parameter named ex --> <aop:after-throwing method="someRequiredTask" pointcut-ref="businessService" throwing="ex"> .... </aop:after-throwing> <!-- an around advice definition --> <aop:around method="someRequiredTask" pointcut-ref="businessService"> ... </aop:around> ..... </aop:aspect> </aop:config> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> .... </bean>You can use same someRequiredTask or different methods for different advices. These methods will be defined as a part of aspect module.
<aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <aop:after method="stringArgumentsMethods" pointcut="args(name)"></aop:after> <aop:after-returning method="afterReturningAdvice" pointcut="args(name)" returning="returnString"></aop:after-returning> <aop:after-throwing method="exceptionMethod" pointcut="args(name)" throwing="ex"> </aop:after-throwing> <aop:around method="myArroundMethod" pointcut="within(com.sdnext.aop.tutorial.model.Circle)"></aop:around> <aop:before method="loggingAdvice" pointcut="execution(public * get*(..))"></aop:before> </aop:aspect> </aop:config> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> .... </bean>
package com.sdnext.aop.aspectj.tutorial; public interface Employee{ void addEmployee(); String addEmployeeReturnValue(); void addEmployeeThrowException() throws Exception; void addEmployeeAround(String name); }
package com.sdnext.aop.tutorial.aspectJ; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggingAspect { //Joint point means a place where advice are applied to target methods @Before("execution(com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))") public void loggingBoforeAdvice(JoinPoint joinPoint) { System.out.println(joinPoint.toString()); //.... //.... System.out.println("Advice run. ADD method is called"); }
<!-- Aspect --> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <!-- @Before --> <aop:pointcut expression="execution(*com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))" id="pointCutBefore"></aop:pointcut> <aop:before method="loggingBoforeAdvice" pointcut-ref="pointCutBefore"></aop:before> </aop:aspect> </aop:config>
package com.sdnext.aop.tutorial.aspectJ; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.After; @Aspect public class LoggingAspect { //Joint point means a place where advice are applied to target methods @After("execution(com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))") public void loggingAfterAdvice(JoinPoint joinPoint) { System.out.println(joinPoint.toString()); //.... //.... System.out.println("Advice run. ADD method is called"); }
<!-- Aspect --> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <!-- @After --> <aop:pointcut expression="execution(*com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))" id="pointCutAfter"></aop:pointcut> <aop:before method="loggingAfterAdvice" pointcut-ref="pointCutAfter"></aop:before> </aop:aspect> </aop:config>
package com.sdnext.aop.tutorial.aspectJ; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterReturning; @Aspect public class LoggingAspect { //Joint point means a place where advice are applied to target methods @AfterReturning( pointcut = "execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeReturnValue(..))", returning= "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { //... } }
<!-- Aspect --> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <!-- @AfterReturning --> <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeReturnValue(..))" id="pointCutAfterReturning"></aop:pointcut> <aop:after-returning method="logAfterReturning" pointcut-ref="pointCutAfterReturning" returning="result"> </aop:after-returning> </aop:aspect> </aop:config>
package com.sdnext.aop.tutorial.aspectJ; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class LoggingAspect { //Joint point means a place where advice are applied to target methods @AfterThrowing( pointcut = "execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeThrowException(..))", throwing= "error") public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { //... } }Its Equivalent functionality writing in XML, with <aop:after-throwing>.
<!-- Aspect --> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <!-- @AfterThrowing --> <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeThrowException(..))" id="pointCutAfterThrowing"></ aop:pointcut> <aop:after-thrwoing method="logAfterThrowing" pointcut-ref="pointCutAfterThrowing" throwing="error"></aop:after-thrwoing> </aop:pointcut></aop:aspect> </aop:config>
package com.sdnext.aop.tutorial.aspectJ; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Around; @Aspect public class LoggingAspect { //Joint point means a place where advice are applied to target methods @Around("execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeAround(..))") public void logAround(ProceedingJoinPoint joinPoint) throws Throwable { //... } }Its Equivalent functionality writing in XML, with <aop:around>.
<!-- Aspect --> <bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect"> </bean> <aop:config> <aop:aspect id="loggingAspect" ref="loggingAspect"> <!-- @Around --> <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeAround(..))" id="pointCutAround"></aop:pointcut> <aop:around method="logAround" pointcut-ref="pointCutAround"></aop:around> </aop:aspect> </aop:config>
Step | Description |
---|---|
1 | Create a java project with a name SpringAOPDemo and create a package com.dineshonjava.sdnext.aop.aspectJ under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs or User Liberaries option . |
3 | Add Sppring AOP secific libraries aspectjrt.jar, aspectjweaver.jar and aspectj.jar in the project. |
4 | Create Java classes LoggingAspect, Employee and AopMain under the com.dineshonjava.sdnext.aop.aspect, com.dineshonjava.sdnext.aop.emp, com.dineshonjava.sdnext.aop package respectively. |
5 | Create Beans configuration file spring.xml under the src folder. |
6 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
package com.dineshonjava.sdnext.aop.aspect; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class LoggingAspect { public void logBeforeAdvice(JoinPoint joinPoint) { System.out.println("logBeforeAdvice() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("******"); } public void logAfterAdvice(JoinPoint joinPoint) { System.out.println("logAfterAdvice() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("******"); } public void logAfterReturningAdvice(JoinPoint joinPoint, Object result) { System.out.println("logAfterReturningAdvice() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("Method returned value is : " + result); System.out.println("******"); } public void logAfterThrowingAdvice(JoinPoint joinPoint, Throwable error) { System.out.println("logAfterThrowingAdvice() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("Exception : " + error); System.out.println("******"); } public void logAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("logAroundAdvice() is running!"); System.out.println("hijacked method : " + joinPoint.getSignature().getName()); System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs())); System.out.println("Around before is running!"); joinPoint.proceed(); System.out.println("Around after is running!"); System.out.println("******"); } }Employee.java
package com.dineshonjava.sdnext.aop.emp; public interface Employee { void addEmployee(); String addEmployeeReturnValue(); void addEmployeeThrowException() throws Exception; void addEmployeeAround(String name); }EmployeeImpl.java
package com.dineshonjava.sdnext.aop.emp.impl; import com.dineshonjava.sdnext.aop.emp.Employee; public class EmployeeImpl implements Employee { public void addEmployee() { System.out.println("addEmployee() is running "); } public String addEmployeeReturnValue() { System.out.println("addEmployeeReturnValue() is running "); return "abc"; } public void addEmployeeThrowException() throws Exception { System.out.println("addEmployeeThrowException() is running "); throw new Exception("Generic Error"); } public void addEmployeeAround(String name) { System.out.println("addEmployeeAround() is running, args : " + name); } }spring.xml
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean class="com.dineshonjava.sdnext.aop.emp.impl.EmployeeImpl" id="employee"></bean> <aop:aspectj-autoproxy> <!-- Aspect --> <bean class="com.dineshonjava.sdnext.aop.aspect.LoggingAspect" id="logAspect"></bean> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect"> <!-- @Before --> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployee(..))" id="pointCutBefore"></aop:pointcut> <aop:before method="logBeforeAdvice" pointcut-ref="pointCutBefore"></aop:before> <!-- @After --> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployee(..))" id="pointCutAfter"></aop:pointcut> <aop:after method="logAfterAdvice" pointcut-ref="pointCutAfter"></aop:after> <!-- @AfterReturning --> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployeeReturnValue(..))" id="pointCutAfterReturning"></aop:pointcut> <aop:after-returning method="logAfterReturningAdvice" pointcut-ref="pointCutAfterReturning" returning="result"></aop:after-returning> <!-- @AfterThrowing --> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployeeThrowException(..))" id="pointCutAfterThrowing"></aop:pointcut> <aop:after-throwing method="logAfterThrowingAdvice" pointcut-ref="pointCutAfterThrowing" throwing="error"></aop:after-throwing> <!-- @Around --> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployeeAround(..))" id="pointCutAround"></aop:pointcut> <aop:around method="logAroundAdvice" pointcut-ref="pointCutAround"></aop:around> </aop:aspect> </aop:config> </aop:aspectj-autoproxy></beans>AopMain.java
package com.dineshonjava.sdnext.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dineshonjava.sdnext.aop.emp.Employee; public class AopMain { public static void main(String[] args) throws Exception { ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml"); Employee employee = (Employee) appContext.getBean("employee"); employee.addEmployee(); //employee.addEmployeeReturnValue(); //employee.addEmployeeThrowException(); //employee.addEmployeeAround("dinesh"); } }Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:
Labels: Spring AOP, Spring3.0