初探SpringBoot

自定义标题

          • 修改maven地址
          • 第一个SpringBoot程序
          • 改端口
          • 自动配置
          • 启动器
          • 主程序

修改maven地址

修改maven地址 在setting maven中设置,选择overwrite ,去到对应的文件夹下面创建settings文件

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">  
<mirrors>
     <mirror>  
        <id>alimavenid>  
        <name>aliyun mavenname>  
        <url>http://maven.aliyun.com/nexus/content/groups/public/url>  
        <mirrorOf>centralmirrorOf>          
    mirror>  
  mirrors>
settings>
第一个SpringBoot程序

创建第一个Springboot

在pom.xml中添加

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starterartifactId>
dependency>

在Application同級目录下创建Controller文件夹创建一个测试类Hello,暂时不管注解的含义

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

重新启动项目,在localhost:8080/hello/hello看到启动成功输出hello

改端口

在resources下的application.properties

server.port=8081

自动配置

pom.xml

  • 标签内点进去有大量的jar包
  • 有大量的启动器
  • 我们写或者引入springBoot依赖的时候,不需要指定版本,因为有版本仓库
启动器
<dependency>
  <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
  • 实际上就是SpringBoot的启动场景;例如

spring-boot-starter-web,他就会帮我们导入web环境的所有依赖

  • SpringBoot将所有的功能场景,变成一个个的启动器

    点开依赖查看jar包,有如下几个我们认识的

5.15.11//消息队列
11.5.0.0//数据库
2.10.6
3.8.1//缓存

需要声明功能就start启动哪个模块,

进入spring官网https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/using-spring-boot.html#using-boot-starter查看功能对应的启动器

主程序
//标注这个类是一个SpringBoot应用
@SpringBootApplication
public class Class1Application {

    public static void main(String[] args) {
        //将Spring应用启动
        SpringApplication.run(Class1Application.class, args);

    }

}
  • 注解

  • @SpringBootConfiguration
    //SpringBoot的配置
    	@Configuration:spring配置类
    		@Component:这是一个spring组件
    @EnableAutoConfiguration :自动配置
    	@AutoConfigurationPackage:自动配置包
    		@Import({Registrar.class}):导入选择器
    

你可能感兴趣的:(JavaEE)