欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【spring-boot】快速构建spring-boot微框架的方法

程序员文章站 2023-12-16 22:09:46
spring-boot是一个快速构建环境的一套框架,其设计理念是尽可能的减少xml的配置,用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置...

spring-boot是一个快速构建环境的一套框架,其设计理念是尽可能的减少xml的配置,用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

废话不多说,关于spring-boot是什么具体请百度。

官网:

1. spring-boot是一个mavan项目,所以其使用的jar包全部是通过maven管理,当然,使用maven也是非常方便的。

首先上我的项目目录结构:

【spring-boot】快速构建spring-boot微框架的方法      

spring-boot打出来的包是一个可执行jar包的状态,使用的是内置的tomcat服务器,所以不需要将项目转成ejb项目。

2.设置pom.xml文件

使用过maven的朋友都知道,maven通过pom文件的依赖来进行管理jar包,所以核心也是这个pom.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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.lclc.boot</groupid>
  <artifactid>boot-cache</artifactid>
  <version>0.0.1-snapshot</version>
  <!-- inherit defaults from spring boot -->
  <parent>
    <!--spring boot基础父类,其中包含了很多必要的jar包,如果不使用父类,则需要自己去依赖这些jars -->
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.1.3.release</version>
  </parent>
  <dependencies>
    <!-- web程序的启动项依赖,通过此依赖可引入内嵌的tomcat等web必须的jars -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <!-- spring-data-jpa程序的启动项依赖,底层为hibernate实现,若不使用此框架则可以依赖其他的orm框架 -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-jpa</artifactid>
    </dependency>
    <!-- thymeleaf程序的启动项依赖,spring-boot对thymeleaf模板引擎支持最好,建议模板引擎使用此框架 -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-thymeleaf</artifactid>
    </dependency>
    <!-- mysql依赖,使用spring-data-jpa需要指定一个数据库方言,用于连接数据库,即mysql驱动 -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
    </dependency>
  </dependencies>

  <dependencymanagement>
    <dependencies>
    </dependencies>
  </dependencymanagement>

  <build>
    <plugins>
      <!-- 通过maven构建的插件 -->
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
  <!-- 仓库,使用spring-boot release版本需要这些 -->
  <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>

3.使用maven update 下载jar包  

4.由于我们使用了thymeleaf引擎,此引擎需要一个templates文件夹来存放静态页面,以便进行跳转前台。

所以在resources下添加此文件夹并加入一个默认的页面index.html(注:此文件夹下必须有一个html页面,否则thymeleaf启动项会抛异常)

5.编写application.properties

这个配置文件是对spring-boot的一些配置,spring-boot通过此文件对集成在其中的一些框架进行配置。由我的项目结构可以看出,我有两个application.properties文件:

application.properties:主配置文件,spring-boot直接读取这个文件。注:配置文件必须放在resources下,即放在项目根目录下。

application-dev.properties:开发环境配置文件,这个是我的开发环境的配置文件,为了简化一些开发,所以需要一些与部署环境不同的配置,比如页面缓存之类的。此文件通过application.properties的spring.profiles.active属性进行配置读取。

上两个文件的代码:

首先是application.properties:

# profiles
## dev | prod | test
spring.profiles.active=dev

# embedded server configuration (serverproperties)
server.port=8080
server.session-timeout=30
server.context-path=
server.tomcat.max-threads=0
server.tomcat.uri-encoding=utf-8

# thymeleaf (thymeleafautoconfiguration)
spring.thymeleaf.encoding=utf-8

# datasource
spring.datasource.initialize=false
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.test-while-idle=true
spring.datasource.max-wait-millis=30000
spring.datasource.validation-query=select 1
spring.datasource.time-between-eviction-runs-millis=20000
spring.datasource.min-evictable-idle-time-millis=28700

然后是application-dev.properties:

#page cache
spring.thymeleaf.cache=false

# datasource 
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost/test_development?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull&transformedbitisboolean=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverclassname=com.mysql.jdbc.driver
spring.datasource.max-active=5
spring.datasource.max-idle=2
spring.datasource.min-idle=1
spring.datasource.initial-size=1
spring.datasource.initialize=false

# jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true

6.于是配置便完成了,现在看怎么使用spring-boot进行启动一个web程序

spring-boot打的包是一个可执行的jar包,当然也可以打成可执行的war包,启动服务器就完全不需要像以前一样弄一个tomcat进行启动了,完全是java application进行启动

通过一个启动文件的main方法

@configuration
@enableautoconfiguration
@componentscan
public class application {
  public static void main(string[] args){
    springapplication springapplication = new springapplication (application.class);
    springapplication.run (args);
  }
}

先来解释下这个文件中的代码。

@configuration:标注此文件为一个配置项

@enableautoconfiguration:使用自动配置

@componentscan:可扫描的

springapplication:启动管理器。

注意,由于是使用注解的方式,所以需要配置扫描路径,spring-boot使用的是启动管理器所在的包为根扫描路径。会扫描其所在的包和子包,所以需要将application.java放在跟路径下,即com.test这个包里。

7.然后执行一下就好了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: