JavaEE微框架Spring Boot深入解读
作者:u014427391 时间:2021-07-31 14:24:55
前言
spring框架作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多,所以知识量很广。
Spring Boot:一款Spring框架的子框架,也可以叫微框架,是2014年推出的一款使Spring框架开发变得容易的框架。学过Spring框架的都知识,Spring框架难以避免地需要配置不少XMl,而使用Spring Boot框架的话,就可以使用注解开发,极大地简化基于Spring框架的开发。
Spring Boot充分利用了JavaConfig的配置模式以及“约定优于配置”的理念,能够极大的简化基于Spring MVC的Web应用和REST服务开发。
然后本博客介绍基于IDEA编辑器的Spring Boot项目创建和部署。
Spring Boot项目创建
1.创建Maven项目
2.在pom.xml加入Spring Boot的jar
如果只是测试一个字符串输出的话,只要加入spring-boot-starter(核心模块)和spring-boot-starter-web(因为这个一个Web项目),可以参考我的配置,这里使用了Spring Boot热部署,需要去github上搜索jar:springloaded-1.2.4.RELEASE.jar,然后下载放在项目的lib文件夹里
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-boot-admin.version>1.4.5</spring-boot-admin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
<dependencies>
<!--springloaded hot deploy -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/springloaded-1.2.5.RELEASE.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
刷新,下载jar到maven项目里
3.编写程序,项目结构如图
写个启动类Application.Java:
启动类设置端口为8087,因为默认端口是8080,而有很多应用都是8080端口,避免重复,最好自己改端口
其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用
@Configuration,@EnableAutoConfiguration和@ComponentScan
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class Application implements EmbeddedServletContainerCustomizer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8087);
}
}
写个Controller类:
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2017/4/24.
*/
@RestController
@RequestMapping("/")
public class DemoController {
@RequestMapping("/demo")
private String demo() {
return "this is spring boot demo!!!";
}
}
导入不想自己写demo,可以通过http://start.spring.io/ ,在平台自动生成一个demo代码,然后打开项目就好
Spring Boot部署
添加个Spring Boot配置服务器
访问:
以上所述是小编给大家介绍的JavaEE微框架Spring Boot深入解读网站的支持!
来源:http://blog.csdn.net/u014427391/article/details/70655332
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中TypeReference用法详情说明
详解java Collections.sort的两种用法
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
winform壁纸工具为图片添加当前月的日历信息
![](https://img.aspxhome.com/file/2023/8/78178_0s.jpg)
Spring Bean自动装配入门到精通
Spring Aop 如何获取参数名参数值
java面试常问的Runnable和Callable的区别
java中request常用方法小结
![](https://img.aspxhome.com/file/2023/5/82995_0s.png)
java实现ftp文件上传下载功能
实例讲解Java 自旋锁
![](https://img.aspxhome.com/file/2023/4/70914_0s.png)
Java实现冒泡排序算法
浅析Spring工厂的反射和配置文件
![](https://img.aspxhome.com/file/2023/4/77964_0s.jpg)
Spring Security保护用户密码常用方法详解
![](https://img.aspxhome.com/file/2023/5/67485_0s.jpg)
idea创建SpringBoot项目时Type选maven project和maven pom有何区别
![](https://img.aspxhome.com/file/2023/9/57499_0s.png)
详解基于java的Socket聊天程序——初始设计(附demo)
![](https://img.aspxhome.com/file/2023/0/75000_0s.png)
mybatis水平分表实现动态表名的项目实例
![](https://img.aspxhome.com/file/2023/2/59772_0s.png)
Java利用Selenium操作浏览器的示例详解
![](https://img.aspxhome.com/file/2023/2/66482_0s.jpg)
java实现邮件发送
![](https://img.aspxhome.com/file/2023/2/68762_0s.jpg)