Spring面试题(下)

2024-09-08 04:18
文章标签 java 面试题 spring

本文主要是介绍Spring面试题(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring面试题(下)

文章目录

  • Spring面试题(下)
    • 1.什么是面向切面编程(AOP)
    • 2.面向切面编程(AOP)的作用
    • 3.AOP的实现方式
    • 4.事务控制的场景
    • 5.如何实现AOP
    • 6.spring中实现通知
    • 7.spring中基于AOP实现事务
    • 8.spring中实现声明式事务

1.什么是面向切面编程(AOP)

  • AOP:全称是Aspect Oriented Programming 即:面向切面编程
  • 简单的说就是我们把重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的原有代码进行增强。

2.面向切面编程(AOP)的作用

  • 在程序运行期间,不修改源代码对已有方法进行增强
  • 减少重复代码
  • 提高效率
  • 维护方便
  • 可用来做:日志记录、性能统计、安全控制、事务处理、异常处理

3.AOP的实现方式

  • 使用动态代理技术

4.事务控制的场景

  • 使用自动提交的方式来控制事务,在每次都只执行一条sql语句的时候,没问题,但是如果一个业务要执行多条sql这种方式就无法正确的实现事务控制。
  • 例如经典的转账问题,使用自动事务控制就会产生A的账户已经扣钱了,B的账户却没加钱的情况。
  • 我们可以自定义一个事务控制类,实现事务的开启、提交、回滚等操作。
  • 但是使用这种办法业务层的代码会很臃肿。

5.如何实现AOP

  • 使用JDK官方的Proxy类(基于接口的动态代理,要求被代理类至少实现一个接口)

  • 使用第三方cglib的Enhancer类(基于子类的动态代理)

  • public class Client {public static void main(String[] args) {final Producer producer = new Producer();IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),producer.getClass().getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object ret = null;Float money = (Float) args[0];if ("saleProduct".equals(method.getName())){ret = method.invoke(producer,money*0.8f);}return ret;}});proxyProducer.saleProduct(10000f);Producer cglibProducer = (Producer)Enhancer.create(producer.getClass(), new MethodInterceptor() {public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {Object ret = null;Float money = (Float) args[0];if ("saleProduct".equals(method.getName())) {ret = method.invoke(producer, money * 0.8f);}return ret;}});cglibProducer.saleProduct(10000f);}
    }
    

6.spring中实现通知

  • 第一步:用bean标签注入通知类

  • 第二步:使用aop:config 声明aop配置

  • 第三步:使用aop:aspect 配置切面

  • 第四步:使用aop:pointcut配置切入点表达式

  • 第五步:使用aop:xxx 配置对应的通知类型(前置通知、后置通知、异常通知、最终通知)

  • <?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置spring的ioc,把service对象配置进来 --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><!-- 配置Logger类 --><bean id="logger" class="com.itheima.utils.Logger"></bean><!-- 配置AOP --><aop:config><!-- 配置切入点表达式 --><aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut><!-- 配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的类型,并且建立通知方法和切入点方法的关联 --><aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before><aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning><aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing><aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after><!-- 配置环绕通知 -->
    <!--            <aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>--></aop:aspect></aop:config>
    </beans>
    
  • 也可以用注解的方法配置

  • <?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置spring创建容器要扫描的包 --><context:component-scan base-package="com.itheima"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    
  • package com.itheima.utils;import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.stereotype.Component;/*** @author chx* @version 1.0* @description: TODO** @date 2021/1/25 0025 9:59*/
    @Component("logger")
    @Aspect
    public class Logger {@Pointcut("execution(* com.itheima.service.impl.*.*(..))")private void pt1(){}/*** 前置通知*/@Before("pt1()")public void beforePrintLog(){System.out.println("Logger类中的pringLog方法开始记录日志了。。。");}/*** 后置通知*/@AfterReturning("pt1()")public void afterReturningPrintLog(){System.out.println("Logger类中的afterReturningPrintLog方法开始记录日志了。。。");}/*** 异常通知*/@AfterThrowing("pt1()")public void afterThrowingPrintLog(){System.out.println("Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");}/*** 最终通知*/@After("pt1()")public void afterPrintLog(){System.out.println("Logger类中的afterPrintLog方法开始记录日志了。。。");}/*** 环绕通知*///@Around("pt1()")public Object aroundPrintLog(ProceedingJoinPoint pjp){Object rtValue = null;try {Object[] args = pjp.getArgs();System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。前置");rtValue = pjp.proceed();//明确调用业务层方法System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。后置");return rtValue;} catch (Throwable throwable) {System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。异常");throw new RuntimeException(throwable);}finally {System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。最终");}}
    }
    

