栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaEE微框架Spring Boot深入解读

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaEE微框架Spring Boot深入解读

前言

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文件夹里


 4.0.0
 com.example
 demo
 war
 1.0-SNAPSHOT
 demo Maven Webapp
 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
  
 
 
  UTF-8
  1.8
  1.4.5
 
 
  
   junit
   junit
   3.8.1
   test
  
  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.springframework.boot
   spring-boot-starter-actuator
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
  
   de.codecentric
   spring-boot-admin-starter-client
   ${spring-boot-admin.version}
  
  
   org.springframework.boot
   spring-boot-starter
  
 
 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
   
    org.springframework.boot
    spring-boot-maven-plugin 
    
     
     
      org.springframework
      springloaded
      ${basedir}/src/main/webapp/WEB-INF/lib/springloaded-1.2.5.RELEASE.jar
     
    
    
     
      
repackage
      
      
exec
      
     
    
   
  
 

刷新,下载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;

@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深入解读,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146442.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号