ITeye博客

Spring AOP

txazo · 2012-09-05 12:32 · Spring · 编程语言 · Markdown

  1. Spring AOP

  2. Spring AOP注解

  3. Spring AOP 1)切面类


public class LogAspect {

	public void before() {
		System.out.println("Before");
	}

	public Object around(ProceedingJoinPoint proceedingJoinPoint)
			throws Throwable {
        System.out.println("Around Begin");  
		long time = System.currentTimeMillis();
		Object object = proceedingJoinPoint.proceed();
		time = System.currentTimeMillis() - time;
		System.out.println("Around After: " + time + "ms");
		return object;
	}

	public void after() {
		System.out.println("After");
	}

}

2)applicationContext.xml


<bean id="logAspect" class="com.txazo.aop.LogAspect" />

<aop:config>
	<aop:aspect id="logAspect" ref="logAspect">
		<aop:pointcut id="service" expression="
            execution(* com.txazo.service.impl.*Impl.*(..))" />
		<aop:before pointcut-ref="service" method="before" />
		<aop:around pointcut-ref="service" method="around" />
		<aop:after pointcut-ref="service" method="after" />
	</aop:aspect>
</aop:config>

  1. Spring AOP注解 1)切面类

@Aspect
@Component
public class LogAspect {

	@Pointcut("execution(* com.txazo.service.impl.*Impl.*(..))")
	private void pointcut() {

	}

	@Before("pointcut()")
	public void before() {
		System.out.println("Before");
	}

	@Around("pointcut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint)
			throws Throwable {
        System.out.println("Around Begin");  
		long time = System.currentTimeMillis();
		Object object = proceedingJoinPoint.proceed();
		time = System.currentTimeMillis() - time;
		System.out.println("Around After: " + time + "ms");
		return object;
	}

	@After("pointcut()")
	public void after() {
		System.out.println("After");
	}

}

2)applicationContext.xml


<aop:aspectj-autoproxy />

<context:component-scan base-package="com.txazo" />

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。