PYG电商项目开发 -- day10 springboot入门、短信发送、用户注册

一、springboot入门案例


1、创建springboot的jar包工程






2、导入依赖的jar包坐标


<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.springboot.demo</groupId>
	<artifactId>springboot_demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 依赖springboot提供的父工程 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
	</parent>

	<dependencies>
		<!-- 加入springboot必须的web包 加入此依赖后,会自动依赖一大批需要依赖的包 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- 加入热部署配置,即:修改完java代码之后不需要重新启动项目,即可进行访问 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

	<!-- 修改JDK版本,父工程默认为1.6 -->
	<properties>
		<java.version>1.8</java.version>
	</properties>


</project>


3、application.properties


# application.properties是springboot的属性参数文件,
# 使用该文件来修改springboot属性中的值,文件名必须交application.properties

# 修改tomcat端口号
server.port=8081

# 自定义属性名=属性值
html_url=http://www.baidu.com


4、编写测试demo


package com.springboot.demo.controller;

import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * springboot入门案例
 * @author wingz
 *
 */
@RestController
public class SpringBootDemo {

	// 获取application.propeties中自定义的属性的值
	private Environment env;

	@RequestMapping("/hello")
	public String showMessage() {
		//使用env获取application.properties中自定义的属性的值,参数是key
		String url = env.getProperty("html_url");
		return url;
	}
}


4、编写引导类


package com.springboot.demo;

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

/**
 * springboot启动的引导类
 * 
 * @author wingz
 *
 */
@SpringBootApplication // 该注解具有扫描跟当前类在同一个包或者子包下的注解的能力
public class Application {

	public static void main(String[] args) {
		/**
		 * 运行该方法,即可启动springboot,springboot内置有一个tomcat插件,端口号默认为8080,访问不需要带项目名
		 * 可以通过application.properties进行修改启动的端口号
		 */
		SpringApplication.run(Application.class, args);
	}
}


5、入门案例总览




二、

猜你喜欢

转载自blog.csdn.net/wingzhezhe/article/details/80724294
今日推荐