您的位置:首页 > 编程语言 > Java开发

springboot 学习笔记1(gradle创建web工程)

2017-05-30 00:00 344 查看
摘要: springboot 简化配置,对很多常用开源框架做了很好的集成,几乎无需配置即可直接使用,简化开发,提供开发效率,公司内部推广使用。

前言

springboot 倾向于约定优于配置,它不需要你配置繁琐的xml文件,极大简化你的开发,它的设计目的是让您尽可能快地创建项目和运行项目。gradle和maven一样都是构建工具。gradle优点就是无需写繁琐的xml,脚本结构比较清晰,易读,就像springboot一样,简化开发。spring源码就是使用的gradle。

1.使用IDEA 创建web项目

1. 创建一个工程。new project ,选择Spring Initializr 。选择你要使用的依赖框架。然后继续next,创建



一个项目。项目结构如下:



2.build.gradle中加入以下依赖,然后build:

buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}
version = '1.0.0'
sourceCompatibility = 1.8

tasks.withType(JavaCompile){
options.encoding="utf-8"
}
repositories {
maven{
url 'http://192.168.0.210:8081/nexus/content/repositories/thirdparty/'
}
maven{
url 'http://192.168.0.210:8081/nexus/content/repositories/central/'
}
}

ext{
dubboVersion="2.5.4"
commonLangVersion="3.2"
nettyVersion="3.2.5.Final"
ehcacheVersion="2.10.4"
}
dependencies {
//starter启动器是一组你可以添加到程序中的依赖描述符,给我们提供能满足我们需要的一站式的服务。starters包含许多你需要快速创建和运行项目的依赖。
//。例如如果使用redis,只需要加入spring-boot-starter-data-redis即可以使用redis,无需其他多余依赖配置。

compile('org.springframework.boot:spring-boot-starter-aop')
//使用缓存
compile('org.springframework.boot:spring-boot-starter-cache')
//使用redis
compile('org.springframework.boot:spring-boot-starter-data-redis')
//使用thymelaf模板引擎
compile ("org.springframework.boot:spring-boot-starter-thymeleaf")
//使用持久化api操作数据库
compile("org.springframework.boot:spring-boot-starter-data-jpa")
//使用restful微服务
compile("org.springframework.boot:spring-boot-starter-data-rest")
//使用actuator执行器,监控应用程序
compile("org.springframework.boot:spring-boot-starter-actuator")
//热部署,修改文件,无需手动重启项目
compile ("org.springframework.boot:spring-boot-devtools")
//安全管理,提供用户单点登录,授权等服务
compile("org.springframework.boot:spring-boot-starter-security")
//kafka分布式消息队列
compile('org.springframework.kafka:spring-kafka')
//web 应用程序
compile('org.springframework.boot:spring-boot-starter-web')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
// Commons Lang3
compile ('org.apache.commons:commons-lang3:3.2')
compile("com.alibaba:dubbo:${dubboVersion}")
compile('mysql:mysql-connector-java:5.1.27')
compile ("org.apache.commons:commons-lang3:${commonLangVersion}")
compile "net.sf.ehcache:ehcache:${ehcacheVersion}"
//引用WEB-INF下jar包
compile fileTree(dir:'src/main/webapp/WEB-INF/lib',include:['*.jar'])

}

sourceSets {
main {
resources {
srcDir 'src/main/resources'
}
output.resourcesDir = "build/classes/main"
}
}

task copyProjectJar(type: Sync){
from configurations.runtime,configurations.compile
into 'build/libs/lib'
}

task generateJar(type: Jar, dependsOn: [copyProjectJar,jar]) {
manifest {
attributes ('Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version,
'Created-By': 'who',
'Main-Class': 'com.wtsd.TcsApplication',
'Class-Path': configurations.compile.collect {'lib/' + it.getName()}.join(' ')
)
}
into('') {
from "$buildDir/classes/main"
include('com/')
}
into('') {
from "$projectDir/src/main/resources"
include ('ehcache.xml','dubbo.xml','*.txt')
}
}

//单元测试
task myTest(type: Test) {
exclude()
filter {
//测试指定包
includeTestsMatching "com.test.service.*"
//指定测试类
includeTestsMatching "*ServiceTest"
}

}

//发布到maven远程仓库
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
repository(url: "http://localhost:8081/repository/project/") {
authentication(userName: "admin", password: "admin123")
}

}
}

//生成doc文档
task generateJavadoc(type: Javadoc) {
source = sourceSets.main.allJava
//指定编码,否则会报错
options {
encoding "utf-8"
charSet 'utf-8'
author true
version true
}
}
//gralde 打包成jar不支持把依赖打包进去,他会默认你已经添加好了依赖。war会将依赖添加进去会的。
//不把依赖打包进去是考虑到大部分用户情况。如果需要包含依赖jar,只能gralde build。上传到资源库只能上传war
//除非使用三方工具

//将doc文档生成jar文件
task generateDocJar(type: Jar) {
//设置依赖关系,打包doc文档为jar之前,必须先生成doc文档
dependsOn generateJavadoc
def jar = file("$buildDir/jar")
jar.mkdirs()

//定义生成jar的文件名
baseName = project.name
from("$buildDir/docs/javadoc") {
into "jar"
}

}

3。build完成之后,所有依赖jar都已经添加进来,然后就是对application.properties添加配置。这里我建议用YAML脚本语言。YAML代码简洁,结构和json一样,层次感比较清晰,阅读方便。只需将application.properties后缀改成yml即可。application.yml:

spring:

#connection to database
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&autoReconnect=true
username: test
password: test
#json对日期进行格式化。对rest接口,日期会自动格式化成时间字符串
jackson:
date-format: YYYY:MM:ss HH:mm:ss
#设置时区,
time-zone:GMT+8
#热部署
devtools:
restart:
additional-paths: src/main/java
#修改java文件之后,是否自动重启,开发阶段,设置为true,
enabled: true
# 使用livereload.
livereload:
enabled: true
jpa:
show-sql: true
properties:
hibernate:
dialect:rg.hibernate.dialect.MySQL5Dialect
cache:
#使用二级缓存
use_second_level_cache: true
#查询使用缓存
use_query_cache:true
#缓存provider  ehcache
region.factory_class:org.hibernate.cache.ehcache.EhCacheRegionFactory
#加载ehcache缓存配置所在路径
cache:
ehcache:
config: classpath*:ehcache.xml
#thymeleaf配置
thymeleaf:
#开发阶段不开启缓存
cache: false
# Content-Type类型.
content-type: text/html
# mvc模板解析.
enabled: true
#模板编码类型.
encoding: UTF-8

#redis配置
redis:
host: localhost
port: 6379
timeout: 3000
pool:
max-active: 8
max-idle: 8
max-wait: -1
kafka:
bootstrap-servers: localhost:9092,localhost:9093
linger.ms: 1
client-id: 1
template:
default-topic: bootkafka
producer:
bootstrap-servers: localhost:9092,localhost:9093
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
retries: 1
batch-size: 16384
acks: 1
consumer:
group-id: testgroup
bootstrap-servers: localhost:9092,localhost:9093
server:
#项目端口号
port: 81
#访问路径
context-path: /test
#session超时时间 秒
session:
timeout: 300
#开启监控
endpoints:
actuator:
enabled: true
beans:
enabled: true
management:
security:
enabled: true
roles: USER

4.创建一个Controller 。启动并在浏览器访问:

@RestController
public class HomeController extends BaseController {

@GetMapping("/test")
public String test() {

return "hello springboot";

}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Boot 搭建