public class LoggingAspect implements Aspect {
public void before() {
System.out.println("Before method execution: Logging...");
}
public void after() {
System.out.println("After method execution: Logging...");
}
}
<bean id="loggingAspect" class="com.example.LoggingAspect" />
<aop:aspectj-autoproxy>
<aop:include name="loggingAspect" />
<aop:pointcut id="samplePointcut" expression="execution(* com.example.SampleClass.*(..))" />
<aop:before pointcut-ref="samplePointcut" method="before" />
<aop:after pointcut-ref="samplePointcut" method="after" />
</aop:aspectj-autoproxy>
public class SampleClass {
public void doSomething() {
System.out.println("Doing something...");
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SampleClass sample = context.getBean(SampleClass.class);
sample.doSomething();
}
}