記錄一下使用注解實現spring AOP的小例子。
第一步,導入相關的jar包:aspectjweaver-1.6.8.jar(提供注解org.aspectj.lang.annotation.Aspect等)、spring-aop-3.0.4.RELEASE.jar(提供自動代理創建器org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)、aopalliance-1.0.jar(提供攔截器功能)。
第二步,配置applicationContext.xml
01.<?xml version="1.0" encoding="UTF-8"?> 02.<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 03. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 04. xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 05. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 06. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 07. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 08. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 09. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 10. <!--其他配置在此省略...--> 11. <!--配置aop自動創建代理--> 12. <aop:aspectj-autoproxy/> 13. <bean id="userManager" class="com.service.Impl.UserManagerImpl"></bean> 14. <bean class="com.util.SecurityHandler"></bean> 15.</beans>
其中相關AOP的配置包括:
1、xmlns:aop="http://www.springframework.org/schema/aop"
2、xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 其中XSD的版本可以在spring-aop.jar中查看
3、<aop:aspectj-autoproxy/>
4、<bean class="com.util.SecurityHandler"></bean>在spring容器中添加切面類bean
含有連接點的類UserManager
01.package com.service;
02.
03.import com.entity.User;
04.
05.public interface UserManager {
06. public void add(User user);
07.}
08.
09.package com.service.Impl;
10.
11.import org.apache.commons.logging.Log;
12.import org.apache.commons.logging.LogFactory;
13.
14.import com.entity.User;
15.import com.service.UserManager;
16.
17.public class UserManagerImpl implements UserManager {
18. private Log log = LogFactory.getLog(UserManagerImpl.class);
19.
20. public void add(User user) {
21. log.debug("add User:" + user.getUsername());
22. }
23.}
切面實現類SecurityHandler.java
01.package com.util;
02.
03.import org.aspectj.lang.annotation.After;
04.import org.aspectj.lang.annotation.Aspect;
05.import org.aspectj.lang.annotation.Before;
06.
07.@Aspect
08.public class SecurityHandler {
09. // 在執行指定方法前執行
10. @Before("execution(* add*(..))")
11. private void checkSecurity() {
12. System.out.println("添加前檢查通過");
13. }
14.
15. // 在執行指定方法後執行
16. @After("execution(* add*(..))")
17. private void checkResult() {
18. System.out.println("添加後檢查通過");
19. }}
測試方法:
01.public static void main(String[] args) {
02. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
03. UserManager userManager = (UserManager) context.getBean("userManager");
04.
05. User user = new User();
06. user.setUsername("aaa");
07. user.setPassword("bbb");
08.
09. userManager.add(user);
10. }
執行結果如下:
添加前檢查通過
2011-03-11 16:13:21,562 [main] DEBUG [com.service.Impl.UserManagerImpl] - add User:aaa
添加後檢查通過
總結:在搭建示例過程中遇到
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException異常,提示無法獲取spring-aop-3.0.xsd文件,後來發現是缺少spring-aop-3.0.4.RELEASE.jar,因為spring-aop-3.0.xsd在它裡面。