ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
2009-02-17 21:42
1311 查看
感谢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>
使用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>
相关文章推荐
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)
- ITCAST视频-Spring学习笔记(使用CGLIB实现AOP功能与AOP概念解释)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- Spring学习笔记(三)Spring注解方式实现AOP
- Spring学习历程---使用注解方式入门AOP
- (9) 使用Spring的注解方式实现AOP入门 以及 细节
- ITCAST视频-Spring学习笔记(编码剖析@Resource注解的实现原理)
- AOP之Spring使用注解方式实现AOP(入门)
- 使用Spring的注解方式实现AOP入门
- 使用Spring的注解方式实现AOP
- Spring_6_使用注解的方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP实例
- ITCAST视频-Spring学习笔记(使用构造器装配属性)
- Spring学习笔记(15)----使用Spring注解方式管理事务
- 使用Spring的注解方式实现AOP
- ITCAST视频-Spring学习笔记(用@Resource注解完成属性装配)