1.什么是Spring
Spring框架是用于构建企业级Java的开源框架,他通过依赖注入和IOC容器帮我我们管理对象;支持AOP,将非业务功能(日志,事务等)从我们业务代码中分离出来,提高了代码的可维护性;提供了强大的事务管理支持,是开发人员不需要过多关注事务的控制;支持模块化设计,开发者可以根据需要选择不同的模块,例如MVC,SpringData;我们可以通过Spring方便整合其他的组件满足业务需要,Sprign的主要目标,就是简化开发,提高项目的可维护性。
2.Spring的核心模块有哪些
- Core Container:
Spring的核心模块,主要提供IOC依赖注入功能的支持,对Bean对象的管理以及国际化,JMS消息服务,SPEL表达式等
SPEL表达式,主要用于配置文件和@Value注解
- AOP:
提供了切面支持,将事务管理和日志等功能从业务逻辑中分离 - SpringDataAccess:
支持对数据访问的支持,和对象关系映射,简化了使用JDBC访问数据库,支持了声明式事务简化开发 - SpringWeb:
提供了SpringMVC的实现,支持webSocket等 - SpringTest:
单元测试
3.Spring框架用到了哪些设计模式
- 工厂设计模式 : Spring 使用工厂模式通过 BeanFactory、ApplicationContext 创建 bean 对象。
- 代理设计模式:Spring的AOP功能的实现
- 单例设计模式:SpringBean都是默认单例的
- 模板方法模式:jdbcTemplate
- 适配器模式:SpringAOP的Advice,SpringMVC匹配Controller也使用了适配器模式
4.说一些Spring框架常用的注解有哪些
- @ComponentScan和@Component
@ComponentScan会扫描加了@Component的类,并将这个类交给Spring容器管理 - @Controller控制层注解、@Service服务层注解、@Repository数据访问层注解
本质上都是@Component,语义有所区别 - @Autowired和@Resource
@Autowired是Spring提供的注解,按照类型进行装配
@Resource是JDK提供的注解,默认按照名称装配,也可以指定按照类型 - @Configuration 声明当前类为配置类,可以替换xml配置文件
- @Bean
将当前方法返回的Bean交给IOC容器管理,和@Component区别是
@Bean | @Component | |
---|---|---|
位置 | 作用在方法上 | 作用在类上 |
用途 | 常用于在@Configuration定义的方法,方法返回的对象被注册成Bean交给Spring管理 | Spring会扫描加了@Configuration注解的类,并将他们注册为Bean |
粒度 | 可以更细粒度控制Bean,因为可以在方法中编写自定义的Bean配置的逻辑 | 标识组件,只用于自动扫描注册,没有很惊喜的控制 |
- @Import :导入第三方包,或者导入配置类
@Configuration
public class AppConfig1 {
@Bean
public MyService myService() {
return new MyService("Service from AppConfig1");
}
}
@Configuration
public class AppConfig2 {
@Bean
public 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);
}
}
- @Value
获取属性文件中的属性值
5.Spring、SpringMVC、SpringBoot有什么区别
SpringMVC是Spring的一个重要模块,帮助Spring构建MVC架构的能力,SpringBoot通过减少配置,开箱即用,简化了Spring的程序开发。
6.BeanFactory和FactoryBean有什么区别
- BeanFactory是Bean工厂,负责管理Bean的生命周期,是SpringIOC容器的和兴结构,ApplicationContext其实就是一种BeanFactory
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// 通过 ApplicationContext 获取 Car 对象
Car car = context.getBean(Car.class);
- 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;
}
@Override
public Car getObject() throws Exception {
// 在这里创建并返回实际的 Car 对象
Car car = new Car();
car.setBrand(brand);
car.setModel(model);
return car;
}
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
@Configuration
class AppConfig {
@Bean
public 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中如何实现多环境配置
- 配置文件使用@Profile注解
// 开发环境配置
@Configuration
@Profile("dev")
public class DevConfig {}
//生产环境的特定配置
@Configuration
@Profile("prod")
public class ProdConfig {}
- 激活profile
在配置文件中执行环境
spring:
profiles:
active: local