Spring Boot 框架学习(一)

绪论

从今天起,关心语言和框架,我有一个项目,辛苦半天,都是BUG。(●ˇ∀ˇ●)

一些人知道,我最近在写个有关博客的项目,起因是买了阿里云服务器,想学点新知识练练手什么的。最近听说springboot比较火(其实发布出来已经三四年了),偶然之间在慕课网上看到springboot 企业级博客系统实战 ,这么一场实战居然要三百多,算了,自己找资料学习吧。

Maven Project 创建

在eclipse上创建maven项目还是第一次,以前都是其他人弄好的,或者直接在svn上导出来,所以记录一下。

两个都需要勾选。
在这里插入图片描述

写上包名,项目名,选择war包。
这里写图片描述

生成的项目目录。
在这里插入图片描述

右键该项目,点击 Properties,打开项目属性窗口,点击 Project Facets,根据自己电脑的jdk配置进行选择。
这里写图片描述

去掉Dynamic Web Project 勾选,点击apply。再次勾选Dynamic Web Project ,出现连接(是用于eclipse 4.5/4.6)。
这里写图片描述

替换地址,勾选web.xml。
在这里插入图片描述

有这些内容就好了。
在这里插入图片描述

最后在Properties中Deployment Assembly,选中与 test 相关的路径,点击右边的 Remove 移除掉,去掉这些测试的东西。
这里写图片描述

基本配置完成,开始spring boot学习之旅。

Spring Boot

在pom.xml中加入以下内容。会下载一些spring boot相关的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.yyy</groupId>
  <artifactId>MavenDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.4.0.BUILD-SNAPSHOT</version>
  </parent>

	<!-- 为Web应用程序添加依赖项 -->
  <dependencies>
  	<dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  </dependencies>

	<!-- 形成一个可执行的jar包 -->
  <build>
  	<plugins>
  		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
  </build>

	<!-- 添加Spring库 -->
	<!-- (如果用其它版本的spring,这部分可以去掉) -->
  <repositories>
	<repository>
		<id>spring-snapshots</id>
		<url>http://repo.spring.io/snapshot</url>
		<snapshots><enabled>true</enabled></snapshots>
	</repository>
	<repository>
		<id>spring-milestones</id>
		<url>http://repo.spring.io/milestone</url>
	</repository>
  </repositories>
  <pluginRepositories>
  	<pluginRepository>
  		<id>spring-snapshots</id>
  		<url>http://repo.spring.io/snapshot</url>
  	</pluginRepository>
  	<pluginRepository>
  		<id>spring-milestones</id>
  		<url>http://repo.spring.io/milestone</url>
  	</pluginRepository>
  </pluginRepositories>
</project>

在src/main/java中添加以下文件。
Application文件。

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

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

Example文件。

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

@RestController
@EnableAutoConfiguration
public class Example {

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

运行一下,选择Run As的java application,选择我们路径下的Application,嗯~ o( ̄▽ ̄)o,成功出错。

这里写图片描述

问题原因,因为maven在加载spring boot的时候,里面所有使用的jar包基本都加载了,而自己在加载上tomcat就多余了,所以在properties中选择Java build path 中去掉本地的tomcat,就好了。

这里写图片描述

在浏览器输入http://localhost:8080/ 可直接看到网页显示我们写的内容。真方便,连项目名都不用写,端口号直接默认,还需要我做什么(⊙﹏⊙)
在这里插入图片描述

这里写图片描述

这个东西虽然目前运行出来,没什么问题。但是引起了我的好奇心,比如要写jsp页面的话怎么跳转,数据怎么和jsp页面交互,怎么和数据库连接,放到服务器上怎么运行,若服务器上有tomcat会不会冲突,怎么把端口号改掉等等。

(若有什么错误,请留言指正,3Q)

Spring Boot 基础教程(基于1.3.x-1.5.x) 快速入门 chapter1:基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程 工程配置 chapter2-1-1:配置文件详解:自定义属性、随机数、多环境配置等 chapter2-1-2:2.0 新特性(一):配置绑定全解析 chapter2-2-1:2.0 新特性(二):新增事件ApplicationStartedEvent Web开发 chapter3-1-1:构建一个较为复杂的RESTful API以及单元测试 chapter3-1-2:使用Thymeleaf模板引擎渲染web视图 chapter3-1-3:使用Freemarker模板引擎渲染web视图 chapter3-1-4:使用Velocity模板引擎渲染web视图 chapter3-1-5:使用Swagger2构建RESTful API chapter3-1-6:统一异常处理 chapter3-1-7:使用Java 8中LocalDate等时间日期类的问题解决 chapter3-1-8:扩展XML请求和响应的支持 数据访问 chapter3-2-1:使用JdbcTemplate chapter3-2-2:使用Spring-data-jpa简化数据访问层(推荐) chapter3-2-3:多数据源配置(一):JdbcTemplate chapter3-2-4:多数据源配置(二):Spring-data-jpa chapter3-2-5:使用NoSQL数据库(一):Redis chapter3-2-6:使用NoSQL数据库(二):MongoDB chapter3-2-7:整合MyBatis chapter3-2-8:MyBatis注解配置详解 chapter3-2-9:使用Flyway来管理数据库版本 chapter3-2-10:使用LDAP来统一管理用户信息 chapter3-2-11:Spring Boot中增强对MongoDB的配置(连接池等) 事务管理 chapter3-3-1:使用事务管理 chapter3-3-2:[分布式事务(未完成)] 其他内容 chapter4-1-1:使用@Scheduled创建定时任务 chapter4-1-2:使用@Async实现异步调用 chapter4-1-3:使用@Async实现异步调用:自定义线程池 chapter4-1-4:使用@Async实现异步调用:资源优雅关闭 chapter4-1-5:使用@Async实现异步调用:使用Future以及定义超时 日志管理 chapter4-2-1:默认日志的配置 chapter4-2-2:使用log4j记录日志 chapter4-2-3:对log4j进行多环境不同日志级别的控制 chapter4-2-4:使用AOP统一处理Web请求日志 chapter4-2-5:使用log4j记录日志到MongoDB chapter4-2-6:Spring Boot 1.5.x新特性:动态修改日志级别] 安全管理 chapter4-3-1:使用Spring Security chapter4-3-2:[使用Spring Session(未完成)] 缓存支持 chapter4-4-1:注解配置与EhCache使用 chapter4-4-2:使用Redis做集中式缓存 邮件发送 chapter4-5-1:实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件 消息服务 chapter5-1-1:[JMS(未完成)] chapter5-2-1:Spring Boot中使用RabbitMQ 其他功能 chapter6-1-1:使用Spring StateMachine框架实现状态机 Spring Boot Actuator监控端点小结 在传统Spring应用中使用spring-boot-actuator模块提供监控端点 Spring Boot应用的后台运行配置 Spring Boot自定义Banner Dubbo进行服务治理 chapter9-2-1:Spring Boot中使用Dubbo进行服务治理 chapter9-2-2:Spring Boot与Dubbo中管理服务依赖
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值