当前位置: 首页 > news >正文

Spring面试大全-基础知识01

1.什么是Spring

Spring框架是用于构建企业级Java的开源框架,他通过依赖注入和IOC容器帮我我们管理对象;支持AOP,将非业务功能(日志,事务等)从我们业务代码中分离出来,提高了代码的可维护性;提供了强大的事务管理支持,是开发人员不需要过多关注事务的控制;支持模块化设计,开发者可以根据需要选择不同的模块,例如MVC,SpringData;我们可以通过Spring方便整合其他的组件满足业务需要,Sprign的主要目标,就是简化开发,提高项目的可维护性。

2.Spring的核心模块有哪些

  1. Core Container:
    Spring的核心模块,主要提供IOC依赖注入功能的支持,对Bean对象的管理以及国际化,JMS消息服务,SPEL表达式等

SPEL表达式,主要用于配置文件和@Value注解

  1. AOP:
    提供了切面支持,将事务管理和日志等功能从业务逻辑中分离
  2. SpringDataAccess:
    支持对数据访问的支持,和对象关系映射,简化了使用JDBC访问数据库,支持了声明式事务简化开发
  3. SpringWeb:
    提供了SpringMVC的实现,支持webSocket等
  4. SpringTest:
    单元测试

3.Spring框架用到了哪些设计模式

  1. 工厂设计模式 : Spring 使用工厂模式通过 BeanFactory、ApplicationContext 创建 bean 对象。
  2. 代理设计模式:Spring的AOP功能的实现
  3. 单例设计模式:SpringBean都是默认单例的
  4. 模板方法模式:jdbcTemplate
  5. 适配器模式:SpringAOP的Advice,SpringMVC匹配Controller也使用了适配器模式

4.说一些Spring框架常用的注解有哪些

  1. @ComponentScan和@Component
    @ComponentScan会扫描加了@Component的类,并将这个类交给Spring容器管理
  2. @Controller控制层注解、@Service服务层注解、@Repository数据访问层注解
    本质上都是@Component,语义有所区别
  3. @Autowired和@Resource
    @Autowired是Spring提供的注解,按照类型进行装配
    @Resource是JDK提供的注解,默认按照名称装配,也可以指定按照类型
  4. @Configuration 声明当前类为配置类,可以替换xml配置文件
  5. @Bean
    将当前方法返回的Bean交给IOC容器管理,和@Component区别是
@Bean@Component
位置作用在方法上作用在类上
用途常用于在@Configuration定义的方法,方法返回的对象被注册成Bean交给Spring管理Spring会扫描加了@Configuration注解的类,并将他们注册为Bean
粒度可以更细粒度控制Bean,因为可以在方法中编写自定义的Bean配置的逻辑标识组件,只用于自动扫描注册,没有很惊喜的控制
  1. @Import :导入第三方包,或者导入配置类
@Configuration
public class AppConfig1 {@Beanpublic MyService myService() {return new MyService("Service from AppConfig1");}
}
@Configuration
public class AppConfig2 {@Beanpublic AnotherService anotherService() {return new AnotherService("AnotherService from AppConfig2");}
}
@Configuration
@Import({AppConfig1.class, AppConfig2.class})
public class MainConfig {}
public class MainApp {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);MyService myService = context.getBean(MyService.class);AnotherService anotherService = context.getBean(AnotherService.class);}
}
  1. @Value
    获取属性文件中的属性值

5.Spring、SpringMVC、SpringBoot有什么区别

SpringMVC是Spring的一个重要模块,帮助Spring构建MVC架构的能力,SpringBoot通过减少配置,开箱即用,简化了Spring的程序开发。

6.BeanFactory和FactoryBean有什么区别

  1. BeanFactory是Bean工厂,负责管理Bean的生命周期,是SpringIOC容器的和兴结构,ApplicationContext其实就是一种BeanFactory
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);// 通过 ApplicationContext 获取 Car 对象Car car = context.getBean(Car.class);
  1. FactoryBean也是一个bean,我们通过FactoryBean自定义Bean的生命周期
