Spring_aop_xml配置文件方式_AspectJ
原创
©著作权归作者所有:来自51CTO博客作者959_1x的原创作品,请联系作者获取转载授权,否则将追究法律责任
目录结构
1.Book,java
public class Book {
public void buy(){
System.out.println("buy.......");
}
}
2.aop:BookProxy.java
@Aspect
public class BookProxy {
@Pointcut("execution(* com.atguigu.spring5.aopxml.*.*(..))")
public void pointCut(){}
@Before("pointCut()")
public void before(){
System.out.println("before........");
}
}
bean2.xml
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">
1.在容器中注册两个组件
2.配置aop
1.切入点 2.切面
切入点为需要增强的方法
切面为增强的方向,是前方还是后方还是环绕还是异常还是返回
test
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean( Book.class);
book.buy();
}
结果为在开始增强。