diff --git a/Chapter 1/helloworld/pom.xml b/Chapter 1/helloworld/pom.xml index 792b4be1..aec87d0d 100644 --- a/Chapter 1/helloworld/pom.xml +++ b/Chapter 1/helloworld/pom.xml @@ -1,11 +1,10 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent - 2.1.1.RELEASE + 1.5.9.RELEASE geektime.demo @@ -14,8 +13,9 @@ HelloWorld Demo project for Spring Boot + - 1.8 + 1.7 diff --git a/Chapter 2/druid-demo/src/main/java/geektime/spring/data/druiddemo/ConnectionLogFilter.java b/Chapter 2/druid-demo/src/main/java/geektime/spring/data/druiddemo/ConnectionLogFilter.java index e637dfd9..15297648 100644 --- a/Chapter 2/druid-demo/src/main/java/geektime/spring/data/druiddemo/ConnectionLogFilter.java +++ b/Chapter 2/druid-demo/src/main/java/geektime/spring/data/druiddemo/ConnectionLogFilter.java @@ -13,6 +13,7 @@ public class ConnectionLogFilter extends FilterEventAdapter { @Override public void connection_connectBefore(FilterChain chain, Properties info) { log.info("BEFORE CONNECTION!"); + } @Override diff --git a/Chapter 2/errorcode-demo/pom.xml b/Chapter 2/errorcode-demo/pom.xml index 40ac1d80..b673144a 100644 --- a/Chapter 2/errorcode-demo/pom.xml +++ b/Chapter 2/errorcode-demo/pom.xml @@ -1,11 +1,10 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent - 2.1.1.RELEASE + 1.5.9.RELEASE geektime.spring.data @@ -15,7 +14,7 @@ Demo project for Spring Boot - 1.8 + 1.7 diff --git a/Chapter 2/errorcode-demo/src/main/java/geektime/spring/data/errorcodedemo/CustomDuplicatedKeyException.java b/Chapter 2/errorcode-demo/src/main/java/geektime/spring/data/errorcodedemo/CustomDuplicatedKeyException.java index 08fcb35d..e05bb202 100644 --- a/Chapter 2/errorcode-demo/src/main/java/geektime/spring/data/errorcodedemo/CustomDuplicatedKeyException.java +++ b/Chapter 2/errorcode-demo/src/main/java/geektime/spring/data/errorcodedemo/CustomDuplicatedKeyException.java @@ -3,7 +3,10 @@ import org.springframework.dao.DuplicateKeyException; public class CustomDuplicatedKeyException extends DuplicateKeyException { - public CustomDuplicatedKeyException(String msg) { + + private static final long serialVersionUID = 3061506279667836931L; + + public CustomDuplicatedKeyException(String msg) { super(msg); } diff --git a/Chapter 3/client-demo/.gitignore b/Chapter 3/client-demo/.gitignore new file mode 100644 index 00000000..153c9335 --- /dev/null +++ b/Chapter 3/client-demo/.gitignore @@ -0,0 +1,29 @@ +HELP.md +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 3/client-demo/mvnw b/Chapter 3/client-demo/mvnw new file mode 100644 index 00000000..8b9da3b8 --- /dev/null +++ b/Chapter 3/client-demo/mvnw @@ -0,0 +1,286 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + wget "$jarUrl" -O "$wrapperJarPath" + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + curl -o "$wrapperJarPath" "$jarUrl" + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/Chapter 3/client-demo/mvnw.cmd b/Chapter 3/client-demo/mvnw.cmd new file mode 100644 index 00000000..fef5a8f7 --- /dev/null +++ b/Chapter 3/client-demo/mvnw.cmd @@ -0,0 +1,161 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" +FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + echo Found %WRAPPER_JAR% +) else ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" + echo Finished downloading %WRAPPER_JAR% +) +@REM End of extension + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/Chapter 3/client-demo/pom.xml b/Chapter 3/client-demo/pom.xml new file mode 100644 index 00000000..aff8c5c9 --- /dev/null +++ b/Chapter 3/client-demo/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.4.RELEASE + + + geektime.spring.springbucks + client-demo + 0.0.1-SNAPSHOT + client-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + de.codecentric + spring-boot-admin-starter-client + 2.1.4 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/Chapter 3/client-demo/src/main/java/geektime/spring/springbucks/clientdemo/ClientDemoApplication.java b/Chapter 3/client-demo/src/main/java/geektime/spring/springbucks/clientdemo/ClientDemoApplication.java new file mode 100644 index 00000000..36e4fdf2 --- /dev/null +++ b/Chapter 3/client-demo/src/main/java/geektime/spring/springbucks/clientdemo/ClientDemoApplication.java @@ -0,0 +1,14 @@ +package geektime.spring.springbucks.clientdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ClientDemoApplication { + + public static void main(String[] args) { + + SpringApplication.run(ClientDemoApplication.class, args); + } + +} diff --git a/Chapter 3/client-demo/src/main/resources/application.properties b/Chapter 3/client-demo/src/main/resources/application.properties new file mode 100644 index 00000000..9ea119a1 --- /dev/null +++ b/Chapter 3/client-demo/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=spring-boot-admin-client +server.port=8081 +##������������ע�� +spring.boot.admin.client.url=http://localhost:8787 +management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/Chapter 3/client-demo/src/test/java/geektime/spring/springbucks/clientdemo/ClientDemoApplicationTests.java b/Chapter 3/client-demo/src/test/java/geektime/spring/springbucks/clientdemo/ClientDemoApplicationTests.java new file mode 100644 index 00000000..bd9ca844 --- /dev/null +++ b/Chapter 3/client-demo/src/test/java/geektime/spring/springbucks/clientdemo/ClientDemoApplicationTests.java @@ -0,0 +1,16 @@ +package geektime.spring.springbucks.clientdemo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ClientDemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/Chapter 3/jpademo/jpademo.iml b/Chapter 3/jpademo/jpademo.iml new file mode 100644 index 00000000..78b2cc53 --- /dev/null +++ b/Chapter 3/jpademo/jpademo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Chapter 3/jpademo/pom.xml b/Chapter 3/jpademo/pom.xml new file mode 100644 index 00000000..5462102e --- /dev/null +++ b/Chapter 3/jpademo/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.kevin + jpademo + 1.0-SNAPSHOT + + + + + mysql + mysql-connector-java + 5.1.38 + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-jdbc + 4.3.5.RELEASE + + + org.springframework + spring-context + 4.3.5.RELEASE + + + + + org.springframework.data + spring-data-jpa + 1.8.0.RELEASE + + + + org.hibernate + hibernate-entitymanager + 3.5.1-Final + + + + + + \ No newline at end of file diff --git a/Chapter 3/jpademo/src/main/java/com/kevin/domain/Employee.java b/Chapter 3/jpademo/src/main/java/com/kevin/domain/Employee.java new file mode 100644 index 00000000..e79e2bce --- /dev/null +++ b/Chapter 3/jpademo/src/main/java/com/kevin/domain/Employee.java @@ -0,0 +1,54 @@ +package com.kevin.domain; + + +import javax.persistence.*; + +/** + * @Entity 指定实体的名称 + * @Table 指定数据库表的名称 如果没有该注解,默认使用实体名称作为表名 + */ + +@Entity +//@Table(name = "tb_employee") +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO)//自动增长 + private Integer id; + + @Column(length = 20) + private String name; + + private Integer age; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + //@Column(length = 20) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + + @Override + public String toString() { + return "id: ["+ id +"]" + "name: ["+ name +"]" + "age: ["+ age +"]" ; + } +} diff --git a/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeCrudRepository.java b/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeCrudRepository.java new file mode 100644 index 00000000..9a713af0 --- /dev/null +++ b/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeCrudRepository.java @@ -0,0 +1,8 @@ +package com.kevin.repository; + +import com.kevin.domain.Employee; +import org.springframework.data.repository.CrudRepository; + +public interface EmployeeCrudRepository extends CrudRepository { + +} diff --git a/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeRepository.java b/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeRepository.java new file mode 100644 index 00000000..1f489117 --- /dev/null +++ b/Chapter 3/jpademo/src/main/java/com/kevin/repository/EmployeeRepository.java @@ -0,0 +1,30 @@ +package com.kevin.repository; + +import com.kevin.domain.Employee; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.RepositoryDefinition; + +import javax.persistence.criteria.CriteriaBuilder; +import java.util.List; + +//public interface EmployeeRepository extends Repository { + +@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class) +public interface EmployeeRepository { + + Employee findByName(String name); + + // where name = ?1 and age = ?2 + List findByNameAndAge(String name, Integer age); + + // where name = ?1 or age = ?2 + List findByNameOrAge(String name, Integer age); + + // where id in(?) + List findByIdIn(List ids); + + // where id in(?) or age = 12 + List findByIdInOrAge(List ids, Integer age); + + +} diff --git a/Chapter 3/jpademo/src/main/resources/beans-new.xml b/Chapter 3/jpademo/src/main/resources/beans-new.xml new file mode 100644 index 00000000..9c8c786c --- /dev/null +++ b/Chapter 3/jpademo/src/main/resources/beans-new.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + org.hibernate.cfg.ImprovedNamingStrategy + org.hibernate.dialect.MySQL5InnoDBDialect + update + true + true + + + + + + + + + + + + + + + + + + diff --git a/Chapter 3/jpademo/src/main/resources/db.properties b/Chapter 3/jpademo/src/main/resources/db.properties new file mode 100644 index 00000000..e69de29b diff --git a/Chapter 3/jpademo/src/test/java/com/kevin/SpringDataTest.java b/Chapter 3/jpademo/src/test/java/com/kevin/SpringDataTest.java new file mode 100644 index 00000000..2b4c378a --- /dev/null +++ b/Chapter 3/jpademo/src/test/java/com/kevin/SpringDataTest.java @@ -0,0 +1,41 @@ +package com.kevin; + +import com.kevin.repository.EmployeeCrudRepository; +import com.kevin.repository.EmployeeRepository; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public abstract class SpringDataTest { + + private ApplicationContext ctx = null; + + @Autowired + protected EmployeeRepository employeeRepository; + + @Autowired + protected EmployeeCrudRepository employeeCrudRepository; + + @Before + public void setup(){ + ctx = new ClassPathXmlApplicationContext("beans-new.xml"); + + employeeRepository = ctx.getBean(EmployeeRepository.class); + employeeCrudRepository = ctx.getBean(EmployeeCrudRepository.class); + + System.out.println("setup"); + } + @After + public void tearDown(){ + ctx = null; + System.out.println("tearDown"); + } + + @Test + public void test_entityManagerFactory(){ + + } +} diff --git a/Chapter 3/jpademo/src/test/java/com/kevin/repository/CrudRepositoryTest.java b/Chapter 3/jpademo/src/test/java/com/kevin/repository/CrudRepositoryTest.java new file mode 100644 index 00000000..ae9198bc --- /dev/null +++ b/Chapter 3/jpademo/src/test/java/com/kevin/repository/CrudRepositoryTest.java @@ -0,0 +1,20 @@ +package com.kevin.repository; + +import com.kevin.SpringDataTest; +import com.kevin.domain.Employee; +import org.junit.Test; + +public class CrudRepositoryTest extends SpringDataTest { + + @Test + public void test_curd(){ + + Employee employee = new Employee(); + employee.setAge(12); + employee.setName("test"); + + employeeCrudRepository.save(employee); + + } + +} diff --git a/Chapter 3/jpademo/src/test/java/com/kevin/repository/EmployeeRepositoryTest.java b/Chapter 3/jpademo/src/test/java/com/kevin/repository/EmployeeRepositoryTest.java new file mode 100644 index 00000000..04f449ad --- /dev/null +++ b/Chapter 3/jpademo/src/test/java/com/kevin/repository/EmployeeRepositoryTest.java @@ -0,0 +1,60 @@ +package com.kevin.repository; + +import com.kevin.SpringDataTest; +import com.kevin.domain.Employee; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class EmployeeRepositoryTest extends SpringDataTest { + + @Test + public void test_findByName(){ + Employee employee = employeeRepository.findByName("zhangsan"); + + System.out.println(employee.toString()); + } + @Test + public void test_findByNameAndAge(){ + List employees = employeeRepository.findByNameAndAge("test", 12); + + for (Employee employee: employees) { + + System.out.println(employee.toString()); + } + } + + + @Test + public void test_findByNameOrAge(){ + List employees = employeeRepository.findByNameOrAge("test", 12); + for (Employee employee: employees) { + System.out.println(employee.toString()); + } + } + + @Test + public void test_findByIdIn(){ + List ids = new ArrayList(); + ids.add(1); + ids.add(2); + ids.add(3); + List employees = employeeRepository.findByIdIn(ids); + for (Employee employee: employees) { + System.out.println(employee.toString()); + } + } + @Test + public void test_findByIdInOrAge(){ + List ids = new ArrayList(); + ids.add(1); + ids.add(2); + ids.add(3); + List employees = employeeRepository.findByIdInOrAge(ids, 13); + for (Employee employee: employees) { + System.out.println(employee.toString()); + } + } + +} diff --git a/Chapter 3/mybatis-demo/pom.xml b/Chapter 3/mybatis-demo/pom.xml index 73da9ce2..153656bd 100644 --- a/Chapter 3/mybatis-demo/pom.xml +++ b/Chapter 3/mybatis-demo/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.2.RELEASE + 1.5.9.RELEASE geektime.spring.data @@ -15,7 +15,7 @@ Demo project for Spring Boot - 1.8 + 1.7 diff --git a/Chapter 3/mybatis-demo/src/main/java/geektime/spring/data/mybatisdemo/handler/TimeTypeHandler.java b/Chapter 3/mybatis-demo/src/main/java/geektime/spring/data/mybatisdemo/handler/TimeTypeHandler.java new file mode 100644 index 00000000..758b10ca --- /dev/null +++ b/Chapter 3/mybatis-demo/src/main/java/geektime/spring/data/mybatisdemo/handler/TimeTypeHandler.java @@ -0,0 +1,5 @@ +package geektime.spring.data.mybatisdemo.handler; + +public class TimeTypeHandler { + +} diff --git a/Chapter 3/server-demo/.gitignore b/Chapter 3/server-demo/.gitignore new file mode 100644 index 00000000..153c9335 --- /dev/null +++ b/Chapter 3/server-demo/.gitignore @@ -0,0 +1,29 @@ +HELP.md +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 3/server-demo/mvnw b/Chapter 3/server-demo/mvnw new file mode 100644 index 00000000..8b9da3b8 --- /dev/null +++ b/Chapter 3/server-demo/mvnw @@ -0,0 +1,286 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + wget "$jarUrl" -O "$wrapperJarPath" + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + curl -o "$wrapperJarPath" "$jarUrl" + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/Chapter 3/server-demo/mvnw.cmd b/Chapter 3/server-demo/mvnw.cmd new file mode 100644 index 00000000..fef5a8f7 --- /dev/null +++ b/Chapter 3/server-demo/mvnw.cmd @@ -0,0 +1,161 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" +FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + echo Found %WRAPPER_JAR% +) else ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" + echo Finished downloading %WRAPPER_JAR% +) +@REM End of extension + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/Chapter 3/server-demo/pom.xml b/Chapter 3/server-demo/pom.xml new file mode 100644 index 00000000..70d1a537 --- /dev/null +++ b/Chapter 3/server-demo/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.4.RELEASE + + + geektime.spring.springbucks + server-demo + 0.0.1-SNAPSHOT + server-demo + Demo project for Spring Boot + + + 1.8 + 2.1.4 + + + + + junit + junit + + + org.springframework.boot + spring-boot-starter-test + + + org.springframework.boot + spring-boot-starter-web + + + + de.codecentric + spring-boot-admin-starter-server + + + + + + + org.projectlombok + lombok + true + + + + + + + de.codecentric + spring-boot-admin-dependencies + ${spring-boot-admin.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/SecurityConfig.java b/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/SecurityConfig.java new file mode 100644 index 00000000..e991a297 --- /dev/null +++ b/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/SecurityConfig.java @@ -0,0 +1,39 @@ +//package geektime.spring.springbucks.serverdemo; +// +//import org.springframework.context.annotation.Configuration; +//import org.springframework.security.config.annotation.web.builders.HttpSecurity; +//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +//import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; +//import org.springframework.security.web.csrf.CookieCsrfTokenRepository; +// +///** +// * @Author 肖拔赟 +// * @Description ${TODO} +// * @DATE Created on 2019-04-13 13:51 +// */ +//@Configuration +//public class SecurityConfig extends WebSecurityConfigurerAdapter { +// +// @Override +// protected void configure(HttpSecurity http) throws Exception { +// SavedRequestAwareAuthenticationSuccessHandler successHandler +// = new SavedRequestAwareAuthenticationSuccessHandler(); +// successHandler.setTargetUrlParameter("redirectTo"); +// successHandler.setDefaultTargetUrl("/"); +// +// http.authorizeRequests() +// .antMatchers("/assets/**").permitAll() +// .antMatchers("/login").permitAll() +// .anyRequest().authenticated().and() +// .formLogin().loginPage("/login") +// .successHandler(successHandler).and() +// .logout().logoutUrl("/logout").and() +// .httpBasic().and() +// .csrf() +// .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) +// .ignoringAntMatchers( +// "/instances", +// "/actuator/**" +// ); +// } +//} \ No newline at end of file diff --git a/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/ServerDemoApplication.java b/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/ServerDemoApplication.java new file mode 100644 index 00000000..73dfb2d8 --- /dev/null +++ b/Chapter 3/server-demo/src/main/java/geektime/spring/springbucks/serverdemo/ServerDemoApplication.java @@ -0,0 +1,18 @@ +package geektime.spring.springbucks.serverdemo; + +import de.codecentric.boot.admin.server.config.EnableAdminServer; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; + +@SpringBootApplication +@EnableAdminServer //启用管理服务器 用来标识该服务器为管理服务器 +public class ServerDemoApplication { + + public static void main(String[] args) { + + SpringApplication.run(ServerDemoApplication.class, args); + } + +} diff --git a/Chapter 3/server-demo/src/main/resources/application.properties b/Chapter 3/server-demo/src/main/resources/application.properties new file mode 100644 index 00000000..73753f53 --- /dev/null +++ b/Chapter 3/server-demo/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=8787 +spring.application.name=server-demo +spring.security.user.name=kevin +spring.security.user.password=123456 \ No newline at end of file diff --git a/Chapter 3/server-demo/src/test/java/geektime/spring/springbucks/serverdemo/ServerDemoApplicationTests.java b/Chapter 3/server-demo/src/test/java/geektime/spring/springbucks/serverdemo/ServerDemoApplicationTests.java new file mode 100644 index 00000000..0a05f6ff --- /dev/null +++ b/Chapter 3/server-demo/src/test/java/geektime/spring/springbucks/serverdemo/ServerDemoApplicationTests.java @@ -0,0 +1,16 @@ +package geektime.spring.springbucks.serverdemo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ServerDemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/Chapter 3/springbucks/pom.xml b/Chapter 3/springbucks/pom.xml index 79f4b208..448e91d6 100644 --- a/Chapter 3/springbucks/pom.xml +++ b/Chapter 3/springbucks/pom.xml @@ -50,6 +50,12 @@ spring-boot-starter-test test + + + org.slf4j + slf4j-api + 1.7.25 + diff --git a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/context/ContextHierarchyDemoApplication.java b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/context/ContextHierarchyDemoApplication.java index 90330782..6c60dc56 100644 --- a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/context/ContextHierarchyDemoApplication.java +++ b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/context/ContextHierarchyDemoApplication.java @@ -21,15 +21,16 @@ public static void main(String[] args) { @Override public void run(ApplicationArguments args) throws Exception { ApplicationContext fooContext = new AnnotationConfigApplicationContext(FooConfig.class); - ClassPathXmlApplicationContext barContext = new ClassPathXmlApplicationContext( - new String[] {"applicationContext.xml"}, fooContext); TestBean bean = fooContext.getBean("testBeanX", TestBean.class); bean.hello(); log.info("============="); + ClassPathXmlApplicationContext barContext = new ClassPathXmlApplicationContext( + new String[] {"applicationContext.xml"}, fooContext); bean = barContext.getBean("testBeanX", TestBean.class); bean.hello(); + log.info("============="); bean = barContext.getBean("testBeanY", TestBean.class); bean.hello(); diff --git a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooAspect.java b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooAspect.java index 936ab69a..c4d16ae2 100644 --- a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooAspect.java +++ b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooAspect.java @@ -3,12 +3,14 @@ import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; @Aspect @Slf4j public class FooAspect { @AfterReturning("bean(testBean*)") public void printAfter() { - log.info("after hello()"); + log.info("FooAspect after hello()"); } } diff --git a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooConfig.java b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooConfig.java index 81f7b907..56365cc9 100644 --- a/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooConfig.java +++ b/Chapter 6/context-hierarchy-demo/src/main/java/geektime/spring/web/foo/FooConfig.java @@ -1,20 +1,31 @@ package geektime.spring.web.foo; import geektime.spring.web.context.TestBean; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; +/** + * @EnableAspectJAutoProxy + * 启动aop,在aop切面增强中,在父容器 + */ + @Configuration @EnableAspectJAutoProxy +@Slf4j public class FooConfig { + @Bean public TestBean testBeanX() { + log.info("FooConfig testBeanX....."); return new TestBean("foo"); } @Bean public TestBean testBeanY() { + log.info("FooConfig testBeanY....."); return new TestBean("foo"); } diff --git a/Chapter 6/context-hierarchy-demo/src/main/resources/applicationContext.xml b/Chapter 6/context-hierarchy-demo/src/main/resources/applicationContext.xml index 9c1987ea..58eb4dfd 100644 --- a/Chapter 6/context-hierarchy-demo/src/main/resources/applicationContext.xml +++ b/Chapter 6/context-hierarchy-demo/src/main/resources/applicationContext.xml @@ -6,6 +6,7 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> + diff --git "a/PDF/\347\254\2543\347\253\240-O" "b/PDF/\347\254\2543\347\253\240-O" new file mode 100644 index 00000000..e69de29b