12、基于XML方式的AOP

创建Bean

//首先创建业务接口
package com.codestd.springstudy.aop.xml.service;

public interface UserService {

    public void login();

    public void logout() throws Exception;

}

//创建实现类
package com.codestd.springstudy.aop.xml.service;

public class UserServiceImpl implements UserService {

    @Override
    public void login() {
        System.out.println("User login");

    }

    @Override
    public void logout() throws Exception {
        System.out.println("User logout");
        throw new Exception("Exception test");
    }

}

//创建切面类
package com.codestd.springstudy.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class AspectBean {

    public void doAfter(JoinPoint jp){
        System.out.println("do after");
    }

    public void doBefore(JoinPoint jp){
        System.out.println("do after");
    }

    public Object doAround(ProceedingJoinPoint  jp) throws Throwable{
        System.out.println("do Around");
        Object obj = jp.proceed();
        return obj;
    }

    public void doThrowing(JoinPoint jp,Exception e){
        System.out.println(e.getClass().getName()+"|->|"+e.getMessage());
    }

}

配置AOP

下面使用XML配置AOP

    <bean id="userService" class="com.codestd.springstudy.aop.xml.service.UserServiceImpl" />

    <bean id="aspectBean" class="com.codestd.springstudy.aop.xml.AspectBean" />

    <aop:config>
        <aop:aspect id="aspect" ref="aspectBean">
            <aop:pointcut expression="execution(* com.codestd.springstudy.aop.xml.service.*.*(..))" id="servicePointcut"/>
            <aop:after method="doAfter" pointcut-ref="servicePointcut" />
            <aop:before method="doBefore" pointcut-ref="servicePointcut"/>
            <aop:around method="doAround" pointcut-ref="servicePointcut"/>
            <aop:after-throwing method="doThrowing" pointcut-ref="servicePointcut" throwing="e"/>
        </aop:aspect>
    </aop:config>

doAround方法要有返回值,并且类型是Object,接收的参数类型为ProceedingJoinPoint
aop:after-throwing中需要使用throwing指定异常的参数名,doThrowing(JoinPoint jp,Exception e)中的e

测试

测试方法如下

package com.codestd.springstudy.aop.xml.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:aop/applicationContext.xml"})
public class UserServiceImplTest {

    @Autowired
    private UserService userService;

    @Test
    public void test() throws Exception {
        this.userService.login();
        this.userService.logout();
    }

}

测试结果如下

do after
do Around
User login
do after
do after
do Around
User logout
do after
java.lang.Exception|->|Exception test

使用XML方式配置引入

<aop:config>
    <aop:aspect>
        <aop:declare-parents types-matching="com.codestd.springstudy.aop.xml.service.UserService+" 
            implement-interface="com.codestd.springstudy.aop.introductions.Flyable" 
            default-impl="com.codestd.springstudy.aop.introductions.DefaultFlyable"/>
    </aop:aspect>
</aop:config>

测试类如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:aop/applicationContext.xml"})
public class UserServiceImplTest {
    @Autowired
    private Flyable flyable;

    @Test
    public void test() throws Exception {
        this.flyable.fly();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大扑棱蛾子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值