走进Spring Boot世界

来源:互联网 发布:自动生成迷宫算法 编辑:程序博客网 时间:2024/05/29 03:35

1.快速创建项目:

     通过网站 start.spring.io 一键生成项目,速度快。

2.定制化启动图案:

 定制化图案连接:http://patorjk.com/software/taag

 在resource目录创建banner.txt。将图案复制在txt中。

3.注解

  @RestController@Controller@ResponseBody的结合体,两个标注合并起来的作用。

  @component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>

  @ConfigurationProperties根据类型校验和管理application中的bean

  @SpringBootApplication注解,它包括三个注解:

       @Configuration:表示将该类作用springboot配置文件类。

       @EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置。

      @ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类。

4.启动项目

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

5.Controller

@RestController

public class DemoApplication2 {

@Autowired

StudentConstants studentConstants;

@RequestMapping(value="/",method=RequestMethod.GET)

public String index(){

return studentConstants.getName()+studentConstants.getAge();

}

}

6.实体

@Component

@ConfigurationProperties(prefix="dn.student")

public class StudentConstants {

private String name;

private String age;

 

省略get,set

}

 

7.Spring boot 的配置文件

  Application.properties 或者 application.yml

  Application.properties 内容:

  server.port=80

  server.context-path=/demo

  logging.level.org.springframework.web=DEBUG

  #spring.profiles.active=product

  spring.profiles.active=dev 设置开发模式(spring.profiles.active=#对应 application-#.properties)

8.application-product.properties内容:

  dn.student.name=maoxu product

  dn.student.age= 22

9.application-dev.properties内容:

  dn.student.name=maoxu dev

  dn.student.age= 22

 

10.Spring boot 核心

Spring boot 四个核心:StarterautoconfigurationactuatorCL

  Starter  可以轻松使用需要的组件

  actuatorspring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。

 

 

 

 


原创粉丝点击