一文学习Springboot(基于2.0)

给大家参考的同时,也作为自己的学习笔记。

Springboot,是Pivotal团队开发的框架。它解决了SpringMVC的一些痛点,如配置文件繁琐等,加快了开发效率。总体上,遵循约定优于配置,很多东西帮我们配好了,开箱即用。

先看个大概:

一.Springboot核心注解


/**
    * @SpringBootApplication注解的类一定要放在自定义包下且属于自定义包的
    * 
    * @SpringBootApplication is a convenience annotation that adds all of the following:
    * @SpringBootApplication是一个方便的注解,增加了所有的以下内容:
    * 
    * @Configuration tags the class as a source of bean definitions for the application context.
    * @Configuration 标记一个类来作为bean定义的应用程序上下文的资源
    * 
    * @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
    * @EnableAutoConfiguration告诉Spring Boot开始加载基于类路径下的配置信息、beans、各种属性配置。
    * 
    *  Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds
    *  it automatically when it sees spring-webmvc on the classpath. This flags the application as a web
    *  application and activates key behaviors such as setting up a DispatcherServlet
    *  通常你会添加@EnableWebMvc为Spring MVC的应用程序,但是当Spring Boot在classpath下检索到spring-webmvc时,
    *  spring boot会自动添加。这标志该应用程序作为Web应用程序,并激活关键行为,如设立的DispatcherServlet
    * 
    * @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
    * @ComponentScan 告诉Spring寻找其他组件,配置,以及业务层类,最前面才能加载到所有的类。
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//下面三个核心注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    ...
}
复制代码

1、@Configuration

org.springframework.context.annotation.Configuration

这是 Spring 3.0 添加的一个注解,用来代替 applicationContext.xml 配置文件,所有这个配置文件里面能做到的事情都可以通过这个注解所在类来进行注册。

下面几个相关注解也是非常重要的!

@Bean

用来代替 XML 配置文件里面的 <bean ...> 配置。

@ImportResource

如果有些通过类的注册方式配置不了的,可以通过这个注解引入额外的 XML 配置文件,有些老的配置文件无法通过 @Configuration 方式配置的非常管用。

@Import

用来引入额外的一个或者多个 @Configuration 修饰的配置文件类。

@SpringBootConfiguration

这个注解就是 @Configuration 注解的变体,只是用来修饰是 Spring Boot 配置而已,或者可利于 Spring Boot 后续的扩展,源码如下。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}
复制代码

2、@ComponentScan

org.springframework.context.annotation.ComponentScan

这是 Spring 3.1 添加的一个注解,用来代替配置文件中的 component-scan 配置,开启组件扫描,即自动扫描包路径下的 @Component 注解进行注册 bean 实例到 context 中。

3、@EnableAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration

看全路径就知道,这是自 Spring Boot 诞生时添加的注解,用来提供自动配置,上面的两个都是 spring-context 包下的,不属于 Spring Boot,所以 Spring 3.0 之后的去 XML 配置方式已经为 Spring Boot 埋下了伏笔!

二.Springboot的三种启动方式

  • 运行带有main方法类

  • 通过命令行 java -jar 的方式

  • 通过spring-boot-plugin的方式

2.1 运行带有main方法类

我用的是Eclipse,选中该类,点击运行图标或者单击右键,选择运行Java Application.

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		// devtools:是spring boot的一个热部署工具
		//设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性.
		//System.setProperty("spring.devtools.restart.enabled","false");  
		
		// 启动Sprign Boot
		ApplicationContext ctx = SpringApplication.run(App.class, args);
		System.out.println("Let's inspect the beans provided by Spring Boot:");
		String[] beanNames = ctx.getBeanDefinitionNames();
		Arrays.sort(beanNames);
		for (String beanName : beanNames) {
			System.out.println(beanName);
		}
	}
}
复制代码

2.2 通过命令行 java -jar 的方式

java -jar jar_path --param

jar_path: 指代将项目打包为jar打包之后的存储路径

--param: 为需要在命令行指定的参数。例如:

java -jar emample.jar --server.port=8081

该命令通过在启动行指定了项目启动后绑定的端口号,因为该命令行参数,将会覆盖application.properties中的端口配置

2.3 通过spring-boot-plugin的方式

在pom.xml中添加添加如下插件:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 在项目中指定了父模块 spring-boot-starter-parent 则不需要添加 -->
                <!--<version>${spring.boot.version}</version>-->
                <configuration>
                    <!-- 主类的入口 -->
                    <mainClass>cn.**.App</mainClass>
                </configuration>
                <!-- jar/war等无须再次打包,可不加 -->
            <!--<executions>
	            <execution>
		            <goals>
		                <goal>repackage</goal>
	                </goals>
		        </execution>
                </executions> -->
            </plugin>
        </plugins>
    </build>
复制代码

项目根目录下运行脚本:

mvn sprint-boot:run

实践学习项目(个人觉得还不错)

传送门:github.com/timebusker/…

源码解读

第一步:获取并启动监听器

第二步:构造容器环境

第三步:创建容器

第四步:实例化SpringBootExceptionReporter.class,用来支持报告关于启动的错误

第五步:准备容器

第六步:刷新容器

第七步:刷新容器后的扩展接口

(内容较多,慢慢消化)

传送门: blog.csdn.net/woshilijiuy…

参考:

启动与部署 更多可看此文

zoeminghong.github.io/2017/10/16/…

blog.csdn.net/u011425751/…

zhuanlan.zhihu.com/p/46887997

github.com/timebusker/…

猜你喜欢

转载自juejin.im/post/5e606e8251882549087dabc3