AOP之基于@Aspect 注解与Schema(xml)区别
来源:互联网 发布:银行家算法经典例题 编辑:程序博客网 时间:2024/06/05 01:33
基于@Aspect 注解
基于@Aspect 注解的AOP开发主要需要目标类、切面、配置文件。
调用关系如图:
首先引入jar包:
下载jar
目标类(HelloWorld):
package com.demo.spring.aop;public class Helloworld { protected String message;//定义String 类型变量 public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void execute(){ System.out.println("Hello"+getMessage()+"!"); }}
切面:
package com.demo.spring.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class MyAspect { /** * 定义切入点函数 */ @Pointcut("execution(* com.demo.spring.aop.*.execute*(..))") void pointcut(){ } /** * 使用@Before声明前置通知 * @param thisJoinPoint */ @Before("pointcut()") public void beforeExecute(JoinPoint thisJoinPoint){ System.out.println("start to say:"); } /** * 使用@Around 注解声明环绕通知 * @param thisJoinPoint * @return * @throws Throwable */ @Around("pointcut()") public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{ System.out.println("before 之前"); Object va=thisJoinPoint.proceed(); System.out.println("after 之前"); return va; } /** * 使用@After 注解声明后置通知 * @param thisJoinPoint */ @After("pointcut()") public void afterExecute(JoinPoint thisJoinPoint){ System.out.print("end!"); }}
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="Helloworld" class="com.demo.spring.aop.Helloworld"> <property name="message"> <value>World</value> </property> </bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean></beans>
客户端调用:
package com.demo.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) { ApplicationContext ctx=new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"); Helloworld hello=(Helloworld) ctx.getBean("Helloworld"); hello.execute();}
}
这中方式主要是用Spring的注解方式开发AOP,还有一种是通过Schema(xml)的方式
基于Schema(xml)
与@Aspect注解相比不同之处主要在切面类与配置文件上,切面类不再用注解,而是在配置文件中配置增强
切面类:
package com.demo.spring.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Aspect;/** * 切面类 * @author dandan * */@Aspectpublic class MyAspect { /** * 前置通知 * @param thisJoinPoint */ public void beforeExecute(JoinPoint thisJoinPoint){ System.out.println("start to say:"); } /** * 环绕通知类 * @param thisJoinPoint * @return * @throws Throwable */ public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{ return thisJoinPoint.proceed(); } /** * 后置通知类 * @param thisJoinPoint */ public void afterExecute(JoinPoint thisJoinPoint){ System.out.println("end!"); }}
配置文件:
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="HelloWorld" class="com.demo.spring.aop.HelloWorld"> <property name="message"> <value>World</value> </property> </bean> <bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean> <aop:config> <!-- 定义切面 --> <aop:aspect id="aopAspect" ref="myAspect"> <!-- 定义切点 --> <aop:pointcut id="pointcut" expression="execution(* execute(..)) and target(com.demo.spring.aop.HelloWorld)"/> <!-- 定义通知 --> <aop:before pointcut-ref="pointcut" method="beforeExecute"/> <aop:around pointcut-ref="pointcut" method="userOperate"/> <aop:after pointcut-ref="pointcut" method="afterExecute"/> </aop:aspect> </aop:config></beans>
0 0
- AOP之基于@Aspect 注解与Schema(xml)区别
- Aop之Xml aspect配置
- 基于@Aspect的AOP注解配置
- 基于注解@Aspect的AOP实现
- <aop:aspect>与<aop:advisor>的区别
- <aop:aspect>与<aop:advisor>的区别
- spring Aop中aop:advisor 与 aop:aspect的区别
- spring 基于Aspect和注解的切面编程(aop)
- AOP之基于Schema配置总结与案例
- 基于@Aspect的AOP
- Spring之<aop:advisor>和<aop:aspect>的区别
- 循序渐进之Spring AOP(6) - 使用@Aspect注解
- Spring AOP之@Aspect注解,参考文章地址
- Spring中基于配置XML与Annotation注解配置AOP
- Spring2.5 注解 Aspect AOP
- Spring2.5 注解 Aspect AOP
- Spring2.5 注解 Aspect AOP
- Spring AOP 之 Aspect
- nyoj305
- NSUserDefaults—ios应用数据存储方式(偏好设置)
- Unable to load class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'
- 排序高级
- Android源码中增加模块
- AOP之基于@Aspect 注解与Schema(xml)区别
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系
- TCP客户端程序的编写
- m个苹果分在n个相同的盒子
- 15电气李书鹏VB作业3(1)
- 学JavaWeb必看文章!为什么我们会使用struts2,springMVC和spring这样的框架?
- 用vb制作计时小工具
- ECSHOP 数据库结构说明 (适用版本v2.7.3)
- 背景图及链接应用样式