spring笔记2:AOP使用

注:本文内容都来自互联网,通过自己整理用于学习,不作任何其他作用!

AOP实现:使用xml配置
1.要切入的类

public class Logger {

    public void printLog(){
        System.out.println("Logger类开始记录日志了");
    }

}

2.业务接口

public interface IAccountService {
    /**
     * 模拟保存账户
     */
    void saveAccount();

    /**
     * 更新账户
     */
    void updateAccount();

    /**
     * 删除账户
     */
    int deleteAccount();

}

3.业务接口实现类

public class AccountServiceImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("执行了保存");
    }

    public void updateAccount() {
        System.out.println("执行了更新");
    }

    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

4.application.xml配置

<?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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
>

<!--&lt;!&ndash;    普通类注入&ndash;&gt;-->
<!--    <bean id="student" class="com.aruiea.pojo.Student">-->
<!--        <property name="name" value="陈俊杰"></property>-->
<!--        <property name="age" value="27"></property>-->
<!--        <property name="ext" value="七项全能"></property>-->
<!--    </bean>-->

<!--&lt;!&ndash;    p命名空间注入&ndash;&gt;-->
<!--    <bean id="student1" class="com.aruiea.pojo.Student"  p:name="王波" p:age="50" p:ext="抽烟喝酒吹牛逼"  />-->
<!--    <bean id="student2" class="com.aruiea.pojo.Student"   />-->

<!--&lt;!&ndash;    多种方式注入&ndash;&gt;-->
<!--    <bean id="pig" class="com.aruiea.pojo.Animal">-->
<!--        <property name="name" value="猪"></property>-->
<!--        <property name="master" ref="student"/>-->
<!--&lt;!&ndash;        //注入数组friend&ndash;&gt;-->
<!--        <property name="friends">-->
<!--            <array>-->
<!--                <value>刘恒</value>-->
<!--                <value>杨柳</value>-->
<!--                <value>查浩</value>-->
<!--                <value>杨天</value>-->
<!--                <value>方知正</value>-->
<!--            </array>-->
<!--        </property>-->
<!--&lt;!&ndash;            注入list集合&ndash;&gt;-->
<!--        <property name="radio">-->
<!--            <list>-->
<!--                <value>音乐电台</value>-->
<!--                <value>鬼故事电台</value>-->
<!--                <value>运动电台</value>-->
<!--                <value>综艺电台</value>-->
<!--            </list>-->
<!--        </property>-->

<!--&lt;!&ndash;        注入set集合&ndash;&gt;-->
<!--        <property name="movie">-->
<!--            <set>-->
<!--                <value>泰坦尼克号</value>-->
<!--                <value>盗梦空间</value>-->
<!--                <value>禁闭岛</value>-->
<!--            </set>-->
<!--        </property>-->

<!--&lt;!&ndash;        注入map&ndash;&gt;-->
<!--        <property name="book" >-->
<!--            <map>-->
<!--                <entry key="1"  value="苏菲的世界"></entry>-->
<!--                <entry key="2"  value="鲁宾苏漂流记"></entry>-->
<!--                <entry key="3"  value="水浒传"></entry>-->
<!--            </map>-->
<!--        </property>-->

<!--&lt;!&ndash;        注入空值&ndash;&gt;-->
<!--        <property name="wife"><null/></property>-->

<!--&lt;!&ndash;        注入properties信息&ndash;&gt;-->
<!--        <property name="info">-->
<!--            <props>-->
<!--                <prop key="附加信息1">1994.11.2</prop>-->
<!--                <prop key="附加信息2">库存</prop>-->
<!--                <prop key="附加信息3">兴趣爱好</prop>-->
<!--                <prop key="附加信息4">943797537@qq.com</prop>-->
<!--            </props>-->
<!--        </property>-->

<!--    </bean>-->

    <!--    通过Autowired注解注入对象-->
    <!--    开启注解支持,添加扫描的包-->
<!--    <context:annotation-config />-->

<!--    普通类注入-->
<!--    <bean id="car" class="com.aruiea.IOC.pojo.Car" />-->
<!--    <bean id="animal" class="com.aruiea.IOC.pojo.Animal" />-->
<!--    <bean id="student" class="com.aruiea.IOC.pojo.Student" />-->
<!--    <bean id="student1" class="com.aruiea.IOC.pojo.Student" />-->


