Skip to content

Commit d5fc407

Browse files
committed
Add lesson 19
1 parent 527ebfe commit d5fc407

File tree

18 files changed

+547
-0
lines changed

18 files changed

+547
-0
lines changed
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.segmentfault</groupId>
7+
<artifactId>spring-boot-lesson-19</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-19</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.6.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-actuator</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-tx</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.segmentfault.springbootlesson19;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootLesson19Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootLesson19Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.segmentfault.springbootlesson19.configuration;
2+
3+
import com.segmentfault.springbootlesson19.domain.Person;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Primary;
7+
8+
/**
9+
* {@link Person} Bean 配置
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.09.13
14+
*/
15+
@Configuration
16+
public class PersonConfiguration {
17+
18+
19+
@Bean("primaryPerson")
20+
@Primary
21+
public Person person() {
22+
23+
Person person = new Person();
24+
25+
person.setId(1L);
26+
person.setName("小马哥");
27+
person.setAge(32);
28+
29+
return person;
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.segmentfault.springbootlesson19.controller;
2+
3+
import com.segmentfault.springbootlesson19.domain.Person;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* TODO
11+
*
12+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
13+
* @see
14+
* @since 2017.09.13
15+
*/
16+
@RestController
17+
public class PersonController {
18+
19+
@Autowired
20+
private Person person;
21+
22+
@GetMapping
23+
public Person person() {
24+
return person;
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.segmentfault.springbootlesson19.domain;
2+
3+
/**
4+
* Person 类
5+
*
6+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
7+
* @see
8+
* @since 2017.09.13
9+
*/
10+
public class Person {
11+
12+
private Long id;
13+
14+
private String name;
15+
16+
private Integer age;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public Integer getAge() {
35+
return age;
36+
}
37+
38+
public void setAge(Integer age) {
39+
this.age = age;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Person{" +
45+
"id=" + id +
46+
", name='" + name + '\'' +
47+
", age=" + age +
48+
'}';
49+
}
50+
51+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# 指定 端口 0 是操作系统的可用随机端口
3+
server.port = 0
4+
5+
# 关闭管理 Endpoint 安全
6+
management.security.enabled = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.segmentfault.springbootlesson19;
2+
3+
import org.junit.Test;
4+
import org.springframework.core.env.Environment;
5+
import org.springframework.core.env.StandardEnvironment;
6+
import org.springframework.mock.env.MockEnvironment;
7+
import org.springframework.web.context.support.StandardServletEnvironment;
8+
9+
import static org.junit.Assert.*;
10+
11+
/**
12+
* {@link Environment}
13+
*
14+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
15+
* @see
16+
* @since 2017.09.13
17+
*/
18+
public class EnvironmentTest {
19+
20+
@Test
21+
public void testSystemProperty() {
22+
assertNotNull(System.getProperty("os.arch"));
23+
24+
// Environment environment = new StandardEnvironment();
25+
// Environment webEnvironment = new StandardServletEnvironment();
26+
MockEnvironment environment = new MockEnvironment();
27+
28+
environment.setProperty("user.country", "EN");
29+
30+
assertEquals("EN", environment.getProperty("user.country"));
31+
32+
}
33+
34+
@Test
35+
public void testManagementSecurityEnabled() {
36+
37+
MockEnvironment environment = new MockEnvironment();
38+
39+
environment.setProperty("management.security.enabled", "true");
40+
41+
assertTrue(environment.getProperty("management.security.enabled", boolean.class));
42+
43+
}
44+
45+
@Test
46+
public void testManagementSecurityDisabled() {
47+
48+
MockEnvironment environment = new MockEnvironment();
49+
50+
environment.setProperty("management.security.enabled", "false");
51+
52+
assertFalse(environment.getProperty("management.security.enabled", boolean.class));
53+
54+
}
55+
}

0 commit comments

Comments
 (0)