代码参考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的实现类UserManagerImpl
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

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
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
@Override15
public void delUser(String id) {16
//
添加日志/安全性检查17
//checksafe();18
System.out.println("UserManagerImpl.delUser()");19
}20

21
@Override22
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

1
package com.zyl.proxy;
2
//切入点
3
public interface MySecurityManager {
4
public void checkSafe();
5
}
6
4.MySecurityManager的实现类MySecurityManagerImpl
package com.zyl.proxy;2
//切入点3
public interface MySecurityManager {4
public void checkSafe();5
}6

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
//该方法只是一个标识,切入点的内容是一个表达式.用来描述切入哪些对象的那些方法.
13
14
@Pointcut("execution(* add*(..))") //1. 前一个*表示返回值,接受有/没有返回值的方法;
15
//2. add后面那个*表示参数方法中有参数/没有参数的方法都包含;
16
//execution(* add*(..))即 :以add开头的所有方法,无论有无返回值,有无参数
17
private void allAddMethod(){
18
19
}
20
//定义 通知(Advice),标识在哪些切入点的何处织入此方法
21
22
@Before("allAddMethod()") //就是上面那个方法标识,找到addAddMethod方法的标识即:@Pointcut("execution(* add*(..))")
23
//就会在所有以add开头的所有方法(无论有无返回值,有无参数)前加入以下的checkSafe()方法
24
public void checkSafe() {
25
System.out.println("checkSafe安全性检查");
26
27
28
29
}
30
}
31
//如果还想在别的方法加入作为切入点,比如加入对del开头的方法和add开头的方法都要切入,那么可以这么写:
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
//该方法只是一个标识,切入点的内容是一个表达式.用来描述切入哪些对象的那些方法.13
14
@Pointcut("execution(* add*(..))") //1. 前一个*表示返回值,接受有/没有返回值的方法;15
//2. add后面那个*表示参数方法中有参数/没有参数的方法都包含;16
//execution(* add*(..))即 :以add开头的所有方法,无论有无返回值,有无参数17
private void allAddMethod(){18
19
}20
//定义 通知(Advice),标识在哪些切入点的何处织入此方法21

22
@Before("allAddMethod()") //就是上面那个方法标识,找到addAddMethod方法的标识即:@Pointcut("execution(* add*(..))")23
//就会在所有以add开头的所有方法(无论有无返回值,有无参数)前加入以下的checkSafe()方法24
public void checkSafe() {25
System.out.println("checkSafe安全性检查");26
27

28

29
}30
}31

// @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中添加的东西.
<?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>插入的方法的那个类(好像叫切面)的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
先看看结果吧.让我们见证这无趣的一刻:
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工厂得到UserManager18
UserManager usermanager=(UserManager)factory.getBean("userManagergb23122");19
20
usermanager.addUser("1", "password");21
22
}23

24
}25

1
checkSafe安全性检查
2
UserManagerImpl.addUser()123
P.S:
checkSafe安全性检查2
UserManagerImpl.addUser()123
spring对aop的支持
1.采用annotation的方式(lib下aspectj的jar包)
1)添加spring的依赖库
2)添加AOP支持
定义Aspect
在Aspect中定义Pointcut和Advice
在配置文件中添加<aop:aspectj-autoproxy/>,让spring启用Aspectj的annotation支持
注意:
在这种方法中,切入点的方法是不会执行的,(如MySecurityManagerImpl中的private void allAddMethod()方法是不会执行的,
只是作为一个标识.(类似配置文件中的id))
它存在的目的仅仅是重用接入点的定义.即,可以在Advice中,通过方法名称引用这个切入点.
