[Spring03]:Spring的aop和简单实现

Spring的aop和简单实现

什么是AOP?

  1. 面向切面编程,一个项目有很多不同的业务功能,把这些业务功能,当成核心点,而还有很多非核心点,比如日志,权限控制等,我们想把这些非核心的点加入到核心点中,那我们就可以使用AOP,把核心点切开一个个面.加入其中.
  2. 切面的目的是加强方法.
  3. AOP的目的是为了减少代码重复,去降低各个业务模块之间的耦合度.

怎么实现AOP?

  1. 导包,(aspectjweaver)
  2. 写一个增强类,类中有增强方法.
  3. 在Spring配置文件中添加AOP配置.(你要告诉Spring那个类是增强类?在什么时候要执行增强方法?)
  4. 执行测试

:-结构:
在这里插入图片描述
:-pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.fan.springtest</groupId>
   <artifactId>spring-test</artifactId>
   <version>1.0.0</version>

   <dependencies>
       <!--spring基础包-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.2.4.RELEASE</version>
       </dependency>
       <!--spring测试包-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>5.2.4.RELEASE</version>
       </dependency>
       <!--测试包-->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
       <!--javaBean-->
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>1.18.12</version>
       </dependency>
       <!--aspectjweaver-->
       <dependency>
           <groupId>org.aspectj</groupId>
           <artifactId>aspectjweaver</artifactId>
           <version>1.9.5</version>
       </dependency>
</project>

:-增强类AopEnhance:

package com.fan.spring.aop.enhance;

import org.springframework.stereotype.Component;

@Component
public class AopEnhance {
   public void add(){
       System.out.println("我已经增强了该方法");
   }
}

:-spring配置文件:

<?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: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/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">
  <!--ioc注解扫描器-->
  <context:component-scan base-package="com.fan.spring.aop"/>
  <!--DI解析器-->
  <context:annotation-config/>

 <!--1.未使用注解的方式-->
  <aop:config>
      <!--1.加入增强类-->
      <aop:aspect ref="aopEnhance">
          <!--1.1.需要增强什么方法-->
          <aop:pointcut id="add" expression="execution(* aop*(..))"/>
          <!--1.2.在什么时候增强哪些方法-->
          <aop:before method="add" pointcut-ref="add"/>
      </aop:aspect>
  </aop:config>
</beans>

:-核心方法接口:

package com.fan.spring.aop.service;

public interface IAopService {

 void aopSay();
}

:-核心实现:

package com.fan.spring.aop.service.impl;

import com.fan.spring.aop.service.IAopService;
import org.springframework.stereotype.Service;

@Service
public class AopServiceImpl implements IAopService {
  public void aopSay() {
      System.out.println("需要被增强的方法");
  }
}

:-测试类:

package com.fan.spring.aop.test;

import com.fan.spring.aop.service.IAopService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config-aop.xml")
public class AopTest {
   @Autowired
   private IAopService iAopService;

   @Test
   public void aopTest(){
       iAopService.aopSay();
   }
}

:-结果
在这里插入图片描述

总结

  1. 不要忘记导包
  2. 知道怎么做,知道怎么说

捷径才是最远的路,不要把未来交给明天的自己.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值