<!--    注册业务逻辑bean-->
    <bean id="accountService" class="com.aruiea.AOP.service.AccountServiceImpl"></bean>

<!--    新增的业务逻辑类bean-->
    <bean id="logger" class="com.aruiea.AOP.utils.Logger"></bean>

<!--    配置springAOP-->
    <aop:config>
<!--        配置切面:切入点Pointcut和通知Logger.printLog-->
        <aop:aspect id="logAdvice" ref="logger">
<!--            建立通知方法和切入点的联系-->
            <aop:before method="printLog" pointcut="execution(public void com.aruiea.AOP.service.AccountServiceImpl.saveAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

面向对象OOP的延续
AOP:面向切面编程

作用:不修改源码的情况下对代码进行增强(拓展)。

ApplicationContext三个常用实现类。
ClassPathXMLApplicationContext:加载类路径下的配置文件
AnnotationConfigurationContext:用于读取注解创建容器
——
BeanFactory是一个顶层类,它读取配置文件会延迟到需要bean的时候。

连接点:所有接口中的方法都是连接点。
切入点:执行invoke方法的地方。
前置通知:invoke方法之前的。
后置通知:invoke方法之后的。
异常通知:invoke事务中catch块中的通知。
最终通知:invoke事务中finally块中的通知。
环绕通知:整个的invoke方法。(在环绕通知中有明确的方法调用)
目标对象:就是被代理对象
织入:把增强应用到目标对象,创建代理对象的过程(Proxy代理)
切面:切入点(要增强的方法)和通知(怎么增强)之间的关系。

spring基于XML的AOP
1.导入context和aspectj包
2.编写接口和接口实现类,新增逻辑类。
3.配置application.xml
< aop:config>< aop:aspect id="" ref=“新增逻辑类” >:配置切面
——
<aop:before method=“通知方法” pointcut=“execution=(public void 包名.类名.方法名(切入点))”
——
4.测试。调用applicationcontext.getbean(“被代理对象”),返回给接口,用接口调用方法。
——
切入点表达式:excution?
——
spring-context:spring上下文,spring任何运行都要IOC的支持
——
spring-aspectj:切面相关
——

TIPS:

maven中pom.xml中packing默认是jar类型,
< packaging>pom</ packaging> ---------> 父类型都为pom类型
< packaging>jar</ packaging> ---------> 内部调用或者是作服务使用
< packaging>war</ packaging> ---------> 需要部署的项目

excution表达式
——
写法:访问修饰符 返回值 包名.类名.方法名(参数)
全统配写法:* .(**)
——
包名可以用
通配符代替,有几级包就用几个*。
还可以用*…表示当前包和子包。
——
public访问修饰符可以省略,返回值可以用代替,
——
类名和方法名也可以用
号通配。
——
方法参数列表:基本数据类型直接写,引用数据类型需要加具体包.类名
方法可以用*通配,但必须要有参数。
可以用…代替,有无参数,何种类型都可以。
——
实际开发中切入点表达式一般写法:切入到业务层实现类下的所有方法。

环绕通知:执行了通知,却没有执行被代理的方法。
原因:环绕通知必须要有明确的方法调用。.*(…):不明确
解决:
1.在通知方法参数中加入ProceedingJoinPoint pjp
2.pjp.process(参数);明确调用切入点方法,参数切入点方法参数
3.通过pjp.getArgs()得到所需参数,赋给数组
4.创建返回值rtValue并修改返回值。返回值=pjp.proceed(args).在这里插入图片描述
在这里插入图片描述

使用注解实现AOP
首先先在spring.xml配置组件扫描包。< context-component-scan base-package=“包”>
——
并且配置spring开启AOP的支持< aop:aspectj-autoproxy>

1.给被代理类和接口加入到spring组件中
2.@Aspect增加到增强类上,表示这是一个切面。
3.在增强类的增强通知上,写上@Around等各种环绕。由于四种通知用注释的顺序不固定,因此要慎重使用。
4.定义一个方法private void p(){},加上@Pointcut(excution(void .*(…)))配置切入点表达式
5.给每个增强方法上加上切入点,比如@Around(“p()”)
6.测试。向上转型,多态。接口 ac = (接口)applicationContext.getbean(“被代理类”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值