1.对AOP的理解 —— 分工来做各个部分,运行时候整合的思想
2.理解 面向过程,面向对象,面向切面 的思想
1)面向过程:房间装修时,准备装一个灯,就拉一根电线,连接灯。
2)面向对象:设计房间中哪些位置需要使用电线接口,然后在相应的位置设置电线接口,以备以后使用。
3)面向切面:装修房子,先设计需要在哪些地方装上电线接口,就将电线接口先设置好并且不打开接口,此处即为连接点,当此处电线切口确实需要使用时将接口打开插电器即为切入点。
方面:功能(登陆 日志)
目标:主要方面(登陆)
切面:切入点 植入 通知的综合体
连接点:可以插入副功能(日志)的地方
切入点:准备插入副功能的地方
通知:对副功能的封装对象
植入:将通知插入切入点
3.实现登陆和日志管理(使用Spring AOP)
1)LoginService LogService TestMain
2)用Spring 管理 LoginService 和 LogService 的对象
3)确定哪些连接点是切入点,在配置文件中
4)将LogService封装为通知
5)将通知植入到切入点
6)客户端调用目标
1. <</span>aop:config>
2. <</span>aop:pointcutexpression="execution(*cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
3. <</span>!--将哪个-->
4. <</span>aop:aspectid="dd" ref="logService">
5. <</span>aop:beforemethod="log" pointcut-ref="myPointcut"/>
6. <</span>/aop:aspect>
7. <</span>/aop:config>
execution(* * cn.com.spring.service.impl.*.*(..))
1)* 所有的修饰符
2)* 所有的返回类型
3)* 所有的类名
4)* 所有的方法名
5)* ..所有的参数名
1.ILoginService.java
1. package cn.com.spring.service;
2.
3. public interface ILoginService {
4. public boolean login(String userName, String password);
5. }
2.LoginServiceImpl.java
1. package cn.com.spring.service.impl;
2.
3. import cn.com.spring.service.ILoginService;
4.
5. public class LoginServiceImpl implements ILoginService {
6.
7. public boolean login(String userName, String password) {
8. System.out.println("login:" + userName + "," + password);
9. return true;
10. }
11.
12. }
3.ILogService.java
1. package cn.com.spring.service;
2.
3. import org.aspectj.lang.JoinPoint;
4.
5. public interface ILogService {
6. //无参的日志方法
7. public void log();
8. //有参的日志方法
9. public void logArg(JoinPoint point);
10. //有参有返回值的方法
11. public void logArgAndReturn(JoinPoint point,Object returnObj);
12. }
4.LogServiceImpl.java
1. package cn.com.spring.service.impl;
2.
3. import org.aspectj.lang.JoinPoint;
4.
5. import cn.com.spring.service.ILogService;
6.
7. public class LogServiceImpl implements ILogService {
8.
9. @Override
10. public void log() {
11. System.out.println("*************Log*******************");
12. }
13.
14. //有参无返回值的方法
15. public void logArg(JoinPoint point) {
16. //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
17. Object[] args = point.getArgs();
18. System.out.println("目标参数列表:");
19. if (args != null) {
20. for (Object obj : args) {
21. System.out.println(obj + ",");
22. }
23. System.out.println();
24. }
25. }
26.
27. //有参并有返回值的方法
28. public void logArgAndReturn(JoinPoint point, Object returnObj) {
29. //此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
30. Object[] args = point.getArgs();
31. System.out.println("目标参数列表:");
32. if (args != null) {
33. for (Object obj : args) {
34. System.out.println(obj + ",");
35. }
36. System.out.println();
37. System.out.println("执行结果是:" + returnObj);
38. }
39. }
40. }
5.applicationContext.java
1. <</span>?xml version="1.0" encoding="UTF-8"?>
2. <</span>beansxmlns="http://www.springframework.org/schema/beans"
3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:p="http://www.springframework.org/schema/p"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans
6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7. http://www.springframework.org/schema/aop
8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
9.
10. <</span>beanid="logService" class="cn.com.spring.service.impl.LogServiceImpl"><</span>/bean>
11. <</span>beanid="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"><</span>/bean>
12.
13. <</span>aop:config>
14. <</span>!--切入点 -->
15. <</span>aop:pointcut
16. expression="execution(*cn.com.spring.service.impl.LoginServiceImpl.*(..))"
17. id="myPointcut" />
18. <</span>!--切面: 将哪个对象中的哪个方法,织入到哪个切入点 -->
19. <</span>aop:aspectid="dd" ref="logService">
20. <</span>!--前置通知
21. <</span>aop:beforemethod="log" pointcut-ref="myPointcut" />
22. <</span>aop:aftermethod="logArg" pointcut-ref="myPointcut">
23. -->
24. <</span>aop:after-returningmethod="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
25. <</span>/aop:aspect>
26. <</span>/aop:config>
27. <</span>/beans>
6.TestMain.java
1. publicclass TestMain {
2. public static void testSpringAOP(){
3. ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
4.
5. ILoginServiceloginService = (ILoginService)ctx.getBean("loginService");
6. loginService.login("zhangsan", "12344");
7. }
8. public staticvoid main(String[] args) {
9. testSpringAOP();
10. }
11. }
7.输出结果:
- login:zhangsan,12344
- 目标参数列表:
- ,
- ,
- 执行结果是:true
解析:1.先调用了login()方法System.out.println("login:" + userName + "," +password);
2.再调用了logArgAndReturn()方法输出了日志,并且返回了login()方法是否成功
1. System.out.println("目标参数列表:");
2. if (args != null) {
3. for (Object obj : args) {
4. System.out.println(obj + ",");
5. }
6. System.out.println();
7. System.out.println("执行结果是:" + returnObj);
8. }