记录一下使用注解实现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
- <?xml version= "1.0" encoding= "UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx= "http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <!--其他配置在此省略...-->
- <!--配置aop自动创建代理-->
- <aop:aspectj-autoproxy/>
- <bean id="userManager" class = "com.service.Impl.UserManagerImpl" ></bean>
- <bean class = "com.util.SecurityHandler" ></bean>
- </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
- package com.service;
- import com.entity.User;
- public interface UserManager {
- public void add(User user);
- }
- package com.service.Impl;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.entity.User;
- import com.service.UserManager;
- public class UserManagerImpl implements UserManager {
- private Log log = LogFactory.getLog(UserManagerImpl. class );
- public void add(User user) {
- log.debug("add User:" + user.getUsername());
- }
- }
切面实现类SecurityHandler.java
- package com.util;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- @Aspect
- public class SecurityHandler {
- // 在执行指定方法前执行
- @Before ( "execution(* add*(..))" )
- private void checkSecurity() {
- System.out.println("添加前检查通过" );
- }
- // 在执行指定方法后执行
- @After ( "execution(* add*(..))" )
- private void checkResult() {
- System.out.println("添加后检查通过" );
- }}
测试方法:
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
- UserManager userManager = (UserManager) context.getBean("userManager" );
- User user = new User();
- user.setUsername("aaa" );
- user.setPassword("bbb" );
- userManager.add(user);
- }
执行结果如下:
添加前检查通过
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在它里面。
来自:http://blog.youkuaiyun.com/cuihaiyang/article/details/6240413