package aop.xml;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Created by admin on 2018/8/17.
*/
public class Audience {
public void silenceCellPhones(){
System.out.println("Silencing cell phones");
}
public void takeSeats(){
System.out.println("Taking seats");
}
public void applause(){
System.out.println("CLAP CLAP CLAP!!!");
}
public void demandRefund(){
System.out.println("Demanding a refund");
}
public void watchPerformance(ProceedingJoinPoint jp){
try {
System.out.println("Around-Silencing cell phones");
System.out.println("Around-Taking seats");
jp.proceed();
System.out.println("Around-CLAP CLAP CLAP!!!");
}catch (Throwable e){
System.out.println("Demanding a refund");
}
}
//-----通知
}
package aop.xml;
import aop.Performance;
/**
* Created by admin on 2018/8/17.
*/
public class PerformanceSong implements Performance{
@Override
public void perform() {
System.out.println("song:xml~~~~~~~~");
}
}
package aop;
/**
* Created by admin on 2018/8/17.
*/
public interface Performance {
public void perform();
}
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">
<bean id="audience" class="aop.xml.Audience"/>
<bean id="performanceSong" class="aop.xml.PerformanceSong"/>
<aop:config>
<aop:aspect ref="audience">
<!--Define pointcut with AspectJ's pointcut EL -->
<aop:pointcut id="performance" expression="execution(* aop.Performance.perform(..))"/>
<aop:before pointcut-ref="performance" method="silenceCellPhones"/>
<aop:after pointcut-ref="performance" method="takeSeats"/>
<aop:after-returning pointcut-ref="performance" method="applause"/>
<aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
<aop:around pointcut-ref="performance" method="watchPerformance"/>
</aop:aspect>
</aop:config>
</beans>
package aop.xml;
import aop.Performance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by admin on 2018/8/17.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-aop.xml")
public class ConcertConfigTest {
@Autowired
Performance performance;
@Test
public void audience() throws Exception {
performance.perform();
}
}