注解方式
applicationContext.xml 加入下面配置
<!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy-target-class="true"/>
LoggingAspect,java
package com.lingdong.spring.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(1) @Component @Aspect public class LoggingAspect { private final static Logger logger = LoggerFactory.getLogger(LoggingAspect.class); @Pointcut("execution(* com.lingdong.spring.aop.*(..))") public void aspect(){} @Before("aspect()") public void before(JoinPoint joinPoint){ if (logger.isInfoEnabled()){ logger.info("before:"+joinPoint) ; } } }
xml配置aop方式
<bean id="loggingAspect" class="com.lingdong.spring.aop.LoggingAspect"/> <!--配置Aop 切入点,切面--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.lingdong.spring.aop.*.*(..))"></aop:pointcut> <aop:aspect ref="loggingAspect"> <aop:before method="before" pointcut-ref="pointcut"></aop:before> </aop:aspect> </aop:config>
LoggingAspect.java
package com.lingdong.spring.aop; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LoggingAspect { private final static Logger logger = LoggerFactory.getLogger(LoggingAspect.class); public void aspect(){} public void before(JoinPoint joinPoint){ if (logger.isInfoEnabled()){ logger.info("before:"+joinPoint) ; } } }
本文出自 “Java技术博客” 博客,请务必保留此出处http://lingdong.blog.51cto.com/3572216/1888689
原文:http://lingdong.blog.51cto.com/3572216/1888689