Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Last active December 19, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricston-git/5999075 to your computer and use it in GitHub Desktop.
Save ricston-git/5999075 to your computer and use it in GitHub Desktop.
mule spring aop example
<!-- 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>
**********************************************************************
* 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...
<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>
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;
}
}
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