源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaEE微框架Spring Boot深入解读

  • 时间:2020-12-04 02:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaEE微框架Spring Boot深入解读
[b]前言[/b] spring框架作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多,所以知识量很广。 Spring Boot:一款Spring框架的子框架,也可以叫微框架,是2014年推出的一款使Spring框架开发变得容易的框架。学过Spring框架的都知识,Spring框架难以避免地需要配置不少XMl,而使用Spring Boot框架的话,就可以使用注解开发,极大地简化基于Spring框架的开发。 Spring Boot充分利用了JavaConfig的配置模式以及“约定优于配置”的理念,能够极大的简化基于Spring MVC的Web应用和REST服务开发。 然后本博客介绍基于IDEA编辑器的Spring Boot项目创建和部署。 [b]Spring Boot项目创建[/b] [b]1.创建Maven项目 [/b] [img]http://files.jb51.net/file_images/article/201704/2017042815174263.png[/img] 在 [b]2.在pom.xml加入Spring Boot的jar [/b] 如果只是测试一个字符串输出的话,只要加入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项目里   [img]http://files.jb51.net/file_images/article/201704/2017042815174264.png[/img]   [b]3.编写程序,项目结构如图 [/b] [img]http://files.jb51.net/file_images/article/201704/2017042815174265.png[/img] 写个启动类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,可以通过[url=http://start.spring.io/]http://start.spring.io/[/url] ,在平台自动生成一个demo代码,然后打开项目就好 [b]Spring Boot部署[/b] 添加个Spring Boot配置服务器 [img]http://files.jb51.net/file_images/article/201704/2017042815174266.png[/img] [img]http://files.jb51.net/file_images/article/201704/2017042815174367.png[/img] 访问: [img]http://files.jb51.net/file_images/article/201704/2017042815174368.png[/img] 以上所述是小编给大家介绍的JavaEE微框架Spring Boot深入解读,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部