Spring_007_AOP_Annotation
来源:互联网 发布:网络棋牌赌博 如何判刑 编辑:程序博客网 时间:2024/05/03 20:48
在Spring中使用AOP的步骤如下:
1、首先在xml文件中添加如下配置:
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
<?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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><context:annotation-config/><context:component-scan base-package="com.jimmy" /><aop:aspectj-autoproxy /> <bean id="userService" class="com.jimmy.service.UserService" > </bean> </beans>
2、导入aspectj的2个jar包:aspectjrt.jar和aspectjweaver.jar
3、xml文件通过该标签: <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 来告知Spring要使用AOP
4、在切面的类标注@Aspect与@Component,并在方法前标注@Before
package com.jimmy.aop;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LogInterceptor {@Before("execution(public void com.jimmy.dao.imp.UserOracleDao.add(com.jimmy.model.User))")public void logBefore(){System.out.println("Log start....");}}
Spring在执行的时候通过标签得知要使用AOP,因此找到使用@Aspect标签的类,并将这个类作为切面,而且通过@Before 方法指导在执行UserOracleDao.add方法之前执行logBefore方法(该切面方法必须要使用@Component,否则Spring无法为其初始化,只有都交由Spring来初始化才可以继续使用AOP)。
- Spring_007_AOP_Annotation
- 每日一句 (November)
- FPGA Verilog HDL 系列实例--------序列信号发生器
- Yii Framework 开发教程(4) Hangman 猜单词游戏实例
- 转载:view 设置阴影+圆角
- ios开发学习--objective-c学习--系列教程1
- Spring_007_AOP_Annotation
- android
- Flex 4 - TitleWindow + Display Icon
- JAVA 日期加减
- SimpleDateFormat 12小时制以及24小时制的写法
- linux常用命令及vi
- 将excel文件导入到dataset中
- 堆区分配方法
- Python socket模块编程