Spring AOP
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);
- }
-
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);
-
}
-
}
- }
-
<?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>
- 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();
- }
- }
-
login:zhangsan,12344
-
目標參數列表:
-
zhangsan,
-
12344,
-
- 執行結果是:true
2.再調用了logArgAndReturn()方法輸出了日誌,並且返回了login()方法是否成功
-
System.out.println("目標參數列表:");
-
if (args != null) {
-
for (Object obj : args) {
-
System.out.println(obj + ",");
-
}
-
System.out.println();
-
System.out.println("執行結果是:" + returnObj);
- }