zoukankan      html  css  js  c++  java
  • spring boot + gradle[草稿]

    入门文档:https://github.com/qibaoguang/Spring-Boot-Reference-Guide

    安装gradle

    官方下载 https://gradle.org/gradle-download/,建议用迅雷。

    环境变量配置:http://jingyan.baidu.com/article/4d58d541167bc69dd4e9c009.html

    首先说一下使用spring-boot开始项目的一些注意事项(针对新手):

    • 为了方便,请抛弃配置XML,真的很冗杂
    • 全面支持annotation注解和java config
    • spring-boot提供的一系列starter开始你的项目
    • spring-boot只是帮你更好的开始一个项目,而不是一个应用框架
    • 请使用IDEA开发
    开始一个web项目

    插件配置:

    idea的模型 https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModel.html

    spring boot插件(配置了该插件后才有 gradle bootRun任务) http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html

    新建文件夹bootmkdir boot,在boot根目录执行gradle init --type java-library,修改build.gradle添加依赖compile 'org.springframework.boot:spring-boot-starter-web',新建Application.java

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

    写一个简单的controller

    @Controller
    public class PublicController {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
    }

    boot几乎所有配置都在application.properties里,新建src/main/resources/application.properties,修改端口号server.port=8090,命令行启动gradle bootRun查看http://localhost:8090Hello World!
    添加其他功能只需要添加对应的starter然后配置即可,比如通常会用到的一些starter

    'org.springframework.boot:spring-boot-starter-web' // web项目
    'org.springframework.boot:spring-boot-starter-data-jpa'  // JPA对应DAO
    'org.springframework.boot:spring-boot-starter-security' // 权限管理
    'org.springframework.boot:spring-boot-starter-thymeleaf' // view层,替代JSP
    'org.springframework.boot:spring-boot-devtools' // 开发工具,热加载

    最后说一下目录结构,一般而言是这样:

    |-- build.gradle
    |-- src
    |----|-- main
    |---------|-- java
    |--------------|-- com.project
    |---------------------|-- controller
    |---------------------|-- service
    |---------------------|-- repository
    |---------------------|-- entity
    |---------|-- resources
    |--------------|-- application.properties
    |--------------|-- application-dev.properties
    |--------------|-- application-pro.properties

    我推荐这样:

    |-- build.gradle
    |-- src
    |----|-- main
    |---------|-- java
    |--------------|-- com.project
    |---------------------|-- user
    |--------------------------|-- controller
    |--------------------------|-- service
    |--------------------------|-- repository
    |--------------------------|-- entity
    |---------|-- resources
    |--------------|-- application.properties
    |--------------|-- application-dev.properties
    |--------------|-- application-pro.properties

    按组件区分,易查看代码,当项目成长到一定程度更加容易拆分。

    热加载

    代码热替换 方法一

    mvn spring-boot:run

    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
              <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.6.RELEASE</version>
              </dependency>
            </dependencies>
            <!--
            <configuration>
              <fork>true</fork>
            </configuration>-->
    
          </plugin>

    代码热加载方法二:

    main方法启动

    -javaagent:D:/software/springloaded-1.2.6.RELEASE.jar -noverify

    模板热替换:

    spring.thymeleaf.cache=false

    必须按Contrl+F9,太傻了

    参考:http://www.jianshu.com/p/ec545ce19bdd
    大量的例子:https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-basewebapp
     很多干货:http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/
     默认配置的:http://www.tuicool.com/articles/veUjQba
  • 相关阅读:
    搭建非域AlwaysOn win2016+SQL2016
    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
    四、基于Windows 2012配置SQL Server 2014 AlwaysOn
    三、安装SQLserver 2014(For AlwaysOn)
    二、 Windows 2012配置故障转移(For SQLServer 2014 AlwaysOn)
    Mybatis-SQL语句构建器类及日志
    Mybatis-JavaAPI
  • 原文地址:https://www.cnblogs.com/hero4china/p/spring_boot_gradle.html
Copyright © 2011-2022 走看看