7.spring中基于AOP实现事务

  • 编写获取连接的工具类,用于从数据源中获取一个连接并且实现和线程绑定

  • package com.itheima.utils;import javax.sql.DataSource;
    import java.sql.Connection;/*** @author chx* @version 1.0* @description: TODO* 连接的工具类,它用于从数据源中获取一个连接并且实现和线程绑定* @date 2021/1/24 0024 14:32*/
    public class ConnectionUtils {private ThreadLocal<Connection> tl = new ThreadLocal<Connection>();private DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}/*** 获取当前线程上的连接*/public Connection getThreadConnection(){//1.先从ThreadLocal上获取Connection conn = tl.get();try {//2.判断当前线程上是否有连接if(conn==null){//3.从数据源获取连接,并且和存入ThreadLocal中conn = dataSource.getConnection();tl.set(conn);}} catch (Exception e) {e.printStackTrace();}return conn;}/*** 解绑*/public void removeConnection(){tl.remove();}
    }
    
  • 编写事务管理相关的工具类

  • package com.itheima.utils;import java.sql.SQLException;/*** @author chx* @version 1.0* @description: TODO* 事务管理相关的工具类* @date 2021/1/24 0024 14:41*/
    public class TransactionManager {private ConnectionUtils connectionUtils;public void setConnectionUtils(ConnectionUtils connectionUtils) {this.connectionUtils = connectionUtils;}/*** 开启事务*/public void beginTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);} catch (Exception e) {e.printStackTrace();}}/*** 提交事务*/public void commit(){try {connectionUtils.getThreadConnection().commit();} catch (Exception e) {e.printStackTrace();}}/*** 回滚事务*/public void rollback(){try {connectionUtils.getThreadConnection().rollback();} catch (Exception e) {e.printStackTrace();}}/*** 释放连接*/public void release(){try {connectionUtils.getThreadConnection().close();connectionUtils.removeConnection();} catch (Exception e) {e.printStackTrace();}}
    }
    
  • 修改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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><bean id="accountDao" class="com.itheima.dao.impl.AccountDao"><property name="runner" ref="runner"></property><property name="connectionUtils" ref="connectionUtils"></property></bean><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean><!-- 配置数据库 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--连接数据库的必备信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="xxxx"></property></bean><!--配置Connection的工具类--><bean id="connectionUtils" class="com.itheima.utils.ConnectionUtils"><property name="dataSource" ref="dataSource"></property></bean><bean id="txManageer" class="com.itheima.utils.TransactionManager"><property name="connectionUtils" ref="connectionUtils"></property></bean><!--配置aop--><aop:config><!--配置通用切入点表达式--><aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut><aop:aspect id="txAdvice" ref="txManageer"><!--配置前置通知:开启事务--><aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before><!--配置后置通知:提交事务--><aop:after-returning method="commit" pointcut-ref="pt1"></aop:after-returning><!--配置异常通知:回滚事务--><aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing><!--配置最终通知:释放连接--><aop:after method="release" pointcut-ref="pt1"></aop:after></aop:aspect></aop:config></beans>
    

8.spring中实现声明式事务

  • 第一步:配置事务管理器

  • 第二步:配置事务通知(引用事务管理器)

  • 第三步:配置事务的属性

  • 第四步:配置AOP切入点表达式

  • 第五步:配置切入点表达式和事务通知的对应关系

  • <?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置业务层 --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/eesy"></property><property name="username" value="root"></property><property name="password" value="0000"></property></bean><!-- 配置账户的持久层 --><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"></property></bean><!-- spring中基于xml的声明式事务控制配置步骤1.配置事务管理器2.配置事务的通知3.配置AOP中的通用表达式切入点表达式4.建立事务通知和切入点表达式的对应关系5.配置事务的属性--><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 指定方法名称:是业务核心方法read-only:是否是只读事务。默认 false,不只读。isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。propagation:指定事务的传播行为。timeout:指定超时时间。默认值为:-1。永不超时。rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。--><!-- 配置事务的通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 配置AOP --><aop:config><!-- 配置切入点表达式 --><aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"/><!-- 建立切入点表达式和事务通知的关系 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor></aop:config>
    </beans>
    

这篇关于Spring面试题(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147086

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S