1.创建两个类,增强类和被增强类,创建方法
package com.atguigu.spring5.aopxml;
//
public class Book {
public void buy(){
System.out.println("buy.............");
}
}
package com.atguigu.spring5.aopxml;
public class BookProxy {
public void before(){
System.out.println("before.......");
}
}
2.在spring配置文件中创建两个对象
3.在spring配置文件中配置切入点
<?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="com.atguigu.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
<!--配置aop增强-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="p" expression="execution(* com.atguigu.spring5.aopxml.Book.buy(..))"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在具体方法上-->
<aop:before method="before" pointcut-ref="p"/>
</aop:aspect>
</aop:config>
</beans>
测试
package com.atguigu.spring5.aopanno.test;
import com.atguigu.spring5.aopanno.User;
import com.atguigu.spring5.aopxml.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
public class TestAop {
@Test
public void testAopXml(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book= context.getBean("book",Book.class);
book.buy();
}
}