Spring
什么是 Spring 框架?Spring 框架有哪些主要模块?
Spring 是一种开源轻量级框架,是为了解决企业应用程序开发复杂性而创建的
Spring 框架本身亦是按照设计模式精心打造,这使得我们可以在开发环境中安心的集成 Spring 框
架,不必担心 Spring 是如何在后台进行工作的。
Spring 框架至今已集成了20 多个模块。这些模块主要被分如下图所示的核心容器、数据访问/集
成,、Web、AOP(面向切面编程)、工具、消息和测试模块。
Spring容器中Bean默认为单例,通过@Scope方法指定 (bean的作用域
prototype:多实例,IOC容器启动的时候,并不会去调用方法创建对象,而是每次获取的时候才会调用方法去创建
singleton:单实例,IOC容器容器启动的时候就会调用方法创建对象放入到IOC容器中,以后每次获取直接从容器中拿同一个bean(大Map.get()拿)
request:主要针对web应用,递交一次请求,创建一个对象
session:同一个session创建一个实例
//Scope属性指定多例,缺省默认单例
@Scope("prototype")
@Bean
public Person person(){
return new Person("aaa", 20);
}
- @Lazy 懒加载
主要针对单实例bean,单实例bean默认在容器启动时创建,加上@Lazy注解表示容器启动时不创建对向,仅当第一次获取时才创建初始化
//懒加载机制
@Lazy
@Bean
public Person person(){
return new Person("aaa", 20);
}
- useDefaultFilters = true注解
@Configuration
//此Filters注解表示扫包时,默认只包含@Controller的注解的bean,但useDefaultFilters = true时,将其他的注解如@Service也包含进去了,因为@Controller与@Service等相关注解都属于@Component子注解
@ComponentScan(value = "com.shadow", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})},
useDefaultFilters = true
)
public class AppConfig {
@Bean
public Person person(){
return new Person("aaa", 20);
}
}
//Controller也属于Component
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
}
//ClassPathBeanDefinitionScanner类 165行
//源码部分,如果useDefaultFilters为true,则调用registerDefaultFilters方法添加Component信息
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry,
boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
this.registry = registry;
if (useDefaultFilters) {
registerDefaultFilters();
}
}
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
}
- 什么是控制反转(IOC)?什么是依赖注入?
IOC:把bean的创建、初始化、销毁交给 spring 来管理,而不是由开发者控制,实现控制反转。
依然注入有以下三种实现方式: 1. 构造器注入 2. Setter 方法注入 3. 接口注入
-
@Conditional条件注册bean
//Conditional注解条件注册bean,该注解value值必须为Condition类型
@Conditional(WindCondition.class)
@Bean
public Person person(){
return new Person("aaa", 20);
}
//上述Conditional注解配置的条件类
public class WindCondition implements Condition {
/**
* 测试条件注册bean,如果当前操作系统为windows则注入bean
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取ioc容器的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//获取当前环境信息
Environment environment = context.getEnvironment();
//environment获取当前操作系统
String property = environment.getProperty("os.name");
if(property.contains("Windows")){
return true; //返回true则注入bean
}
return false;
}
}
- @Import注册bean
@Configuration
@ComponentScan(value = "com.shadow")
@Controller
//用Import注解注册bean,可放入多个class,指定的class需要提供无参构造方法,bean的id默认为全类名
@Import({Person.class})
public class AppConfig {
}
//还可以通过ImportBeanDefinitionRegistrar自定义注册,向容器中注册bean;
//新建一个类CustomImportBeanDefinitionRegistrar实现该接口,通过Import注解导入
@Import({Person.class, CustomImportBeanDefinitionRegistrar.class})
public class AppConfig {
}
public class CustomImportBeanDefinitionRegistrar
implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//在容器中注册自定义bean
RootBeanDefinition beanDefinition = new RootBeanDefinition(Order.class);
registry.registerBeanDefinition("order", beanDefinition);
}
}