Last active
December 19, 2015 18:29
-
-
Save ricston-git/5999075 to your computer and use it in GitHub Desktop.
mule spring aop example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- this is the object that will be proxied by Spring's AOP infrastructure --> | |
<bean id="MyComponentBean" class="org.example.components.MyComponent" /> | |
<!-- this is the actual advice itself --> | |
<bean id="monitor" class="org.example.advice.MyAdvice" /> | |
<aop:config> | |
<aop:aspect ref="monitor"> | |
<aop:pointcut id="runtime-check" | |
expression="execution(* org.example.components.MyComponent.*(..))" /> | |
<aop:around method="injectAround" pointcut-ref="runtime-check" /> | |
<aop:before pointcut-ref="runtime-check" method="interceptBefore" /> | |
<aop:after-returning pointcut-ref="runtime-check" | |
method="interceptAfter" /> | |
<aop:after-throwing pointcut-ref="runtime-check" | |
method="exceptionThrown" /> | |
</aop:aspect> | |
</aop:config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
********************************************************************** | |
* Application: mule-aop * | |
* OS encoding: MacRoman, Mule encoding: UTF-8 * | |
* * | |
* Agents Running: * | |
* Clustering Agent * | |
* JMX Agent * | |
********************************************************************** | |
INFO 2013-07-15 12:12:17,264 [main] org.mule.module.launcher.MuleDeploymentService: | |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
+ Started app 'mule-aop' + | |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
INJECT BEFORE! | |
a message is on its way to MyComponent... | |
inside component... | |
INJECT AFTER! | |
a message is exiting MyComponent... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<spring:beans> | |
<spring:import resource="classpath:org/example/aspects/aop.xml" /> | |
</spring:beans> | |
<flow name="Mule-aopFlow1" doc:name="Mule-aopFlow1"> | |
<http:inbound-endpoint exchange-pattern="request-response" | |
host="localhost" port="8081" doc:name="HTTP" /> | |
<component> | |
<spring-object bean="MyComponentBean" /> | |
</component> | |
</flow> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAdvice { | |
public void interceptBefore() { | |
System.out.println("a message is on its way to MyComponent..."); | |
} | |
public void interceptAfter() { | |
System.out.println("a message is exiting MyComponent..."); | |
} | |
public void exceptionThrown() { | |
System.out.println("an error has been thrown in Mycomponent..."); | |
} | |
public Object injectAround(ProceedingJoinPoint pjp) throws Throwable { | |
// do some logic before | |
System.out.println("INJECT BEFORE!"); | |
// run the actual method | |
Object retVal = null; | |
try { | |
retVal = pjp.proceed(); | |
} catch (Exception e) { | |
} | |
// more logic after method executes | |
System.out.println("INJECT AFTER!"); | |
return retVal; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyComponent implements Callable{ | |
@Override | |
public Object onCall(MuleEventContext eventContext) throws Exception { | |
System.out.println("inside component..."); | |
return eventContext.getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment