Spring注解驱动开发-AOP、Tx和Servlet3.0

1 AOP

1.1 什么是AOP?

  • 在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。
  • 底层就是动态代理。

1.2 AOP的应用

  • 步骤:

    • ①定义一个目标类以及目标方法。
    • ②定义一个切面类,以及前置通知、后置通知等,使用各自的注解将通知织入到目标方法上。
    • ③将目标类和切面类注册到容器中。
    • ④在切面类上标注@Aspect注解,来告诉Spring这是一个切面类。
    • ⑤在配置类上加上@EnableAspectJAutoProxy开启AspectJ自动代理。  
  • 示例:

    • MathCalculator.java  
package com.xuweiwei.spring.aop;

/**
 * @describe: 定义一个业务逻辑类MathCalculator:在业务逻辑类运行的时候将日志进行打印(方法之前、方法之后、方法正常返回结果、方法出现异常信息)等
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
public class MathCalculator {

    public int div(int a, int b) {
        return a / b;
    }

}
    • LogAspect.java  
package com.xuweiwei.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @describe: 定义一个日志切面类LogAspect:切面类里面的方法需要动态的感知MathCalculator.div运行到那个阶段。
 * 所谓的通知方法:
 * 前置通知(@Before):在目标方法运行之前执行
 * 后置通知(@After):在目标方法运行之后执行
 * 返回通知(@AfterReturning):在目标方法正常返回之后运行
 * 异常通知(@AfterThrowing):在目标方法出现异常以后运行
 * 环绕通知(@Around):动态代理,手动推进目标方法运行
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@Aspect //标注这个类是切面类
public class LogAspect {

    @Pointcut("execution(* com.xuweiwei.spring.aop.MathCalculator.div(..))")
    public void pointcut() {

    }

    @Before(value = "pointcut()")
    public void logStart(JoinPoint joinpoint) {
        Object[] args = joinpoint.getArgs();
        System.out.println(joinpoint.getSignature().getName() + "方法开始^_^,参数列表是:" + Arrays.asList(args));
    }

    @After(value = "pointcut()")
    public void logEnd(JoinPoint joinpoint) {
        System.out.println(joinpoint.getSignature().getName() + "方法结束^_^");
    }

    @AfterReturning(value = "pointcut()", returning = "val")
    public void logReturn(JoinPoint joinpoint, Object val) {
        System.out.println(joinpoint.getSignature().getName() + "方法正常返回……运行结果:{" + val + "}");
    }

    @AfterThrowing(value = "pointcut()", throwing = "ex")
    public void logThrow(Exception ex) {
        System.out.println("除法出现异常……异常信息:{" + ex.getMessage() + "}");
    }

}
    • 配置类:将目标类和切面类都加入到Spring的容器中,并开启AspectJ自动代理  
package com.xuweiwei.spring.config;

import com.xuweiwei.spring.aop.LogAspect;
import com.xuweiwei.spring.aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @describe:
 * @author: 不为往事扰,余生只爱笑。
 * @version: 1.0
 */
@EnableAspectJAutoProxy
@Configuration
public class MainConfig {

    @Bean
    public MathCalculator mathCalculator(){
        return new MathCalculator();
    }

    @Bean
    public LogAspect logAspect(){
        return new LogAspect();
    }

}
    • 测试  
package com.xuweiwei.sping;

import com.xuweiwei.spring.aop.MathCalculator;
import com.xuweiwei.spring.config.MainConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ConfigurationSpringTest {

    @Test
    public void test() {
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);

        MathCalculator mathCalculator = context.getBean(MathCalculator.class);

        int result = mathCalculator.div(1, 1);

        System.out.println("返回结果:" + result);
    }

}

原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/10509747.html

时间: 2024-07-30 18:03:43

Spring注解驱动开发-AOP、Tx和Servlet3.0的相关文章

Spring注解驱动开发--AOP功能测试

前言 Spring的AOP指的是在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式[动态代理]. AOP功能测试 ①导入AOP模块 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </depend

Spring注解驱动开发(一)--项目搭建

一. 前言 <Spring注解驱动开发>系列文章是基于Spring的4.3.11.RELEASE版本,通过注解的方式进行开发演示. 二. 项目搭建 1.依赖包引用 创建一个maven工程,引入相关的依赖包.我们以依赖最少的原则只引用spring-context和junit包. <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>s

Spring注解驱动开发(二)--组件注入

一.前言 上一篇我们搭建了一个简单的Spring项目,并简单的使用了 组件注册.这一篇中,我们来详细的讲解组件注入. 二.组件注入 1. @ComponentScan 在上一篇中,我们使用了@Configuration和@Bean实现了组件注入.但是如果需要注入的组件很多的情况下,每个组件都需要通过一个@Bean注解进行注入,这样就会很麻烦.所以Spring提供了@ComponentScan注解. @ComponentScan可以指定需要扫描的包,在这些包下,@Component注解标注的组件都

Spring注解驱动开发(七)-----servlet3.0、springmvc

ServletContainerInitializer Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContainerInitializer的实现2.提供ServletContainerInitializer的实现类: 必须绑定在,META-INF/services/javax.servlet.ServletContainerInitializer 文件

Spring注解驱动开发(四)-----aop、声明式事务

AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects)-----导入相关jar包 2.MathCalculator-----一个业务逻辑的类-----在业务逻辑运行的时候将日志进行打印(方法之前.方法运行结束.方法出现异常,xxx) package com.atguigu.aop; public class MathCalculator { publ

Spring注解驱动开发(一)-----组件注册

注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

Spring注解驱动开发(五)-----扩展原理

扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFactory的后置处理器在BeanFactory标准初始化之后调用,来定制和修改BeanFactory的内容:所有的bean定义已经保存加载到beanFactory,但是bean的实例还未创建. 注:首先spring容器会创建beanDefinition,此时bean并没有初始化. 示例: ExtConfi

Spring注解驱动开发(六)-----spring容器创建【源码】

Spring容器的refresh()[创建刷新] 1.prepareRefresh()刷新前的预处理 1).initPropertySources()初始化一些属性设置;子类自定义个性化的属性设置方法: 2).getEnvironment().validateRequiredProperties();检验属性的合法等 3).earlyApplicationEvents= new LinkedHashSet<ApplicationEvent>();保存容器中的一些早期的事件: 2.obtainF

Spring注解驱动开发--属性赋值

前言 在实际开发当中,Spring中bean的属性直接赋值用的不是太多,整理这方面的资料,做一个小结,以备后续更深入的学习. 通过配置文件的方式 以配置文件的方式启动spring容器时,可以使用property标签的value给bean的属性赋值,赋值的形式有以下几种: <--通过context:property-placeholder将properties文件中的值加载的环境变量中(properties中的属性值最终是以环境变量的形式存储的)><context:property-pla