发布时间:2019-09-22 07:44:41编辑:auto阅读(2374)
Spring3基于注释驱动的AOP
实在是郁闷刚刚编辑了一篇文章,由于字数的原因,没发布成功,好我就分开写吧,今天向大家介绍的是Spring基于注释驱动的AOP,其实估计这已经不是什么新技术了,但是我争取写的通俗易懂,大家从我这看一次就能明白,那就是我最高兴的了.
还是那样,今天我主要介绍如何配置,写出一个例子,然后大家按照例子一配就ok了.配置文件很简单,只需要在Spring配置文件中加入以下这句话就行了,下面这句话是让Spring启动自动AOP代理
- <!--启动spring的aop自动代理-->
- <aop:aspectj-autoproxy/>
然后再创建一个AOP类
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.pdp.biz.dao.usermanage.UserDAO;
- /**
- * ClassName:DemoAop
- * Reason: TODO ADD REASON
- *
- * @author 王涛
- * @version
- * @since Ver 1.1
- * @Date 2010-11-24 上午10:25:18
- *
- */
- @Aspect
- @Service
- public class DemoAop {
- @Autowired
- public UserDAO userDao;
- @After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")
- public void doAfter(JoinPoint jp) {
- System.out.println(userDao.findUsersCount(null));
- Object[] args = jp.getArgs();
- for(Object obj : args){
- System.out.println("参数值:"+obj);
- }
- System.out.println("后处理切入----------"+jp.getTarget().getClass().getName()+"----"+jp.getSignature().getName());
- }
- }
里面的注释分别是@Aspect用于告诉Spring这个是一个需要织入的类,
- @After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")
- public void doAfter(JoinPoint jp) { ... }
里面的doAfter方法上面有一行注释,指明这个方法将在UserManageServiceImpl.sayhi(..)方法运行结束之后来执行,参数JoinPoint主要携带了参数值和方法名什么的,到时候自己查查文档就ok了
public interface JoinPoint
Provides reflective access to both the state available at a join point and static information about it. This information is available from the body of advice using the special form thisJoinPoint. The primary use of this reflective information is for tracing and logging applications.
aspect Logging {
before(): within(com.bigboxco..*) && execution(public * *(..)) {
System.err.println("entering: " + thisJoinPoint);
System.err.println(" w/args: " + thisJoinPoint.getArgs());
System.err.println(" at: " + thisJoinPoint.getSourceLocation());
}
}
Nested Class Summary
static interfaceJoinPoint.EnclosingStaticPart
static interfaceJoinPoint.StaticPart
This helper object contains only the static information about a join point.
Field Summary
static java.lang.StringADVICE_EXECUTION
static java.lang.StringCONSTRUCTOR_CALL
static java.lang.StringCONSTRUCTOR_EXECUTION
static java.lang.StringEXCEPTION_HANDLER
static java.lang.StringFIELD_GET
static java.lang.StringFIELD_SET
static java.lang.StringINITIALIZATION
static java.lang.StringMETHOD_CALL
static java.lang.StringMETHOD_EXECUTION
The legal return values from getKind()
static java.lang.StringPREINITIALIZATION
static java.lang.StringSTATICINITIALIZATION
static java.lang.StringSYNCHRONIZATION_LOCK
static java.lang.StringSYNCHRONIZATION_UNLOCK
Method Summary
java.lang.Object[]getArgs()
Returns the arguments at this join point.
java.lang.StringgetKind()
Returns a String representing the kind of join point.
SignaturegetSignature()
Returns the signature at the join point.
SourceLocationgetSourceLocation()
Returns the source location corresponding to the join point.
JoinPoint.StaticPartgetStaticPart()
Returns an object that encapsulates the static parts of this join point.
java.lang.ObjectgetTarget()
Returns the target object.
java.lang.ObjectgetThis()
Returns the currently executing object.
java.lang.StringtoLongString()
Returns an extended string representation of the join point.
java.lang.StringtoShortString()
Returns an abbreviated string representation of the join point.
java.lang.StringtoString()
运行的时候直接运行就ok了.
- @Test
- public void testgetUsers(){
- userManageService.sayhi("Hi 大家好~!");
- }
上一篇: 整理python教程
下一篇: Python 练手程序合集(三)
51284
50736
41334
38144
32612
29514
28364
23233
23201
21526
1597°
2330°
1932°
1873°
2201°
1914°
2601°
4370°
4216°
2993°