spring AspectJ 基本使用
来源:互联网 发布:美国eia数据在那里看 编辑:程序博客网 时间:2024/06/09 20:01
spring 实际使用 (这就使用一个例子说明)
测试类以及结果:
package unit;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.HelloService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
HelloService service = (HelloService) context.getBean("helloService");
service.annotationAop();
System.out.println();
service.xmlAop();
}
}
结果:
annotationAop//正常方法运行
aop--AspectJ! //annotation拦截
xmlAop //正常方法运行
aop--XmlAop! //配置拦截
use jar
- --aspectjrt.jar
- --aspectjweaver.jar
- --acglib-nodep-2.1_3.jar
- --commons-logging.jar
- --spring.jar
- --spring-aspects.jar
XML配置
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 测试使用类 分别由 方法 annotationAop/xmlAop -->
<bean id="helloService"
class="bean.HelloService"/>
<!-- annotation aop 拦截 使用@Aspect
@Pointcut("execution(* annotationAop(..))")
@AfterReturning("mainMethod()")
-->
<bean id="xmlAop"
class="aop.AnnotationAspectJ"/>
<aop:aspectj-autoproxy/>
<!-- xml aop 配置文件拦截 -->
<bean id="XmlAspectJ"
class="aop.XmlAspectJ"/>
<aop:config>
<aop:aspect ref="XmlAspectJ">
<aop:pointcut id="mainMethod" expression="execution(* xmlAop(..))"/>
<aop:after-returning pointcut-ref="mainMethod" method="goXmlAop"/>
</aop:aspect>
</aop:config>
</beans>
HelloService.java
package bean;
public class HelloService {
public void annotationAop() {
System.out.println(" annotationAop ");
}
public void xmlAop(){
System.out.println(" xmlAop ");
}
}
AnnotationAspectJ.java
package aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AnnotationAspectJ {
@Pointcut("execution(* annotationAop(..))")
public void mainMethod() {}
@AfterReturning("mainMethod()")
public void sayHello() {
System.out.println("aop--AspectJ!");
}
}
XmlAspectJ.java
package aop;
public class XmlAspectJ {
public void goXmlAop(){
System.out.println(" aop--XmlAop! ");
}
}
- spring AspectJ 基本使用
- 【Spring AOP】基于AspectJ的AOP基本使用
- Spring中的AOP——AspectJ的基本使用
- Spring-AOP 使用@AspectJ
- spring项目中使用Aspectj
- Spring之配置使用@AspectJ
- Spring aspectJ切面使用步骤
- Spring AOP使用ASPECTJ错误
- Spring中使用AspectJ指示器
- Spring中使用AspectJ指示器
- Spring aop之使用AspectJ
- Spring中的AOP(七)(实际上应该是一)——AspectJ的基本使用
- Spring中的AOP(七)(实际上应该是一)——AspectJ的基本使用
- 使用Spring,Aspectj进行面向切面编程
- 使用Spring AOP 的@AspectJ记录日志
- spring 学习笔记 使用aspectj开发aop
- 使用Spring AOP和AspectJ编排工作流
- Spring AOP使用@AspectJ实现日志管理
- js 汉字编码【鸡蛋】
- ZOJ 1240 IBM Minus One
- bloom filter简单实现
- memset对数组赋初值探讨
- DevExpress XtraGrid 动态自定义控制button按钮显示
- spring AspectJ 基本使用
- Android有关Please execute 'adb uninstall my.test' in a shell报错处理
- CentOS6.2下一步一步源代码安装OpenStack(八)Swift代理节点配置、运行
- ALM 解决方案之TechExcel DevSuite 评估报告 - 2
- Go语言_eclipse环境搭建(继上篇)
- poj 3767 I Wanna Go Home
- 黑马程序员_Java学习日记第二天--面向对象
- SSL协议及其应用
- 关于openSessionInView的问题