在之前那個動態(tài)代理的基礎(chǔ)上做些修改.
代碼參考dynamic-proxy-AOP1
1.首先呢接口類UserManager
1
package com.zyl.proxy;
2
3
public interface UserManager
{
4
5
public void addUser(String name,String password);
6
7
public void delUser(String id);
8
9
public void modifyUser(int id, String name, String password);
10
11
}
12
2.UserManager的實現(xiàn)類UserManagerImpl
1
package com.zyl.proxy;
2
3
public class UserManagerImpl implements UserManager
{
4
5
6
public void addUser(String name, String password)
{
7
//
添加日志/安全性檢查
8
//checksafe();
9
//采用添加代理類的方法會如何?
10
System.out.println("UserManagerImpl.addUser()123");
11
12
}
13
14
@Override
15
public void delUser(String id)
{
16
//
添加日志/安全性檢查
17
//checksafe();
18
System.out.println("UserManagerImpl.delUser()");
19
}
20
21
@Override
22
public void modifyUser(int id, String name, String password)
{
23
//
添加日志/安全性檢查
24
//checksafe();
25
System.out.println("UserManagerImpl.modifyUser()");
26
27
}
28
// private void checksafe(){
29
// System.out.println("檢查安全性的方法");
30
// }
31
}
32
3.寫接口MySecurityManager
1
package com.zyl.proxy;
2
//切入點
3
public interface MySecurityManager
{
4
public void checkSafe();
5
}
6
4.MySecurityManager的實現(xiàn)類MySecurityManagerImpl
1
package com.zyl.proxy;
2
import org.aspectj.lang.annotation.Aspect;
3
import org.aspectj.lang.annotation.Before;
4
import org.aspectj.lang.annotation.Pointcut;
5
//spring AOP:采用annotation的方式
6
//定義 切面(Aspect):
7
@Aspect //聲明
8
public class MySecurityManagerImpl implements MySecurityManager
{
9
10
11
//定義切入點(Pointcut),切入點的名稱allAddMethod,該方法不能有返回值,
12
//該方法只是一個標(biāo)識,切入點的內(nèi)容是一個表達(dá)式.用來描述切入哪些對象的那些方法.
13
14
@Pointcut("execution(* add*(..))") //1. 前一個*表示返回值,接受有/沒有返回值的方法;
15
//2. add后面那個*表示參數(shù)方法中有參數(shù)/沒有參數(shù)的方法都包含;
16
//execution(* add*(..))即 :以add開頭的所有方法,無論有無返回值,有無參數(shù)
17
private void allAddMethod()
{
18
19
}
20
//定義 通知(Advice),標(biāo)識在哪些切入點的何處織入此方法
21
22
@Before("allAddMethod()") //就是上面那個方法標(biāo)識,找到addAddMethod方法的標(biāo)識即:@Pointcut("execution(* add*(..))")
23
//就會在所有以add開頭的所有方法(無論有無返回值,有無參數(shù))前加入以下的checkSafe()方法
24
public void checkSafe()
{
25
System.out.println("checkSafe安全性檢查");
26
27
28
29
}
30
}
31
//如果還想在別的方法加入作為切入點,比如加入對del開頭的方法和add開頭的方法都要切入,那么可以這么寫:
// @Pointcut("execution(* add*(..))||execution(* del*(..))")
5.配置文件
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xmlns:context="http://www.springframework.org/schema/context"
5
xmlns:tx="http://www.springframework.org/schema/tx"
6
xmlns:aop="http://www.springframework.org/schema/aop"
7
xsi:schemaLocation="http://www.springframework.org/schema/beans
8
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9
http://www.springframework.org/schema/context
10
http://www.springframework.org/schema/context/spring-context-2.5.xsd
11
http://www.springframework.org/schema/aop
12
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
13
http://www.springframework.org/schema/tx
14
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
15
16
17
<aop:aspectj-autoproxy/> <!-- 啟用aspectj對aop的支持 -->
18
19
20
<bean id="mySecurityManagerImpl" class="com.zyl.proxy.MySecurityManagerImpl"/>
21
<bean id="userManagergb23122" class="com.zyl.proxy.UserManagerImpl"/>
22
</beans>
注意beans中添加的東西.
插入的方法的那個類(好像叫切面)的bean id名稱好像也用不到,隨便取就好了.
而,UserManagerImpl的bean id在client中要用到.不要亂取.
6.client
1
package com.zyl.ooxx;
2
3
import org.springframework.beans.factory.BeanFactory;
4
import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6
import com.zyl.proxy.UserManager;
7
8
9
10
public class client
{
11
12
public static void main(String[] args)
{
13
//所有對象要從ioc中去取,所以之前這些對象要在xml中注冊
14
15
//通過配置文件初始化bean工廠
16
BeanFactory factory =new ClassPathXmlApplicationContext("applicationContext.xml");
17
//通過bean工廠得到UserManager
18
UserManager usermanager=(UserManager)factory.getBean("userManagergb23122");
19
20
usermanager.addUser("1", "password");
21
22
}
23
24
}
25
先看看結(jié)果吧.讓我們見證這無趣的一刻:
1
checkSafe安全性檢查
2
UserManagerImpl.addUser()123
P.S:
spring對aop的支持
1.采用annotation的方式(lib下aspectj的jar包)
1)添加spring的依賴庫
2)添加AOP支持
定義Aspect
在Aspect中定義Pointcut和Advice
在配置文件中添加<aop:aspectj-autoproxy/>,讓spring啟用Aspectj的annotation支持
注意:
在這種方法中,切入點的方法是不會執(zhí)行的,(如MySecurityManagerImpl中的private void allAddMethod()方法是不會執(zhí)行的,
只是作為一個標(biāo)識.(類似配置文件中的id))
它存在的目的僅僅是重用接入點的定義.即,可以在Advice中,通過方法名稱引用這個切入點.