419、Java框架73 -【Spring - AOP】 2020.11.27

132 篇文章 0 订阅
8 篇文章 0 订阅

目录

0、AOP

1、Java包

2、思路图

3、准备业务类 ProductService

4、TestSpring

5、准备日志切面 LoggerAspect

6、applicationContext.xml

7、TestSpring

8、参考链接


 

0、AOP

AOP 即 Aspect Oriented Program 面向切面编程 
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 
所谓的周边功能,比如性能统计,日志,事务管理等等 

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP

1、Java包

为了支持AOP,需要用到一些额外的JAR包。 这些额外的JAR包放在右上角的可运行项目,解压出来的lib目录下。 
至于多了哪些,记不太清楚了。。。。 都导入吧。。。。

2、思路图

1. 功能分两大类,辅助功能和核心业务功能
2. 辅助功能和核心业务功能彼此独立进行开发
3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程æè·¯å¾

 

3、准备业务类 ProductService

  • ProductService
package com.how2java.service;
 
public class ProductService {
     
    public void doSomeService(){
         
        System.out.println("doSomeService");
         
    }
     
}

4、TestSpring

在引入切面之前,调用该业务类

TestSpring

package com.how2java.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

5、准备日志切面 LoggerAspect

该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。
 

Object object = joinPoint.proceed();


就是将来与某个核心功能编织之后,用于执行核心功能的代码

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
 
public class LoggerAspect {
 
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

 

6、applicationContext.xml

<bean name="s" class="com.how2java.service.ProductService">

</bean>


声明业务对象

<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>


声明日志切面

结合思路图

<aop:pointcut id="loggerCutpoint"

expression=

"execution(* com.how2java.service.ProductService.*(..)) "/>


指定右边的核心业务功能
 

<aop:aspect id="logAspect" ref="loggerAspect">

<aop:around pointcut-ref="loggerCutpoint" method="log"/>

</aop:aspect>


指定左边的辅助功能
然后通过aop:config把业务对象与辅助功能编织在一起。
 

execution(* com.how2java.service.ProductService.*(..))


这表示对满足如下条件的方法调用,进行切面操作:
* 返回任意类型
com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 开头的类的任意方法
(..) 参数是任意数量和类型

<?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"
    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-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="yyy" />
    </bean>
 
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>
     
    <bean name="s" class="com.how2java.service.ProductService">
    </bean>   
     
    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
     
    <aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.how2java.service.ProductService.*(..)) "/>
             
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>    
  
</beans>

7、TestSpring

TestSpring 代码没有发生任何变化,通过配置的方式,把切面和核心业务类编制在了一起。

运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志

TestSpring

package com.how2java.test;
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
  
public class TestSpring {
  
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

8、参考链接

[01] How2j - Spring - Spring - AOP

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值