aop概念:
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善,实际上是一种思想,对一个方法在不改变源码的情况下进行的增强。
aop的核心概念:
连接点(joinpoint):目标对象中可以被增强的方法 叫做连接点
切点(pointcut):连接点中真正被增强方法叫做切点
增强/通知(advice):增强的功能的方法
切面(aspect):切点+增强=切面
织入(weave):将切点与增强结合的过程叫做织入
引介(introduction):动态的为目标对象添加字段,方法...
aop案例
1.导包spring-aop.jar
spring-aspects.jar
aopalliance.jar
aspectJ.jar
2.连接点编写
public class Target {
public void show1(){
System.out.println("show1 running ...");
}
public void show2(){
System.out.println("show2 running ...");
}
public void show3(){
System.out.println("show3 running ...");
}
public void show4(){
System.out.println("show4 running ...");
}
3.增强编写
public class MyAspect {
public void before(){
System.out.println("before running .. ");
}
}
4.在applicationContext.xml中织入
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--被增强 -->
<bean id="target" class="com.itheima.aop.Target"></bean>
<!--增强 -->
<bean id="myaspect" class="com.itheima.aop.MyAspect"></bean>
<aop:config>
<aop:aspect ref="myaspect">
<aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.show1())"/>
</aop:aspect>
</aop:config>
</beans>
5.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private Target target =new Target();
@Test
public void test(){
target.show1();
}
}
细节一:增强类型
细节二:切点表达式的写法
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.*.*(..))"/>
<aop:after-returning method="afterReturing" pointcut="execution(* com.itheima.aop.Tatget.*(..))"/>
细节三:切点表达式抽取
<aop:pointcut expression="execution(* com.itheima.*.*(..)" id="mypointcut"/>
细节四:切面内部 可以配置多个切点表达式和 多个增强的组合
AOP注解方式开发
开发步骤
1.导包,同上xml方式
2.编写增强
3.编写连接点
4.注解织入
5.测试
因为我们要在java文件中写bean,那我们就要开启组件扫描,其实也可以开启全注解,个人觉得开发还是混着用比较舒服
首先在applicationContext.xml中配置组件扫描
<context:component-scan base-package="com.itheima"></context:component-scan>
其次配置aop自动代理(使java中能识别aop注解)
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
配置连接点
@Component("target")
public class Target {
public void show1(){
System.out.println("show1 running ...");
}}
配置增强
@Component("myAspect")
@Aspect
public class MyAspect {
@Before("execution(* com.itheima.aop.*.*(..))")
public void before(){
System.out.println("before running .. ");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private Target target =new Target();
@Test
public void test(){
target.show1();
}
}
ok以上就是spring aop开发笔记