Java学习路线-58:AOP面向切面编程
【摘要】 AOP 面向切面编程
AOP aspect oriented programming OOP Object oriented programming
提供申明式服务允许用户实现自定义切面
传统编程模式
自上而下,纵向的编程
Jsp ->
Action ->
Service ->
Dao
1234567
AOP 编程: 在不改变原有的代码,增...
AOP 面向切面编程
AOP aspect oriented programming
OOP Object oriented programming
- 提供申明式服务
- 允许用户实现自定义切面
传统编程模式
自上而下,纵向的编程
Jsp ->
Action ->
Service ->
Dao
- 1
- 2
- 3
- 4
- 5
- 6
- 7
AOP 编程:
在不改变原有的代码,增加新的功能
Jsp ->
Action ->
Service <- log() ->
Dao
- 1
- 2
- 3
- 4
- 5
- 6
- 7
好处:
- 使得真实角色处理业务更加纯粹,不再关注公共的问题
- 公共业务由代理类完成,实现业务的分工
- 公共业务发生扩展时变得更加集中和方便
关注点:日志,安全,缓存,事务
切面 Aspect:一个关注点的模块化
实现 AOP
1、通过 Spring 接口实现
依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
package com.spring.service;
public interface UserService { public void add(); public void delete();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
package com.spring.service.impl;
import com.spring.service.UserService;
public class UserServiceImpl implements UserService { @Override public void add() { System.out.println("add"); } @Override public void delete() { System.out.println("delete"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
package com.spring.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
// 前置通知
public class BeforeLog implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName() + ": "+ method.getName()); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
package com.spring.aop;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
// 后置通知
public class AfterLog implements AfterReturningAdvice { @Override public void afterReturning( Object result, Method method, Object[] objects, Object target) throws Throwable { System.out.println(target.getClass().getName() + ": "+ method.getName()); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="service" class="com.spring.service.impl.UserServiceImpl"/> <bean id="beforeLog" class="com.spring.aop.BeforeLog"/> <bean id="AfterLog" class="com.spring.aop.AfterLog"/> <aop:config> <aop:pointcut id="action" expression="execution(* com.spring.service.impl.UserServiceImpl.* (..))"/> <aop:advisor advice-ref="beforeLog" pointcut-ref="action"/> <aop:advisor advice-ref="AfterLog" pointcut-ref="action"/> </aop:config>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
package com.spring.test;
import com.spring.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService) context.getBean("service"); service.add(); service.delete(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
执行结果
com.spring.service.impl.UserServiceImpl: add
add
com.spring.service.impl.UserServiceImpl: add
com.spring.service.impl.UserServiceImpl: delete
delete
com.spring.service.impl.UserServiceImpl: delete
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Spring AOP 本质
就是讲公共的业务(如日期,安全等)和领域业务结合,
当执行领域业务时将会把公共业务加进来,
实现公共业务的重复利用,
领域业务更纯粹,可以专注于领域业务,
本质是动态代理
2、自定义类实现 AOP
UserService、UserServiceImpl、Demo 三个类不变
添加 Log 类和修改配置文件
package com.spring.aop;
public class Log { public void before(){ System.out.println("--before--"); } public void after(){ System.out.println("--after--"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="service" class="com.spring.service.impl.UserServiceImpl"/> <bean id="log" class="com.spring.aop.Log"/> <aop:config> <aop:aspect ref="log"> <aop:pointcut id="action" expression="execution(* com.spring.service.impl.UserServiceImpl.* (..))"/> <aop:before method="before" pointcut-ref="action"/> <aop:after method="after" pointcut-ref="action"/> </aop:aspect> </aop:config>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
执行结果
--before--
add
--after--
--before--
delete
--after--
- 1
- 2
- 3
- 4
- 5
- 6
3、注解实现 AOP
业务类不改变
package com.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class Log { @Before("execution(* com.spring.service.impl.UserServiceImpl.* (..))") public void before(){ System.out.println("--before--"); } @After("execution(* com.spring.service.impl.UserServiceImpl.* (..))") public void after(){ System.out.println("--after--"); } @Around("execution(* com.spring.service.impl.UserServiceImpl.* (..))") public Object Around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("--Around before--"); Object result = pjp.proceed(); System.out.println("--Around after--"); return result; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="service" class="com.spring.service.impl.UserServiceImpl"/> <bean id="log" class="com.spring.aop.Log"/> <aop:aspectj-autoproxy />
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
执行结果
--Around before--
--before--
add
--Around after--
--after--
--Around before--
--before--
delete
--Around after--
--after--
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
总结
公共业务:
日志,安全,权限,缓存,事务
分离思想
在不改变原有代码的情况下,增加额外的功能
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/106889187
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)