Spring Aop 初探(4)

只是为了自己更容易记住,理解相关内容,所以写了一篇简单的文章,主要还是参考前辈的大作完成。高手勿喷。


1. 创建maven工程文件,其中pom.xml 文件的内容如下:

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
    </dependencies>

这个文件与之前的文件其实是相同的,配置完成后,使用maven update下载jar文件

2. 创建一个实现类ShowService,只有一个方法:

package com.tanj.test;
public class ShowService {
public void show() {  
        System.out.println(getClass().toString() + " show Hello Spring Aop!");  
    }  
}

3. 创建一个切面类ShowAspect

package com.tanj.test;
import org.aspectj.lang.ProceedingJoinPoint;
public class ShowAspect {
// before通知方法  
    public void beforeShow() {  
        System.out.println(getClass().toString() + " before show");  
    }  
    // after通知方法  
    public void afterShow() {  
        System.out.println(getClass().toString() + " after show");  
    }  
    // afterReturn通知方法  
    public void afterReturnShow() {  
        System.out.println(getClass().toString() + " afterReturn show");  
    }  
    // afterThrowing通知方法  
    public void afterThrowingShow() {  
        System.out.println(getClass().toString() + " afterThrowing show");  
    }  
    // around通知方法  
    public void aroundShow(ProceedingJoinPoint jpoint) {  
        try {  
            System.out.println(getClass().toString() + " around before show");  
            // 执行目标对象的连接点处的方法  
            jpoint.proceed();  
            System.out.println(getClass().toString() + " around after show");  
        } catch (Throwable e) {  
            System.out.println(getClass().toString() + " around afterThrowing show");  
        }  
    }  
}

3. 相应的Spring 配置文件 applicationContext.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd   
     http://www.springframework.org/schema/context   
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <aop:config>  
        <!-- 定义切面 引用通知的bean -->  
        <aop:aspect id="show-aspect1" ref="showAspect">  
            <!-- 设置切入点 -->  
            <aop:pointcut id="pointcut1" expression="execution(* com.tanj.test.ShowService.*(..))" />  
            <!--指定before通知方法为,ShowAspect.beforeShow(),引用切入点pointcut1 -->  
            <aop:before method="beforeShow" pointcut-ref="pointcut1" />  
            <!--指定after通知方法为,ShowAspect.afterShow(),引用切入点pointcut1 -->  
            <aop:after method="afterShow" pointcut-ref="pointcut1" />  
            <!--指定afterReturn通知方法为,ShowAspect.afterReturnShow(),引用切入点pointcut1 -->  
            <aop:after-returning method="afterReturnShow" pointcut-ref="pointcut1" />  
            <!--指定afterThrowing通知方法为,ShowAspect.afterThrowingShow(),引用切入点pointcut1 -->  
            <aop:after-throwing method="afterThrowingShow" pointcut-ref="pointcut1" />  
            <!--指定around通知方法为,ShowAspect.aroundShow(),引用切入点pointcut1 -->  
            <aop:around method="aroundShow" pointcut-ref="pointcut1" />  
        </aop:aspect>  
    </aop:config>  
  
    <!-- 生成切面通知的bean -->  
    <bean id="showAspect" class="com.tanj.test.ShowAspect" />  
    <!-- 生成com.tanj.test.ShowService -->  
    <bean id="showService" class="com.tanj.test.ShowService"></bean>  
</beans>  

4. 创建测试类MainTest:

package com.tanj.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tanj.test.ShowService;;
public class MainTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ShowService showService = (ShowService)context.getBean("showService");
showService.show();
}
}

5. 运行测试类,可以看到结果。


注意: 本篇文章完全采用注解的方式,实现Spring的Aop 相关内容,但是很在实际应用中,可以根据需要,选择是用代码还是配置的方式实现。


参考的博客如下:

http://blog.csdn.net/sigangjun/article/details/12840853 个人在练习的时候,稍微做了一些改动,以方便个人理解。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值