<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${aopalliance.version}</version> </dependency> import org.aopalliance.aop.Advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LoggingAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("Method invoked: " + methodInvocation.getMethod().getName()); Object result = methodInvocation.proceed(); System.out.println("Method completed: " + methodInvocation.getMethod().getName()); return result; } } <bean id="loggingAspect" class="com.example.LoggingAspect" /> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.Service.*(..))" /> <aop:around method="invoke" pointcut-ref="serviceMethods" /> </aop:aspect> </aop:config> public class Service { public void doSomething() { System.out.println("Doing something..."); } public void doAnotherThing() { System.out.println("Doing another thing..."); } } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Service service = (Service) context.getBean("service"); service.doSomething(); service.doAnotherThing(); }


上一篇:
下一篇:
切换中文