public class Car {private String brand;private String model;
}
class CarFactoryBean implements FactoryBean<Car> {private String brand;private String model;public void setBrand(String brand) {this.brand = brand;}public void setModel(String model) {this.model = model;}@Overridepublic Car getObject() throws Exception {// 在这里创建并返回实际的 Car 对象Car car = new Car();car.setBrand(brand);car.setModel(model);return car;}@Overridepublic Class<?> getObjectType() {return Car.class;}@Overridepublic boolean isSingleton() {return true;}
}
@Configuration
class AppConfig {@Beanpublic CarFactoryBean carFactoryBean() {CarFactoryBean factoryBean = new CarFactoryBean();factoryBean.setBrand("Toyota");factoryBean.setModel("Camry");return factoryBean;}
}
class MainApp {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);// 注意:获取 Car 对象时使用的是 beanName "carFactoryBean"Car car = context.getBean("carFactoryBean", Car.class);System.out.println(car);}
}

7.为什么不建议直接使用Spring的@Async

当在使用@Async时,如果不指定具体的线程池名称或者有多个线程池,那么其使用的是就是默认线程池SimpleAsyncTaskExecutor,而该线程池的默认配置为(在TaskExecutionProperties中)容量:Integer.MAX_VALUE,最大线程数:Integer.MAX_VALUE,因此不会重复利用线程,创建线程不会有限制,当线程数量到达一定程度之后,就会影响相应的性能了,因此在使用@Async注解时,最好使用自定义线程池
详情请看Spring面试大全@Async使用02

8.Spring中如何实现多环境配置

  1. 配置文件使用@Profile注解
// 开发环境配置
@Configuration
@Profile("dev")
public class DevConfig {}
//生产环境的特定配置
@Configuration
@Profile("prod")
public class ProdConfig {}
  1. 激活profile
    在配置文件中执行环境
spring:profiles:active: local
http://www.lryc.cn/news/293421.html

相关文章:

  • Transformer实战-系列教程4:Vision Transformer 源码解读2
  • cesium-水平测距
  • 【Android-Compose】手势检测实现按下、单击、双击、长按事件,以及避免频繁单击事件的简单方法
  • AUTOSAR汽车电子嵌入式编程精讲300篇-基于神经网络的CAN总线负载率优化(续)
  • python爬虫6—高性能异步爬虫
  • 日历功能——C语言
  • GPIO中断
  • springboot完成一个线上图片存放地址+实现前后端上传图片+回显
  • 编程思维与生活琐事的内在关联及其应用价值
  • OSPF排错
  • day07-CSS高级
  • 05 MP之ActiveRecord模式+SimpleQuery
  • git diff查看比对两次不同时间点提交的异同
  • 基于muduo网络库开发服务器程序和CMake构建项目 笔记
  • 前端支持下载模板、导入数据、导出数据(excel格式)
  • 编译Faiss-gpu【InterMKL】C++ 按步骤操作 基本不会有问题的 python原理相同。
  • conn.execute的用法详解
  • GetBuffer() 与 ReleaseBuffer() 使用详解
  • Flink CEP(基本概念)
  • [AIGC] Spring Gateway与 nacos 简介
  • 2024-2-3-复习作业
  • 【如何快速上手Vue.js框架——详细介绍】
  • 1Panel应用推荐:青龙定时任务管理平台
  • BUUCTF-Real-[struts2]s2-013
  • 【实战知识】使用Github Action + Nginx实现自动化部署
  • web前端--------渐变和过渡
  • docker镜像结构
  • 一个 WPF + MudBlazor 的项目模板(附:多项目模板制作方法)
  • 【数据结构与算法】之排序系列-20240203
  • C++之std::tuple(一) : 使用