Hello world
<parent>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-parent </artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web </artifactId>
<!--没有版本号-->
</dependency>
配置类
@SpringBootApplication
public class ApplicationConfig {
public static void main(String[] args){
SpringApplication.run(ApplicationConfig.class,args);
}
}
控制层
@RestController
public class Examlpe {
@RequestMapping("/hello")
String home(){
return "hello world";
}
}
执行 如下图 端口8080 页面加载,本机IP加 端口加映射 名字 localhost:8080/hello
Spring 的组件代码是轻量级的,但它的配置却是重量级的。
一,概念
Spring两大核心:基于工厂模式IOC(DI)和基于动态代理AOP。
其中IOC(DI)是指控制器反转(依赖注入),原来要使用某个类对象实例是必须自己创建,使用spring后就不需要自己创建,由spring创建,需要时直接从spring中获取并且有依赖关系是会spring会通过反射自动注入。
AOP就是不影响正常执行过程的前后加入额外的逻辑。比如权限,日志等,该执行的业务逻辑正常执行知识可以进行权限的判断核日志记录。
第一阶段:xml配置
在Spring 1.x时代,使用Spring开发满眼都是xml配置的Bean,随着项目的扩大,我们需要把xml配置文件放到不同的配置文件里,那时需要频繁的在开发的类和配置文件之间进行切换
第二阶段:注解配置
在Spring 2.x 时代,随着JDK1.5带来的注解支持,Spring提供了声明Bean的注解(例如@Component、@Service),大大减少了配置量。主要使用的方式是应用的基本配置(如数据库配置)用xml,业务配置用注解
第三阶段:java配置
Spring 3.0 引入了基于 Java 的配置能力,这是一种类型安全的可重构配置方式,可以代替 XML。我们目前刚好处于这个时代,Spring4.x和Spring Boot都推荐使用Java配置。
二,概念 问题
1.parent - spring-boot-starter-parent 的作用?SpringBoot的父工程,管理了很多很多的jar包
<dependencies> : 导入依赖,如果一个父工程使用 dependencies导入依赖 ,
那么这个dependencies里面的所有依赖都会被子模块直接继承使用 -> 放所有子模块公共的jar包 ,test包 。
<dependencyManagement> : 如果一个父工程使用 dependencyManagement 导入依赖 ,
那么这个dependencyManagement里面的所有依赖,是不能直接被只模块使用的。
如果子项目要使用 dependencyManagement里面的jar包 ,就需要在子模块的 <dependencies> 去导入jar包
但是,版本号不用写,使用户工程的版本号 -> 管理版本号 , 除了所有子模块都能用到的包以外的其他包放到 dependencyManagement
2.spring-boot-starter-web 包的作用? 用来整合web层的一个依赖,包括了(SpringMvc,日志,tomcat,json,自动配置包)
3.@RestController的作用? 是一个组合标签 @Controller + @ResponseBody
4.@EnableAutoConfiguration的作用? 启用自动配置,自动配置前段控制器,视图解析器等等
@EnableAutoConfiguration -> AutoConfigurationImportSelector(选择器) -> classpath的jar包中加载 META-INF/spring.factories -> 加载自动配置类
5.SpringApplication.run的作用?
启动SpringBoot应用 ,准备应用数据和环境 ,创建加载容器 , 加载注册Bean(初始化bean…),解析配置类 ,做自动配置 , 项目打包到内嵌的tomcat ,运行项目
6.Tomcat 是哪儿来的? SpringBoot内嵌tomcat , 就是通过 spring-boot-starter-web 包导入了tomcat
7.项目结构(jar) ? SpringBoot默认的项目打包方式就是 jar
8.前段控制器,视图解析器等等相关配置为什么没有手动配?自动配置已经帮我们配置了
三,spring的配置
@SpringBootApplication:
包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration。
@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。
@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。
@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的context:component-scan(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring容器管理。
ImportSelector:导入选择器
// 导入选择器
public class MyImportSelector implements ImportSelector {
//方法 返回 类 交给 容器管理
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"cn.itsource._08_import_selector.MyBean","cn.itsource._08_import_selector.OtherBean"};
}
}
//配置类
@Configuration
@Import(value = MyImportSelector.class)
public class AppplicationConfig {
}
ImportBeanDefinitionRegistrar:注册器
//bean 注册器
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
//BeanDefinitionRegistry 用来 bean的注册器
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
// 注册 bean
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MyBean.class);
RootBeanDefinition otherBean = new RootBeanDefinition(OtherBean.class);
beanDefinitionRegistry.registerBeanDefinition("myBean",rootBeanDefinition);
beanDefinitionRegistry.registerBeanDefinition("otherBean",otherBean);
}
}
//配置类
@Configuration
@Import(value = MyImportBeanDefinitionRegistrar.class)
public class AppplicationConfig {
}