ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)

 

感谢ITCAST发布的免费视频

 

使用Spring进行面向切面(AOP)编程

要进行AOP编程,首先我们要在Spring的配置文件中引入aop命名空间:

<beans xmlns=http://www.springframework.org/schema/beans

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

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.springramework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>

</beans>

 

spring提供两种切面使用方式,实际工作中我们可以选用其中一种:

基于XML配置方式进行AOP开发

基于注解方式进行AOP开发

 

基于注解方式声明切面

首先启动对@AspectJ注解的支持(蓝色部分):

<beans xmlns=http://www.springframework.org/schema/beans

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

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.springramework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>

<aop:aspectj-autoproxy/>

 

</beans>

 

@Aspect

public class MyInterceptor {

    @Pointcut("execution (* cn.service.impl.PersonServiceBean.*(..))")

    public void anyMethod() {} //declare a pointcut

   

    @Before("anyMethod()") //declare a before advice, parameter is the name of the pointcut

    public void doAccessCheck() {

       System.out.println("before advice");

    }

}

比较奇怪的是使用方法作为切入点名称

 

配置文件:

<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="myInterceptor" class="cn.service.MyInterceptor"></bean>

    <bean id="personService" class="cn.service.impl.PersonServiceBean"></bean>

</beans>

转载于:https://www.cnblogs.com/tongzhiyong/archive/2009/02/17/1392762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值