SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(三)

简介: SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(三)

⑤. 基于XML的AOP开发


  • ①. 导入依赖


<properties>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <!--导入spring的context坐标,context依赖core、beans、expression-->
    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
        <!-- aspectj的织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>


②. 创建目标接口和目标类(内部有切点)


  //创建目标接口和目标类(内部有切点)
  public interface TargetInterface {
      public void method();
  }
  public class Target implements TargetInterface {
      @Override
      public void method() {
          System.out.println("Target running....");
      }
  }
  //创建切面类(内部有增强方法)
  public class MyAspect {
      //前置增强方法
      public void before(){
          System.out.println("前置代码增强.....");
      }
  }


③. 基于xml的形式进行配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--目标对象-->
    <bean id="target" class="com.xiaozhi.aop.Target"/>
    <!--切面对象-->
    <bean id="myAspect" class="com.xiaozhi.aop.MyAspect"></bean>
    <!--配置织入:告诉Spring框架,哪些方法(切点)需要进行哪些增强(前置 | 后置)-->
    <aop:config>
          <!--声明切面[告诉spring哪个类是切面类]-->
          <aop:aspect ref="myAspect">
          <!--切面:切入点+通知-->
              <!--通知
              method:切面类中增强的方法
              pointcut:切入点表达式
              -->
              <!--前置通知-->
              <aop:before method="before" pointcut="execution(public void com.xiaozhi.aop.Target.save())"></aop:before>
          </aop:aspect>
    </aop:config>
</beans>


④. 进行测试


  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:applicationContext.xml")
  public class AopTest {
      @Autowired
      private TargetInterface target;
      @Test
      public void test1(){
          target.method();
      }
  }
相关文章
|
6天前
|
监控 Java Spring
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
34 16
|
16天前
|
XML 安全 Java
Spring AOP—深入动态代理 万字详解(通俗易懂)
Spring 第四节 AOP——动态代理 万字详解!
69 24
|
16天前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
104 26
|
19天前
|
缓存 Java 数据库
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
169 89
|
2月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
62 21
|
2月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
183 73
|
2月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
74 6
|
4月前
|
XML Java 数据格式
手动开发-简单的Spring基于XML配置的程序--源码解析
手动开发-简单的Spring基于XML配置的程序--源码解析
107 0
|
5月前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
346 18
|
9月前
|
XML Java 数据格式
Spring高手之路18——从XML配置角度理解Spring AOP
本文是全面解析面向切面编程的实践指南。通过深入讲解切面、连接点、通知等关键概念,以及通过XML配置实现Spring AOP的步骤。
129 6
Spring高手之路18——从XML配置角度理解Spring AOP