1、用xml的方式创建对象bean
<?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.xsd">
<!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
</beans>2、在这个spring核心配置文件中开启aop注解模式<!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>3、在增强类上面使用@Aspect注解
在增强方法上面使用注解
(在被增强的类上不需要注解)
package cn.itcast.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyBook {
@Before(value="execution(* cn.itcast.aop.Book.*(..))")
public void before1(){
System.out.println("before.........");
}
}
本文介绍了如何使用Spring框架通过XML配置文件实现AOP(面向切面编程)。具体包括配置文件的基本结构、如何定义Bean以及如何开启AOP的注解支持。此外,还展示了如何在增强类中使用@Aspect与@Before等注解来实现特定的方法拦截。
4371

被折叠的 条评论
为什么被折叠?



