Book类(buy方法将被设置为切入点)
package package2;
public class Book {
public void buy(){
System.out.println("buy...");
}
}
BookProxy类(BookProxy类将被设置为切面容器,before将被设为通知)
package package2;
public class BookProxy {
public void before(){
System.out.println("before...");
}
}
bean2.xml
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--创建对象-->
<bean id="book" class="package2.Book"></bean>
<bean id="bookProxy" class="package2.BookProxy"></bean>
<!--配置aop增强-->
<aop:config>
<!--切入点,命名为p-->
<aop:pointcut id="p" expression="execution(* package2.Book.buy())"/>
<!--配置切面(配置存在增强方法的类):将通知(增强方法)应用到切入点-->
<aop:aspect ref="bookProxy">
<!--定义通知(增强)-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
</beans>
Test类
public class MyTest {
@Test
public void testAopXml(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}
}
测试结果
before...
buy...