首页 技术 正文
技术 2022年11月12日
0 收藏 509 点赞 3,398 浏览 3952 个字

SpringBoot HelloWorld

功能需求

​浏览器发送hello请求,服务器接收请求并处理,相应HelloWorld字符串

1.创建一个maven工程;(jar)

2.导入SpringBoot相关依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</dependency>
</dependencies>

3.编写一个主程序,启动SpringBoot应用

/** * @SpringBootApplication 标注一个主程序类,说明这是一个SpringBoot 应用
**/
@SpringBootApplication
public class HelloWorldMainApplication { public static void main(String[] args) { //启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class,args);
}}

4.编写相关的Controller、Service

@Controller
public class HelloController { @ResponseBody
@RequestMapping("/hello")
public String hello(){
return "HelloWorld!";
}
}

5.运行主程序测试

http://localhost:8080/hello

6. 简化部署

<!--    这个插件可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

将这个应用打成jar包(自带tomcat),直接使用java -jar的命令进行执行

7.探究HalloWorld

1.POM文件

1.父项目

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
它的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
真正管理Spring Boot应用里面的所有依赖版本;

Spring Boot版本仲裁中心(spring-boot-dependencies);

以后我们导入依赖默认是不需要些版本的;(没有在dependencies里面管理的依赖自然需要声明)

2.启动器starter

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web:

​spring-boot-starter:spring-boot场景启动器;

​spring-boot-starter-web帮我们导入了web模块正常运行所依赖的组件;

Spring Boot将所有功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来

2.主程序类,主入口类

/**
* @SpringBootApplication 标注一个主程序类,说明这是一个SpringBoot 应用
**/
@SpringBootApplication
public class HelloWorldMainApplication { public static void main(String[] args) { //启动Spring应用
SpringApplication.run(HelloWorldMainApplication.class,args);
}}

@SpringBootApplication:Spring Boot 应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的方法来启动SpringBoot应用;

@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.@SpringBootConfiguration:Spring Boot的配置类;

​标注在某个类上,表示这是一个SpringBoot的配置类

​包含@Configuration:配置类上来标注这个注解;

​配置类就是以往的配置文件;将配置文件替换成配置类,使用该注解才能让springboot知道这是个配置类,配置类也是容器中的一个组件:@Component

2.@EnableAutoConfiguration:开启自动配置功能

​以前我们需要配置的东西,Springboot帮我们自动配置;

@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

​@AutoConfigurationPackage:自动配置包

​@Import({Registrar.class}):Spring的底层注解@Import,给容器中导入一个组件;导入的组件由{Registrar.class}来决定

​将主配置类(@SpringBootApplication标注的类)的所在包以及所有子包里面的所有组件扫描到Spring容器中

​@Import({EnableAutoConfigurationImportSelector.class})

​给容器中导入组件

​EnableAutoConfigurationImportSelector:导入哪些组件的选择器,将所有需要导入的组件以全类名返回,这些组件就会被添加到容器中

​会给容器中导入96个自动配置类(xxxAutoConfigration):就是容器中导入这个场景需要的所有组件,并配置好这些组件;有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;

​获取Configurations主要使用了

​SpringFactoriesLoader.loadFactoryNames(EnableAutoConfigurationr.class, classLoader);

​从类路径下获取 META-INF/spring.factories 资源文件(spring-boot-autoconfigure-1.5.9.RELEASE中),获取EnableAutoConfigurationr指定的值(如上图),将这些值作为自动配置类导入到容器中,自动配置类就生效了,即自动配置。

​ 以前我们需要自己配置的东西,自动配置类都帮我们完成了。(eg.WebMvcAutoConfiguration)

​@bean添加组件

//WebMvcAutoConfiguration
@Bean //RequestMapping
@Primary
public RequestMappingHandlerMapping requestMappingHandlerMapping() { @Bean //视图解析器
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
.....

​J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-x.x.x.RELEASE.jar下的org.springframework.boot.autoconfigure中

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,096
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765