Spring AOP使用Aspectj基于xml方式,初始化Bean参数

来源:互联网 发布:淘宝BB8机器人 编辑:程序博客网 时间:2024/06/05 03:22

场景:

大多数实体继承了一个BaseBean,这里所做的就是使用Spring的Aop功能实现,拦截到的方法,对其参数做一些处理。


spring-xxx.xml的核心配置:

    <aop:aspectj-autoproxy proxy-target-class="true" />    <aop:config>       <!--配置com.xxx.xxx.*.controller包下所有类或接口的所有方法-->       <aop:pointcut id="businessService" expression="execution(* com.xxx.xxx.*.controller.*.*(..))" />       <aop:aspect id="TestAspect" ref="aspectBean">           <aop:before pointcut-ref="businessService" method="doBefore"/>           <aop:after pointcut-ref="businessService" method="doAfter"/>           <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>       </aop:aspect>    </aop:config>    <bean id="aspectBean" class="com.xxx.xxx.common.TestAspect" />

切点类:

package com.xxx.xxx.common;import java.lang.reflect.Method;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.aspectj.lang.JoinPoint;import org.slf4j.LoggerFactory;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import com.xxx.xxx.bean.BaseBean;import com.xxx.xxx.bean.LoginUser;public class TestAspect {    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TestAspect.class);    public void doAfter(JoinPoint jp) {        LOGGER.debug("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());    }    public void doBefore(JoinPoint jp) throws Exception {        Object[] args = jp.getArgs();        for (Object object : args) {            // 判断获取的参数对象是不是继承了BaseBean            if (BaseBean.class.isAssignableFrom(object.getClass())) {                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();                HttpSession httpSession = request.getSession();                if (httpSession == null) {                    LOGGER.error("获取session失败");                }                LoginUser loginUser = (LoginUser) httpSession.getAttribute(Constants.LOGIN_USER);                Class father = httpSession.getAttribute(Constants.LOGIN_USER).getClass().getSuperclass();                try {                    // 获取父类setLoginBean方法                    Method method = father.getMethod("setLoginBean", new Class[] { LoginUser.class });                    // 使用反射机制可以打破封装性,导致了java对象的属性不安全                    // method.setAccessible(true);                    // 反射调用父类方法                    method.invoke(object, new Object[] { loginUser });                } catch (Exception e) {                    LOGGER.error(e.getStackTrace() + ":" + e.getMessage());                }            }        }        LOGGER.debug("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + "[PARAMS]:" + jp.getArgs().toString());    }    public void doThrowing(JoinPoint jp, Throwable ex) {        LOGGER.error("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");        LOGGER.error(ex.getStackTrace() + ":" + ex.getMessage());    }}

注:这里拦截的类是Controller,并非Service,而在拦截ControllerService的时候spring对切点的配置是有一些不一样的,关于Spring在字节码增强的方面,笔者刚接触Spring不久,现在恐怕还没能力解释什么,这里不多说,免得误导了大家。有很多前辈已经给我们指明了前路,感兴趣的可以自行百度。

学生浅薄,望众师指点wengang.liu
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高铁票身份证验证失败怎么办 网上订的火车票查不到怎么办 已经参加工作想学个本科证怎么办 火车晚点耽误下一趟列车怎么办 门外装监控没有预留电线怎么办 框架柱主筋柱顶预留长度不够怎么办 遇到很嚣张的人怎么办 在地板砖上铺木地板卧室门怎么办 宝宝打预防针的本子丢了怎么办 宝宝打预防针本子丢了怎么办 打疫苗的本子丢了怎么办 麦客收割机麦秸里加麦粒怎么办 高铁乘务员身高不够怎么办 坐火车买到站票怎么办 买上车补票原票怎么办? 买的商务座补票怎么办 12306账号被别人登录怎么办 飞机不提供餐食怎么办 12306退票支付宝失败怎么办 12306重复支付怎么办支付宝 支付宝登的12306账号怎么办 没买儿童高铁票怎么办 网上订的机票怎么办托运 半夜买高铁票不出票怎么办 轻轨少买了一站怎么办 高铁火车票丢了怎么办 如果高铁票丢了怎么办 高铁票丢了怎么办 报销 高铁如果没赶上怎么办 高铁管家待核验怎么办 动车没有票了怎么办 12306取消订单3次怎么办 【12306取消订单3次怎么办】 火车票取消订单3次怎么办 12306收不到验证码怎么办 安逸花验证码次数限制怎么办 航班晚点导致错过转机怎么办 想去沈阳站送站怎么办 高铁没有赶上车怎么办 火车晚点赶不上下一趟车怎么办 列车晚点影响下一趟车怎么办?