-----------------------------基于XML配置方案
目标对象接口
1 public interface IUserService {
public void add();
public void update();
public void del();
public void search();
}

1.创建目标对象

 public class UserServiceImpl implements IUserService {
@Override
public void add() {
System.out.println("add....");
}
@Override
public void update() {
System.out.println("update....");
}
@Override
public void del() {
System.out.println("del....");
}
@Override
public void search() {
System.out.println("search....");
}
}

2.创建通知(增强 advice)

 //adcice增强类    Aspectj框架定义的通知类型
public class UserServiceHelper {
//前置通知
public void before(JoinPoint jp){
System.out.println("拦截目标类"+jp.getSignature().getDeclaringTypeName());
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("前置通知"); }
public void afterreturning(JoinPoint jp,Object val){
System.out.println("目标返回值"+val);
System.out.println("后置通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前通知");
Object value = pjp.proceed();
System.out.println("环绕后通知");
return value;
}
public void afterthrowing(JoinPoint jp,Throwable ex){
//有异常时才输出
System.out.println("异常抛出通知"+ex);
}
public void after(JoinPoint jp){
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("最终通知");
}
}

3.1在Spring的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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="./aop4.xml" />
</beans>

3.2在Spring的xmp配置apo1.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- target 目标对象 -->
<bean id="userService" class="cn.aspectjs.UserServiceImpl"/>
<!-- advice 通知-->
<bean id="userServiceAdvice" class="cn.aspectjs.UserServiceHelper"/>
<!-- config是用来声明,内容表示不管有没有接口,都用aspectj框架实现aop,aspect用来配置切面 -->
<aop:config proxy-target-class="true">
<aop:aspect ref="userServiceAdvice">
<aop:pointcut expression="execution(* *.add(..))" id="MypointCut"/>
<!-- method是增强类 -->
<aop:before method=""/>
<aop:before method="before" pointcut-ref="MypointCut"/>
<aop:after-returning method="afterreturning" pointcut-ref="MypointCut" returning="val"/>
<aop:around method="around" pointcut-ref="MypointCut" />
<aop:after-throwing method="afterthrowing" pointcut-ref="MypointCut" throwing="ex"/>
<aop:after method="after" pointcut-ref="MypointCut" />
</aop:aspect>
</aop:config>
</beans>

4.测试类

 //Spring整合JUnit测试
@RunWith(SpringJUnit4ClassRunner.class)
//参数定义了要装入的Spring配置文件
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AspectJTest{
@Autowired
private IUserService userService;
@Test
public void test1(){
//对应aop1.xml中的aspectj定义的通知.
userService.add();
}
}

                   基于annotation方案

1.编写目标

@Component //spring 扫描对象
public interface ICustomerService {
public void save();
public void search();
public void update();
}
 @Service
public class CustomerService implements ICustomerService {
@Override
public void save() {
System.out.println("save......");
}
@Override
public void search() {
//System.out.println(10/0);
System.out.println("search......");
}
@Override
public void update() {
System.out.println("update......");
} }

2.编写增强

 //增强类(通知)
@Component//声明Spring容器
@Aspect//声明当前的bean的就是一个切面
public class CustomerServiceHelper {
@Pointcut("execution(* *.s*(..))")
private void mypointcut(){}; @Pointcut("execution(* *.update(..))")
private void mypointcut1(){};
//前置通知
@Before("mypointcut()||mypointcut1()")
public void beforeA(JoinPoint jp){
System.out.println("目标类名称"+jp.getSignature().getDeclaringTypeName());
       System.out.println("目标方法名称"+jp.getSignature().getName());
System.out.println("前置通知......");
}
@AfterReturning(value="execution(* *.s*(..))",returning="o")
public void AfterReturning(JoinPoint jp,Object o){
System.out.println("后置通知.,目标方法的返回是"+o);
}
@Around("execution(* *.s*(..))")
public Object Around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("执行前操作...");
//是否执行目标方法
Object value = pjp.proceed();
System.out.println("执行后操作...");
//环绕通知必须要有返回值,值为目标方法的返回值
System.out.println("目标方法的返回值"+value);
return value;
}
@AfterThrowing (value="execution(* *.s*(..))",throwing="t")
public void AfterThrowing(JoinPoint jp,Throwable t){
System.out.println("异常抛出通知......"+t);
}
@org.aspectj.lang.annotation.After(value="execution(* *.s*(..))")
public void After(JoinPoint jp){
System.out.println("最后通知......");
}
}

