博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssm 自定义aop不生效 不执行
阅读量:6936 次
发布时间:2019-06-27

本文共 4127 字,大约阅读时间需要 13 分钟。

hot3.png

 

在s

sm里自己写了个aop切面,通过注解配置,发现不生效,最后找到原因,是spring配置时,只扫描了controller和service,并没有扫描aop类

applicationContent。xml

log4jdbcInterceptor
dataSource

 

 

init-servlet.xml

/front/errorUploadSize
package framework.aop;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect@Componentpublic class WxUserDzUserUpdateAspect {	private static final Log logger = LogFactory.getLog(WxUserDzUserUpdateAspect.class);	/**	 * 对注释有RequestMapping标记的方法,进行AOP切入拦截	 */	@Pointcut(" execution (* com.sunny.service.impl..*.*(..))")	public void controllerPointcut() {	}	@After(value="controllerPointcut()")	public void after(JoinPoint joinPoint) {		System.out.println("---------------------------joinPoint");	}	@SuppressWarnings("unchecked")	@Around("controllerPointcut()  ")	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {		System.out.println("---------------------------joinPoint");		return joinPoint.proceed(joinPoint.getArgs());	}	/**	 * 异常统一处理	 * 	 * @Title: afterThrowing	 * @Description:	 * @author weifa.Yang	 * @date 2016-8-3 下午04:19:57	 * @param e	 * @return void	 */	@AfterThrowing(pointcut = "controllerPointcut() ", throwing = "e")	public void afterThrowing(JoinPoint joinPoint, Throwable e) {		logger.error("展客[RPC],请求接口[" + joinPoint.getTarget().getClass().getName() + "],方法[" + joinPoint.getSignature().getName() + "],方法抛出异常:" + e.getMessage());	}	/**	 * 返回值日志记录	 * 	 * @Title: afterReturning	 * @Description:	 * @author weifa.Yang	 * @date 2016-8-3 下午04:20:05	 * @param joinPoint	 * @param result	 * @return void	 */	@AfterReturning(pointcut = "controllerPointcut() ", returning = "result")	public void afterReturning(JoinPoint joinPoint, Object result) {		logger.info("响应展客[RPC]接口[" + joinPoint.getSignature().getName() + "],返回结果:");	}}

添加扫描后 ,可以执行了

但是这样有个隐患,就是springmvc配置了全路径扫描,而不是最正确的只扫描controller,这就会导致没有事务增强,所以我就改成init-servlet。xml只扫描controller,结果这样导致aop无法生效。

原因是,Service的扫描在spring容器内,而不是在springmvc子容器内,配置aop应该在application。xml里,在里面加上        <aop:aspectj-autoproxy proxy-target-class="true"/>   开启aop就可以了

 

转载于:https://my.oschina.net/u/1423640/blog/888966

你可能感兴趣的文章
Android实用代码七段(五)
查看>>
sql 表连接
查看>>
将不确定变为确定~对象被new后什么时候会抛System.NullReferenceException
查看>>
3.5. Filename prefix
查看>>
【sublime Text】sublime Text3安装可以使xml格式化的插件
查看>>
脏读和数据库一致性的分析
查看>>
使用阿里云配置管理ACM实现zookeeper依赖服务的透明Failover迁移
查看>>
Bitbucket免费的私有仓库
查看>>
微信硬件平台智能路由行业解决方案
查看>>
MySQL和Oracle中的隐式转换
查看>>
Qcon大会归来(r12笔记第36天)
查看>>
MapReduce实现倒排索引(类似协同过滤)
查看>>
Netkiller Architect 手札之前言
查看>>
ORACLE RAC 11.2.0.4 for RHEL6.8 集群CRS异常导致集群命令无法使用
查看>>
.NET 缩略图服务器 ResizingServer
查看>>
Angular vs React 最全面深入对比
查看>>
3.3. shutdown
查看>>
Oracle分页查询语句(六)
查看>>
[翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6
查看>>
Nginx禁止特定用户代理(User Agents)访问(转)
查看>>