Spring Boot 学习(一)感慨

本文介绍了 Spring Boot 的基本概念及如何使用 Maven 和 Gradle 进行项目搭建,包括配置文件、依赖管理等内容,并通过一个简单的示例展示了如何创建 RESTful API。

老是在传统行业混,面试了几家公司。我这种独立开发者,(之前写全栈,和面试官争吵了一会儿啊,hah观点不同,会java,数据库,前端,android的简直不被看好。主要是不太精通)面试劣势。

都是问一些集群,并发,大数据,分布式的东西,看来普通的java程序员混不下去了。hah。所以也学习一下吧~

在综合考虑对spring也是比较熟悉的情况下,想学习spring cloud,现在掀起对spring boot进行一些了解。

spring boot就是一款spring“集团”整的一份轻量级,无配置,默认约定的快速开发框架。

我现在学习的1.5.1.release

建议使用maven进行项目管理,现在maven在java中用的应该是多的吧。maven3.2+

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

以上需要继承spring boot parent

如果不想继承,使用依赖管理

<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

 

gradle也可,2.9+,build.gradle file:

plugins {
  id 'org.springframework.boot' version '1.5.1.RELEASE'
  id 'java'
}
jar {
  baseName = 'myproject'
  version = '0.0.1-SNAPSHOT'
}
repositories {
  jcenter()
}
dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
  testCompile("org.springframework.boot:spring-boot-starter-test")
}

 

我们新建一个类

package com.du;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@ComponentScan
public class Main {
	
	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Main.class, args);
	}
	
}

run as java application,

然后打开localhost:8080即可看到Hello World!说明我们的基础配置已经成功

源码可查看http://git.oschina.net/dim/SpringBoot

小技巧:在启动后,你看到的是如下的内容

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: v1.5.1.RELEASE
2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication

你可以在你的classpath中假如一个文件banner.txt

随便填写,比如hellow dim!

然后就可以看到

Hellow DIm!
2017-03-01 13:56:10.696  INFO 10332 --- [           main] com.du.Main                              : Starting Main on dim-PC with PID 10332 (C:\Users\Administrator\git\SpringBoot\target\classes started by Administrator in C:\Users\Administrator\git\SpringBoot)

定制自己的启动广告,hahah

转载于:https://my.oschina.net/u/435471/blog/846717

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值