系统常用的三种ApplicationContext:
ClassPathXmlApplicationContext:从类路径中加载配置文件初始化上下文
FileSystemApplicationContext:从文件系统中加载配置文件初始化上下文
AnnotationConfigApplicationContext:通过注解配置初始化上下文
WebApplicationContext:web应用的上下文
常用注解:
@Component:常用于组件注册
@Repository:常用于DAO层注册
@Service:常用于业务接口实现类注册
@Configuration:配置类,相当于一个配置文件,作用于类上面
@Controller:常用于解析请求
@RestController:同上,该注解下的方法全部返回数据
@RequestMapping:请求映射路径
@GetMapping:get请求映射
@PostMapping:post请求映射
@RequestBody:获得post请求body的内容
@ResponseBody:将返回json响应内容
高级注解:
@ComponentScan:配置要扫描的包。作用于类上面。
@ComponentScans:相当于指定多个@ComponentScan
@Scope:指定实例的生成规则,默认为单实例
@Import:快速给容器导入组件(ImportSelector:导入选择器,id默认是全类名,作为@Import的值使用)
@PostConstruct:类初始化的时候调用
@PreDestroy:类销毁的时候调用
@Conditional:按条件注册实例或者配置管理类。value值为实现Condition的类
@ComponentScan使用详解:
basePackages和value作用相等,表示要扫描的包及其该包下面的子包
useDefaultFilters:是否使用默认的扫描过滤器,默认为true
lazyInit:是否使用懒加载机制,默认为false
excludeFilters:指明要被排除的注解的规则,当useDefaultFilters为true的时候配合使用。
includeFilters:指明要被扫描的注解的规则,当useDefaultFilters为false的时候配合使用。
FilterType:ANNOTATION(按注解进行扫描),ASSIGNABLE_TYPE(按指定的类型进行扫描),
ASSIGNABLE_TYPE(使用ASPECTJ表达式,不经常使用),REGEX(按正则表达式进行扫描),
CUSTOM(自定义规则,该规则需要实现TypeFilter接口)
public class CustomeTypeFilter implements TypeFilter {
/**
*
* @param metadataReader 读取正在扫描的类的信息
* @param metadataReaderFactory 读取其他任何类的信息
* @return
* @throws IOException
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//获取当前类的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前类的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
String className = classMetadata.getClassName();
//获取当前类的资源信息
Resource resource = metadataReader.getResource();
if(className.contains("er")){
return true;
}
return false;
}
}
@Scope详解:
value值:
singleton:单实例,等同于ConfigurableBeanFactory#SCOPE_SINGLETON
prototype:多实例,等同于ConfigurableBeanFactory#SCOPE_PROTOTYPE
request:每次请求产生一个实例,等同于org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
session:每个会话产生一个实例,等同于org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
globalSession:全局会话产生一个实例,等同于org.springframework.web.context.WebApplicationContext#SCOPE_GLOBAL_SESSION //全局会话产生一个实例
@Conditional详解:
public class WindowCondition implements Condition {
/**
*
* @param conditionContext 判断条件能使用的上下文环境
* @param annotatedTypeMetadata 注释信息
* @return
*/
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取ioc当前的类工厂
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取类加载器
ClassLoader contextClassLoader = conditionContext.getClassLoader();
//获取系统环境变量
Environment environment = conditionContext.getEnvironment();
//获取bean定义的注册类
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//获取资源加载器
ResourceLoader resourceLoader = conditionContext.getResourceLoader();
if(registry.containsBeanDefinition("person") && environment.getProperty("os.name").toLowerCase().contains("window")){
return true;
}
return false;
}
}
@Configuration
public class SpringConfig {
@Conditional(value = {WindowCondition.class})
@Bean
public Person person01() {
return new Person("bill", 60);
}
}
@Import详解:
/**
* ImportSelector:导入选择器,id默认是全类名,作为@Import的值使用。
*/
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.wyn.bean.Red"};
}
}
/**
* ImportBeanDefinationRegister:导入bean定义注册器,作为@Import的值使用。
*/
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
boolean green = beanDefinitionRegistry.containsBeanDefinition("com.wyn.bean.Green");
boolean red = beanDefinitionRegistry.containsBeanDefinition("red");
if(green && red){
RootBeanDefinition rainBow = new RootBeanDefinition(RainBow.class);
rainBow.setScope(RootBeanDefinition.SCOPE_SINGLETON);
rainBow.setLazyInit(true);
beanDefinitionRegistry.registerBeanDefinition("rainBow", rainBow);
}
}
}
@Import(value = {Green.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class SpringConfig {
}
更多问题可以加微信公众号:代码小栈,期待为您解决更多问题