spring(AOP)多个切面

/**
 * 切面:日志
 * @author w7
 *
 */
public class Logger {
    public void logging(){
        System.out.println("logging");
    }
}
/**
 * 切面:安全框架
 * @author w7
 *
 */
public class Security {
    public void security(){
        System.out.println("security");
    }
}
/**
 * 切面:权限
 * @author w7
 *
 */
public class Privilege {
    private String access;
    public String getAccess() {
        return access;
    }
    public void setAccess(String access) {
        this.access = access;
    }

    /**
     * 环绕通知方法
     * 
     * @param joinPoint 目标方法,有返回值 
     * @return
     * @throws Throwable
     */
    public Object controlTarget(ProceedingJoinPoint joinPoint) throws Throwable{

        Object obj = null; //目标方法返回值 

        if(this.getAccess().equals("admin")){
            obj = joinPoint.proceed();//调用目标方法
        }else{
            System.out.println("权限不足");
        }
        return obj;
    }
}
/**
 * 业务接口
 * @author w7
 *
 */
public interface SalaryManager {
    /**
     * 查看工资
     */
    public String showSalary();
}
/**
 * 目标类:业务实现
 * @author w7
 *
 */
public class SalaryManagerImpl implements SalaryManager{

    public String showSalary() {
        System.out.println("正在查看工资");
        return "aaaa";
    }
}
/**
 * 第二个 环绕通知测试
 * 
 * 如果有多个环绕通知时,则所有环绕通知 执行完毕后,才执行目标方法
 * @author w7
 *
 */
public class AroundClass {
    public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("----------------");
        joinPoint.proceed();
    }
}
//xml配置
<bean id="salaryManager" class="com.itheima09.springaop.xml.salary.SalaryManagerImpl"></bean>

    <!--
         引入多个切面
        logger:日志系统
        security:安全框架
        privilege:权限控制
     -->
    <bean id="logger" class="com.itheima09.springaop.xml.salary.Logger"></bean>
    <bean id="security" class="com.itheima09.springaop.xml.salary.Security"></bean>
    <bean id="privilege" class="com.itheima09.springaop.xml.salary.Privilege">
        <property name="access" value="admin"></property>
    </bean>  

    <!-- 
        多个环绕通知 测试
     -->
    <bean id="aroundClass" class="com.itheima09.springaop.xml.salary.AroundClass"></bean>

    <aop:config>
        <aop:pointcut 
            expression="execution(* com.itheima09.springaop.xml.salary.SalaryManagerImpl.*(..))" 
            id="perform"/>

        <aop:aspect ref="logger">
            <aop:before method="logging" pointcut-ref="perform"/>
        </aop:aspect>
        <aop:aspect ref="security">
            <aop:before method="security" pointcut-ref="perform"/>
        </aop:aspect>
        <aop:aspect ref="privilege">
            <aop:around method="controlTarget" pointcut-ref="perform"/>
        </aop:aspect>

        <aop:aspect ref="aroundClass">
            <aop:around method="aroundMethod" pointcut-ref="perform"/>
        </aop:aspect>
    </aop:config>   
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        SalaryManager salaryManager = (SalaryManager)context.getBean("salaryManager");

        //执行目标方法
        String s = salaryManager.showSalary();
        System.out.println(s);
    }
结果:
logging
security
----------------  // 第二个环绕通知执行的输出
正在查看工资       //目标方法执行结果
null             //目标方法返回值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值