1. Simple use: Hello World example
1. Define the target class
public interface Hello { void sayHello(); }
public class HelloImpl implements Hello { @Override public void sayHello() { System.out.println("hello matt!"); } }
2. Define the aspect support class
public class HelloAspect { public void beforeAdvice() { System.out.println("****beforeAdvice"); } public void afterFinnallyAdvice() { System.out.println("****afterFinnallyAdvice"); } }
3. Configure the slice
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- 目标类 --> <bean id="hello" class="cn.matt.aop.HelloImpl"></bean> <!-- 切面支持类 --> <bean id="helloAspect" class="cn.matt.aop.HelloAspect"></bean> <aop:config> <!-- 切点 --> <aop:pointcut id="pointcut" expression="execution(* cn.matt.aop..*.*(..))"/> <!-- 切面 --> <aop:aspect ref="helloAspect"> <aop:before pointcut-ref="pointcut" method="beforeAdvice"/> <aop:after pointcut="execution(* cn.matt.aop..*.*(..))" method="afterFinnallyAdvice"/> </aop:aspect> </aop:config> </beans>
4. Test
@Test public void testSayHello () { ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); Hello hello = context.getBean(Hello.class); hello.sayHello(); }
output:
****beforeAdvice hello matt! ****afterFinnallyAdvice
Second, the introduction of AOP configuration
AOP related definitions must be placed under the <aop:config> tag, under which there can be <aop:pointcut>, <aop:advisor>, <aop:aspect> tags, and the configuration order cannot be changed
AOP configuration steps:
1) Declare the aspect backing bean (instantiate the backing class via the <bean> tag)
2) Declare the aspect and refer to the aspect-backed bean (the aspect is specified by the <aop:aspect> tag, and the ref attribute is used to refer to the aspect-backed bean)
3) There are two ways to declare the pointcut, note that the pointcut is also a bean
i) Use <aop:pointcut> to declare a pointcut bean that can be shared by multiple aspects
<aop:config> <aop:pointcut id="pointcut" expression="execution(* cn.javass..*.*(..))"/> <aop:aspect ref="aspectSupportBean"> <aop:before pointcut-ref="pointcut" method="before"/> </aop:aspect> </aop:config>
ii) Anonymous pointcut Bean, specified by the pointcut attribute
<aop:config> <aop:aspect ref="aspectSupportBean"> <aop:after pointcut="execution(* cn.javass..*.*(..))" method="afterFinallyAdvice"/> </aop:aspect> </aop:config>
4) Statement notice, there are five kinds:
i) Called before the pre-notification method is called
ii) Called after the post-return notification method is called and when it returns normally
iii) Called after the post-exception notification method is called and an exception is thrown
iv) always called after post-final notification method call
v) Surround notification can control the execution process of the method, such as determining whether the method is executed, when to execute, replacing method parameters during execution, replacing return value after execution, etc.