转至:http://blog.chinaunix.net/uid-26959955-id-3275282.html
1.对AOP的理解 —— 分工来做各个部分,运行时候整合的思想
2.理解 面向过程,面向对象,面向切面 的思想
1)面向过程:房间装修时,准备装一个灯,就拉一根电线,连接灯。
2)面向对象:设计房间中哪些位置需要使用电线接口,然后在相应的位置设置电线接口,以备以后使用。
3)面向切面:装修房子,先设计需要在哪些地方装上电线接口,就将电线接口先设置好并且不打开接口,此处即为连接点,当此处电线切口确实需要使用时将接口打开插电器即为切入点。
方面:功能(登陆 日志)
目标:主要方面(登陆)
切面:切入点 植入 通知的综合体
连接点:可以插入副功能(日志)的地方
切入点:准备插入副功能的地方
通知:对副功能的封装对象
植入:将通知插入切入点
3.实现登陆和日志管理(使用Spring AOP)
1)LoginService LogService TestMain
2)用Spring 管理 LoginService 和 LogService 的对象
3)确定哪些连接点是切入点,在配置文件中
4)将LogService封装为通知
5)将通知植入到切入点
6)客户端调用目标
-
<aop:config>
- <aop:pointcut expression="execution(* cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
- <!--将哪个-->
- <aop:aspect id="dd" ref="logService">
- <aop:before method="log" pointcut-ref="myPointcut"/>
- </aop:aspect>
-
</aop:config>
execution(* * cn.com.spring.service.impl.*.*(..))
1)* 所有的修饰符
2)* 所有的返回类型
3)* 所有的类名
4)* 所有的方法名
5)* ..所有的参数名
1.ILoginService.java
-
package cn.com.spring.service;
-
public interface ILoginService {
- public boolean login(String userName, String password);
- }
2.LoginServiceImpl.java
-
package cn.com.spring.service.impl;
-
import cn.com.spring.service.ILoginService;
-
public class LoginServiceImpl implements ILoginService {
- public boolean login(String userName, String password) {
- System.out.println("login:" + userName + "," + password);
- return true;
- }
- }
3.ILogService.java
-
package cn.com.spring.service;
-
import org.aspectj.lang.JoinPoint;
-
public interface ILogService {
- //无参的日志方法
- public void log();
- //有参的日志方法
- public void logArg(JoinPoint point);
- //有参有返回值的方法
- public void logArgAndReturn(JoinPoint point,Object returnObj);
- }
4.LogServiceImpl.java
-
package cn.com.spring.service.impl;
-
import org.aspectj.lang.JoinPoint;
-
import cn.com.spring.service.ILogService;
-
public class LogServiceImpl implements ILogService {
- @Override
- public void log() {
- System.out.println("*************Log*******************");
- }
-
- //有参无返回值的方法
- public void logArg(JoinPoint point) {
- //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
- Object[] args = point.getArgs();
- System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- }
- }
- //有参并有返回值的方法
- public void logArgAndReturn(JoinPoint point, Object returnObj) {
- //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
- Object[] args = point.getArgs();
- System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- System.out.println("执行结果是:" + returnObj);
- }
- }
- }
5.applicationContext.java
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="logService" class="cn.com.spring.service.impl.LogServiceImpl"></bean>
- <bean id="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"></bean>
- <aop:config>
- <!-- 切入点 -->
- <aop:pointcut
- expression="execution(* cn.com.spring.service.impl.LoginServiceImpl.*(..))"
- id="myPointcut" />
- <!-- 切面: 将哪个对象中的哪个方法,织入到哪个切入点 -->
- <aop:aspect id="dd" ref="logService">
- <!-- 前置通知
- <aop:before method="log" pointcut-ref="myPointcut" />
- <aop:after method="logArg" pointcut-ref="myPointcut">
-
-->
- <aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
- </aop:aspect>
- </aop:config>
-
</beans>
6.TestMain.java
- public class TestMain {
-
public static void testSpringAOP(){
- ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
-
- ILoginService loginService = (ILoginService)ctx.getBean("loginService");
- loginService.login("zhangsan", "12344");
- }
- public static void main(String[] args) {
- testSpringAOP();
- }
- }
7.输出结果:
-
login:zhangsan,12344
- 目标参数列表:
- zhangsan,
- 12344,
- 执行结果是:true
解析:1.先调用了login()方法System.out.println("login:" + userName + "," + password);
2.再调用了logArgAndReturn()方法输出了日志,并且返回了login()方法是否成功
-
System.out.println("目标参数列表:");
- if (args != null) {
- for (Object obj : args) {
- System.out.println(obj + ",");
- }
- System.out.println();
- System.out.println("执行结果是:" + returnObj);
- }
分享到:
相关推荐
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,swagger和spring Aop日志结合 ,
spring aop做的日志管理,网上看的,没测试过。
结合logback和切面对参数名和参数值,方法运行时间,方法返回值记录日志,pom中需要添加ogback和切面依赖。
本资源用来展示如何使用 spring aop 进行日志记录,例子里面通过aop的配置,把产生的日志存放到当前项目的根目录下,而且对方法执行过程中的参数进行了记录,对于aop如何记录日志不清楚的同学可以看看。
采用SpringAOP拦截Controller,Service实现操作日志管理,统一处理异常,登陆日志管理,是SpringAOP的应用实践。通过SpringAOP的处理,可以方便移植日志管理功能,是个不错的学习demo
spring mvc利用aop日志实现对controller层的拦截
各种切面编写方式,简单方便的完成切点打印日志,和执行其他方法少时诵诗书所
spring AOP基于注解来实现业务日志管理
spring AOP 切面日志 分层打日志
4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、...
aop日志.doc
AOP技术则是将日志记录和安全检测代码封装为方法,在处理业务逻辑之前分别调用已经封装好的方法即可,有效解决重复性代码的问题,并为程序开发、调试带来极大方便
基于Spring AOP的日志与测试源码 不包含jar包
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
利用自定义注解和spring aop和java反射机制生成用户能够读懂的日志记录。如:用户张三在2013年9月27日17:00执行了用户管理模块的用户删除功能参数为(编号:123456)各位可根据需要写入数据库或者保存到文件。
可扩展性强:使用 Spring AOP 进行方法耗时监测,可以方便的对其他的切面进行扩展,例如:日志、异常处理、权限控制等切面。 4. 提高系统性能:通过对系统中的方法进行耗时监测,可以及时发现系统中的性能瓶颈,...
此工程为使用eclipse创建java project,使用jdk1.8,项目中包含完整的代码和jar包,导入eclipse即可运行
spring aop实现日志功能 开发技术-其它
使用Spring的AOP技术,实现日志记录功能。记录如下信息: 在哪个时间访问了哪个方法,方法传入了哪些参数