springboot学习

本文介绍如何使用Spring Boot和Maven快速搭建一个简单的Web应用,并展示了基本的配置方式及启动过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考自: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"})



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值