public interface MyPointcut {
boolean isMatch(Method method);
}
public class LoggingAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// Before method logic
String methodName = methodInvocation.getMethod().getName();
System.out.println("Before invoking method: " + methodName);
// Invoke the target method
Object result = methodInvocation.proceed();
// After method logic
System.out.println("After invoking method: " + methodName);
return result;
}
}
public class LoggingAspect implements MyPointcut, Advice {
private MyPointcut pointcut;
private Advice advice;
public LoggingAspect(MyPointcut pointcut, Advice advice) {
this.pointcut = pointcut;
this.advice = advice;
}
@Override
public boolean isMatch(Method method) {
return pointcut.isMatch(method);
}
@Override
public Advice getAdvice() {
return advice;
}
}
public class UserService {
public void registerUser(User user) {
// Registration logic
System.out.println("User registered successfully");
}
}
public class ProductService {
public void sellProduct(Product product) {
// Selling logic
System.out.println("Product sold successfully");
}
}
<bean id="myPointcut" class="com.example.MyPointcutImpl" />
<bean id="loggingAdvice" class="com.example.LoggingAdvice" />
<bean id="loggingAspect" class="com.example.LoggingAspect">
<constructor-arg ref="myPointcut" />
<constructor-arg ref="loggingAdvice" />
</bean>
<aop:config>
<aop:aspect ref="loggingAspect">
<aop:around method="invoke" pointcut-ref="myPointcut" />
</aop:aspect>
</aop:config>
- SpringSource Org Aopalliance: https://github.com/spring-projects/aopalliance
- AOPalliance: http://aopalliance.sourceforge.net/