spring AOP---【小白系列】0基础到熟练应用spring框架(三)

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开发笔记

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LawsonJin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值