3.配置

 <?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="./aop2.xml"/>
</beans>

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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 在Spring配置文件中配置扫描注解 -->
<context:component-scan base-package="cn.annotation"/>
<!-- 开启aspectj注解自动代理 .false代表的是如果目标是有接口的使用proxy代理,如果没有接口使用cglib动态代理-->
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="false"/>
</beans>

aop2

4.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AnnotationTest {
@Autowired
private ICustomerService customerService;
@Test
public void test(){
customerService.search();
}
}

5.控制台打印结果

执行前操作...
目标类名称cn.annotation.CustomerService
目标方法名称search
前置通知......
search......
执行后操作...
执行通知,目标方法的返回值null
最后通知......
后置通知.,目标方法的返回是null

在Spring整合aspectj实现aop的两种方式的更多相关文章

  1. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  2. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

  3. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  4. Spring Boot定义系统启动任务的两种方式

    Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...

  5. Spring Boot 中实现定时任务的两种方式

    在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...

  6. Spring容器自动调用方法的两种方式

    先看一个Spring中Bean的实例化过程: 1.配置文件中指定Bean的init-method参数 <bean class="com.jts.service.UserService& ...

  7. 使用aspectJ实现Spring AOP的两种方式

    方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1)       引入相关jar包 2)       创建Spring核心配置文件,必须导 ...

  8. 关于aop的两种方式-基于注解和基于aspectj

    spring的aop确实好用,能够在不影响业务功能的情况下,实现一些低耦合的功能. 而aop又有两种常用的实现方式,一种是用aspectj表达式去匹配,实现全局的配置,表达式还可以使用与或非符号去连接 ...

  9. spring的面向切面实现的两种方式

    面向切面:主要应用在日志记录方面.实现业务与日志记录分离开发. spring面向切面有两种实现方式:1.注解 2.xml配置. 1.注解实现如下: (1)配置如下: <?xml version= ...

随机推荐

  1. oracle 基础知识(四) 构成

    一, oracle服务 一个oracle 服务由一个oracle 实例和一个oracle数据库组成. oracle = instance + database 总体概念: 二, oracle 实例 0 ...

  2. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

  3. Java Mail邮件发送的简单实现

    1.什么是java mail JAVA MAIL是利用现有的邮件账户发送邮件的工具,通过JAVA Mail的操控,让程序自动的使用设置的邮箱发送邮件. 这一机制被广泛的用在注册激活和垃圾邮件的发送等方 ...

  4. Path类 操作文件类

    // Path类 IO命名空间 静态类 不能创建对象类名. string str =@"E:\C#程序设计基础入门教程\(第十一天)\122\22\nee.txt"; ////in ...

  5. 使用 Azure CLI 创建虚拟机

    使用 az vm create 命令创建虚拟机. 创建虚拟机时,可使用多个选项,例如操作系统映像.磁盘大小调整和管理凭据. 在此示例中,创建了一个名为“myVM”的运行 Ubuntu Server 的 ...

  6. attribute和property的区别

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  7. Java基础入门 - 简介

    官网:https://www.oracle.com Java分为三个体系: JavaEE: Java Platform, Enterprise Edition, Java平台企业版 JavaSE: J ...

  8. javascript小数相减出现一长串的小数位数

    我们要修改网页某个数据的显示格式,需要两步操作: 1.在JS中通过$('.class1 .class2 li:eq(2) span.value').text().trim();类似的语句获取到数据内容 ...

  9. VS2012 无法启动 IIS Express Web

    用记事本打开项目的.csproj文件,定位到<WebProjectProperties>,把关于IIS的配置<DevelopmentServerPort>.<Develo ...

  10. PAT 1056 Mice and Rice

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> #i ...