spring aop 注解方式

前置、后置、环绕、切面、切点


package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
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.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Audience {
	@Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
	public void performance() {
	}

	@Before("performance()")
	public void takeSeats() {
		System.out.println("The audience is taking their seats.");
	}

	@Before("performance()")
	public void turnOffCellPhones() {
		System.out.println("The audience is turning off their cellphones");
	}

	@AfterReturning("performance()")
	public void applaud() {
		System.out.println("CLAP CLAP CLAP CLAP CLAP");
	}

	@AfterThrowing("performance()")
	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
	
	@Around("performance()")
	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			long start = System.currentTimeMillis();
			joinpoint.proceed();
			long end = System.currentTimeMillis();
			System.out.println("The performance took " + (end - start) + " milliseconds.");
		} catch (Throwable t) {
			System.out.println("Boo! We want our money back!");
		}
	}
}

引入新方法


package com.springinaction.springidol;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

@Aspect
public class ContestantIntroducer {

	@DeclareParents(value = "com.springinaction.springidol.Performer+", defaultImpl = GraciousContestant.class)
	public static Contestant contestant;
}

package com.springinaction.springidol;

public interface Contestant {
	void receiveAward();
}

package com.springinaction.springidol;

public class GraciousContestant implements Contestant {

	public void receiveAward() {
		System.out.println("Why, thank you all very much!");
	}
}

XML配置

spring-idol.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--<start id="preamble" /> -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!--<end id="preamble" /> -->

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!--<start id="audience_bean" /> -->
	<bean id="audience" class="com.springinaction.springidol.Audience" />
	<!--<end id="audience_bean" /> -->

	<!--<start id="contestant_introducer" /> -->
	<bean class="com.springinaction.springidol.ContestantIntroducer" />
	<!--<end id="contestant_introducer" /> -->
	
	<bean id="volunteer" class="com.springinaction.springidol.Volunteer" />
	<bean id="magician" class="com.springinaction.springidol.Magician" />

	<!--<start id="aspectj_autoproxy" /> -->
	<aop:aspectj-autoproxy />
	<!--<end id="aspectj_autoproxy" /> -->

</beans>


注意:

<aop:aspectj-autoproxy />


测试类:

package com.springinaction.springidol;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol.xml")
public class AspectTest {
	@Autowired
	ApplicationContext context;
	
	@Autowired
	Performer performer;

	@Test
	public void audienceShouldApplaud() throws Exception {
		performer.perform();
	}
	
	Contestant contestant;

	@Test
	public void eddieShouldBeAContestant() {
		contestant = (Contestant)performer;
		contestant.receiveAward();
	}
	
	@Autowired
	MindReader magician;

	@Autowired
	Thinker volunteer;
	
	@Test
	public void magicianShouldReadVolunteersMind() {
		volunteer.thinkOfSomething("Queen of Hearts");

		assertEquals("Queen of Hearts", magician.getThoughts());
	}
}

运行audienceShouldApplaud:

The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took 0 milliseconds.

运行eddieShouldBeAContestant:

Why, thank you all very much!

运行magicianShouldReadVolunteersMind:

Intercepting volunteer's thoughts : Queen of Hearts


其他几个未贴代码的类请看http://blog.csdn.net/feier7501/article/details/24102545

你可能感兴趣的:(spring aop 注解方式)