package com.liaoli.service;
public interface UserService {public void add();public void delete();public void update();public void select();
}
package com.liaoli.service;public class UserServiceImpl implements UserService {public void add(){System.out.println("增加了一个用户");};public void delete(){System.out.println("删除了一个用户");};public void update(){System.out.println("修改了一个用户");};public void select(){System.out.println("查询了一个用户");};
}
package com.liaoli.log;import flect.Method;
import org.springframework.aop.MethodBeforeAdvice;public class Log implements MethodBeforeAdvice {public void before(Method method, Object[] args, Object target) throws Throwable {System.out.Class().getName()+"被执行了");}
}
后置日志:AfterLog.java
package com.liaoli.log;import flect.Method;
import org.springframework.aop.AfterReturningAdvice;public class AfterLog implements AfterReturningAdvice {public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("执行了:" + Name() + "返回结果为:" + returnValue);}
}
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="" xmlns:xsi=""xmlns:aop="" xsi:schemaLocation=" .xsd://www.springframework/schema/aop/spring-aop.xsd"><!-- 注册 bean--><bean id="userService" class="com.liaoli.service.UserServiceImpl"/><bean id="log" class="com.liaoli.log.Log"/><bean id="afterLog" class="com.liaoli.log.AfterLog"/><!-- 配置AOP 导入AOP的约束--><aop:config><!--切入点:expression表达式 execution(要执行的位置) --><aop:pointcut id="pointcut" expression="execution(* com.liaoli.service.UserServiceImpl.*(..))"/><aop:advisor advice-ref="log" pointcut-ref="pointcut" /><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut" /></aop:config>
</beans>
package com.liaoli.service;import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext(l");UserService userService = Bean("userService", UserService.class);userService.add();}
}
测试发现,接口的所有方法,都增加了日志功能。
本文发布于:2024-01-31 00:53:57,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170663375124128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |