Spring3.0的AOP基于Annotation AOP基于XML
2014-11-16 00:26
281 查看
首先搭建环境,
既然使用Annotation和AOP,第一步需要在.xml的配置文件中配置命名空间:我看了2.5的视频结合百度,配不好xml文件,最后发现官网上非常清楚,需要什么就在官网上看什么
切面文件:这里要明白切面的一些标记及语法
使用XML下的AOP
切面文件:将球面的注解去掉
.xml配置文件
对于Annotation和xml配置的AOP来说,显然XML配置的AOP重要一些。对于别人项目中的类的注解不可见的情况下,只需要配置文件即可实现切面编程.
既然使用Annotation和AOP,第一步需要在.xml的配置文件中配置命名空间:我看了2.5的视频结合百度,配不好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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <strong> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <aop:aspectj-autoproxy /> </strong> </beans>
切面文件:这里要明白切面的一些标记及语法
@Aspect @Component public class LogInterceptor { <strong>@Pointcut("execution(public * com.bjsxt.service..*.add(..))")</strong> public void myMethod(){}; <strong>@Before("myMethod()")</strong> public void before() { System.out.println("method before"); } <strong>@Around("myMethod()")</strong> public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.proceed(); System.out.println("method around end"); } }
使用XML下的AOP
切面文件:将球面的注解去掉
@Aspect @Component public class LogInterceptor { //@Pointcut("execution(public * com.bjsxt.service..*.add(..))") public void myMethod(){}; //@Before("myMethod()") public void before() { System.out.println("method before"); } //@Around("myMethod()") public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.proceed(); System.out.println("method around end"); } }
.xml配置文件
<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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> <aop:config> <aop:aspect id="logAspect" ref="logInterceptor"> <aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" /> </aop:aspect> </aop:config> </beans>
对于Annotation和xml配置的AOP来说,显然XML配置的AOP重要一些。对于别人项目中的类的注解不可见的情况下,只需要配置文件即可实现切面编程.
相关文章推荐
- Spring3.0 入门进阶(3):基于XML方式的AOP使用
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring中AOP基于XML的两种配置方法
- Spring AOP的使用 基于XML
- Spring AOP(3)基于XML配置实现的示例
- spring添加AOP切面-基于XML配置
- 基于spring3.0的采用XML配置的spring MVC项目
- 基于xml配置的spring aop 的实现日志 和为通知传递参数
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- spring之aop编程——基于注解、xml配置文件方式
- Spring学习总结7(AOP-基于XML)
- 基于XML的spring AOP配置
- Spring 基于XML配置的AOP入门案例
- 基于Spring 4.0和Servlet 3.0编写无XML配置的REST服务
- spring -- aop基于xml的简单实现
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring的AOP配置(基于xml)
- springAOP基于xml的配置前后环绕通知
- Spring 基于xml配置方式的AOP
- Spring AOP使用Aspectj基于xml方式,初始化Bean参数