Spring Boot学习之初步搭建

最近一直在看spring boot的相关内容,在网上阅读了很多相关的文章,非常感谢他们,在此把自己的一些操作记录下来,希望可以帮到后面学习的人。

整个测试项目完成后的项目结构如图


在eclipse中新建一个maven项目,创建完成后,修改pom.xml文件,pom.xml文件

<pre name="code" class="html"><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.waylon</groupId>
	<artifactId>spring-boot-simple</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-simple</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<properties>
		<!-- 设置spring-boot的版本,java编码的格式,以及jdk的版本,防止每次update project时jdk的版本总是变成1.5 -->
		<spring.boot.version>1.3.3.RELEASE</spring.boot.version>
		<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>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
 

保存编译等所有架包下载完成之后,项目仍然有红叉,更新项目“maven” --> “update project”

完成之后创建一个测试文件Application.java,一般放在根包下

/****************************************
 * Copyright (c) 2016 NiuWa.
 * All rights reserved.
 * Created on 2016年3月3日
 * 
 * Contributors:
 * 	BWaylon - initial implementation
 ****************************************/
package com.waylon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @title Application.java
 * 项目启动main方法,一般单独放在根包下面
 * @author BWaylon
 * @version 1.0
 * @created 2016年3月3日
 */
@SpringBootApplication //same as @Configuration @EnableAutoConfiguration @ComponentScan 
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	

}
在Controller包下创建一个测试的类SampleController.java
/****************************************
 * Copyright (c) 2016 NiuWa.
 * All rights reserved.
 * Created on 2016年3月3日
 * 
 * Contributors:
 * 	BWaylon - initial implementation
 ****************************************/
package com.waylon.controller;

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

/**
 * 
 * @title SampleController.java
 *
 * @author BWaylon
 * @version 1.0
 * @created 2016年3月3日
 */
@RestController
@EnableAutoConfiguration
public class SampleController {

	@RequestMapping("/index")
	public String home() {
		return "hello world!";
	}
	
}

完成后在Application.java中直接运行main方法,启动成功后控制台输出


打开浏览器,输入地址即可访问




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值