Spring Boot 学习(一)感慨

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

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

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

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

我如今学习的1.5.1.releasegit

建议使用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 parentweb

若是不想继承,使用依赖管理面试

<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:spring

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

相关文章
相关标签/搜索