springboot学习

来源:互联网 发布:淘宝盗用视频怎么处理 编辑:程序博客网 时间:2024/06/06 09:08

参考自:Java EE开发的颠覆者 Spring Boot实战

不断更新中……

一、springboot搭建

springboot集成maven的pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.tianmaying</groupId>
  <artifactId>spring-web-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>spring-web-demo</name>
  <description>Demo project for Spring WebMvc</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

package com.ch5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
新建测试类,如下
@RestController
@SpringBootApplication
public class Ch522Application {
    @RequestMapping("/")
    public String index(){
        return "Hello Spring Boot";
    }
    public static void main(String[] args) {
        SpringApplication.run(Ch522Application.class, args);
    }
}
其中RestController是个符合注解(RequestMapping+Controller)
SpringBootApplication可以读取配置文件,启动springboot服务

springboot启动时会有一个默认启动图案,通过在src/main/resources下新建banner.txt,可以修改springboot的启动图案

通过http://patorjk.com/software/taag可以定制自己需要的启动图案。

如果不想显示启动图案,可以在main方法中设置

SpringApplication app = new SpringApplication(Ch522Application.class);
        app.setShowBanner(true);
        app.run(args);

或者

new SpringApplicationBuilder(Ch522Application.class).showBanner(false).run(args);

二、springboot配置文件

可在src/main/resources下面添加配置文件,application.properties或者application.yml(yaml目前STS3.7以上开始支持,IDEA则只对springboot的properties配置提供自动提示功能,且@PropertySource注解不支持加载yaml文件),所以此处建议使用application.properties。

application.prpoerties配置示例:

server-port=9090

server.context-path=/helloa

application.yml配置示例:

server:

  port:9090

  contextPath:/hello

注意,此处不是tab,而是两个空格。

另外,springboot可通过@importResource来加载xml配置

@ImportResource({"classpath:test.xml","classpath:test2.xml"})



0 0
原创粉丝点击