您的位置:首页 > 编程语言 > Java开发

spring AOP 配置

2016-03-25 16:29 513 查看
需要的JAR包:antlr-2.7.6.jar  aspectjrt.jar  aspectjweaver.jar   cglib-nodep-2.1_3.jar

首先创建一个普通类(主要类)

<span style="font-size:14px;">public class aopServer {

public void sayhello(){
System.out.println("aopServer");
}
}</span>


写一个切面类

<span style="font-size:14px;">public class aopLog {
public void berfor(){
System.out.println("berfor log");
}
}</span>


spring中两种方式配置AOP  一种是annotation,另一种是xml

annotion方式的配置

在spring的配置文件中加入:

<span style="white-space:pre">	</span><!-- 启用注解方式配置AOP -->
<context:annotation-config/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 扫描包-->
<context:component-scan base-package="com.log"></context:component-scan>
在切面类上添加注解
@Aspect  //定义为切面类
@Component  //注入IOC容器
在切面类的对应方法上添加注解@Berfore或@after ,这两个的value值为execution表达式 :execution(public void com.server.aopServer.sayhello())
可以“*” 匹配,“..” 表示方法的任意参数
XML方式配置:
<span style="font-family: Arial, Helvetica, sans-serif;"><aop:config></span>
<aop:aspect id="myAop" ref="log">
<aop:pointcut id="target" expression="execution(* com.server.aopServer.sayhello(..))"/>
<aop:before method="berfor" pointcut-ref="target"/>
</aop:aspect>
</aop:config>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: