Skip to content

Commit

Permalink
springboot2 第一章
Browse files Browse the repository at this point in the history
  • Loading branch information
levin950825 committed Apr 20, 2018
1 parent da8ded2 commit 8975147
Show file tree
Hide file tree
Showing 29 changed files with 264 additions and 60 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# spring-boot2
- **chapter1:** 基于 spring-boot2.x 整合了 es 搜索
- **chapter2:** 基于 spring-boot2.x 整合了 rabbitMQ,禁用默认ACK配置改为手动ACK,延迟队列消费
- **chapter3:** 基于 spring-boot2.x 整合了 Redis
- **chapter1:** 基于 spring-boot2.x 的 入门程序
49 changes: 49 additions & 0 deletions chapter1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
gradle/
# maven #
target
logs

# eclipse #
.settings
.project
.classpath
.log

# windows #
Thumbs.db

# Mac #
.DS_Store

# Package Files #
*.war
*.ear

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
.idea/
overlays
bin


### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
105 changes: 48 additions & 57 deletions chapter1/pom.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.battcn</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>battcn-boot</name>
<description>Demo project for Spring Boot</description>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>


<dependencies>
<dependency>
<groupId>com.battcn</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.4.4-RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
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.battcn</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>chapter1</name>
<description>我的用第一个SpringBoot工程</description>

<!--版本采用的是最新的 2.0.1.RELEASE TODO 开发中请记得版本一定要选择 RELEASE 哦 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- 默认就内嵌了Tomcat 容器,如需要更换容器也极其简单-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试包,当我们使用 mvn package 的时候该包并不会被打入,因为它的生命周期只在 test 之内-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
43 changes: 43 additions & 0 deletions chapter1/src/main/java/com/battcn/Chapter1Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.battcn;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
* 我的第一个SpringBoot程序
* 其中 @RestController 等同于 (@Controller 与 @ResponseBody)
*
* @author Levin
*/
@RestController
@SpringBootApplication
public class Chapter1Application {

public static void main(String[] args) {
SpringApplication.run(Chapter1Application.class, args);
}

@GetMapping("/demo1")
public String demo1() {
return "Hello battcn";
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
// 目的是
return args -> {
System.out.println("来看看 SpringBoot 默认为我们提供的 Bean:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
Arrays.stream(beanNames).forEach(System.out::println);
};
}
}
2 changes: 2 additions & 0 deletions chapter1/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server.port=9090
server.servlet.context-path=/chapter1
Binary file added chapter1/src/main/resources/banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions chapter1/src/test/java/com/battcn/Chapter1ApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.battcn;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Chapter1ApplicationTests {


@LocalServerPort
private int port;

private URL base;

@Autowired
private TestRestTemplate template;

@Before
public void setUp() throws Exception {
// TODO 因为我们修改了 content-path 所以请求后面要带上
this.base = new URL("http://localhost:" + port + "/chapter1/demo1");
}

@Test
public void demo1() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
assertEquals(response.getBody(), "Hello battcn");
}
}
62 changes: 62 additions & 0 deletions chapter_n1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.battcn</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>battcn-boot</name>
<description>Demo project for Spring Boot</description>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>


<dependencies>
<dependency>
<groupId>com.battcn</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.4.4-RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions chapter_n4/src/main/java/com/battcn/MyClassLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.battcn;

/**
* @author Levin
* @since 2018/4/18 0018
*/
public class MyClassLoaderTest {


public static void main(String[] args) {

final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);

}


}

0 comments on commit 8975147

Please sign in to comment.