Skip to content

Commit c01b611

Browse files
committed
Add lesson 10
1 parent e4a1474 commit c01b611

File tree

15 files changed

+416
-0
lines changed

15 files changed

+416
-0
lines changed
Loading
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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-10</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-10</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+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-actuator</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>javax.cache</groupId>
36+
<artifactId>cache-api</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-cache</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-data-redis</artifactId>
47+
</dependency>
48+
49+
<!--<dependency>-->
50+
<!--<groupId>org.springframework</groupId>-->
51+
<!--<artifactId>spring-jdbc</artifactId>-->
52+
<!--</dependency>-->
53+
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-web</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-test</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
76+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.segmentfault.springbootlesson10;
2+
3+
import org.springframework.cache.CacheManager;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.cache.concurrent.ConcurrentMapCache;
6+
import org.springframework.cache.support.SimpleCacheManager;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.transaction.annotation.EnableTransactionManagement;
10+
11+
import java.util.Arrays;
12+
13+
/**
14+
* 缓存的配置
15+
*
16+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
17+
* @see
18+
* @since 2017.08.04
19+
*/
20+
@Configuration
21+
@EnableCaching
22+
//@EnableTransactionManagement
23+
public class CacheConfiguration {
24+
25+
@Bean
26+
public CacheManager simpleCacheManager() {
27+
28+
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
29+
30+
ConcurrentMapCache cache = new ConcurrentMapCache("cache-1");
31+
ConcurrentMapCache personsCache = new ConcurrentMapCache("persons");
32+
33+
simpleCacheManager.setCaches(Arrays.asList(cache, personsCache));
34+
35+
return simpleCacheManager;
36+
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.segmentfault.springbootlesson10;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
import java.util.concurrent.locks.Lock;
7+
import java.util.concurrent.locks.ReentrantLock;
8+
9+
@SpringBootApplication
10+
public class SpringBootLesson10Application {
11+
12+
//lock-free
13+
// private volatile int data;
14+
//
15+
private synchronized void doSome() {
16+
17+
Lock outterLock = new ReentrantLock();
18+
Lock innterLock = new ReentrantLock();
19+
20+
try {
21+
outterLock.tryLock();
22+
innterLock.tryLock();
23+
} finally {
24+
innterLock.unlock();
25+
outterLock.unlock();
26+
}
27+
28+
}
29+
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(SpringBootLesson10Application.class, args);
33+
34+
// Integer var = new Integer(1);
35+
// Integer var2 = new Integer(1);
36+
// System.out.println(var.hashCode());
37+
// System.out.println(var2.hashCode());
38+
//
39+
// System.out.println(System.identityHashCode(var));
40+
// System.out.println(System.identityHashCode(var2));
41+
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.segmentfault.springbootlesson10.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.cache.Cache;
6+
import org.springframework.cache.CacheManager;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* TODO
14+
*
15+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
16+
* @see
17+
* @since 2017.08.04
18+
*/
19+
@RestController
20+
@RequestMapping("/cache")
21+
public class CacheController {
22+
23+
@Autowired
24+
@Qualifier("simpleCacheManager")
25+
private CacheManager simpleCacheManager;
26+
27+
@PostMapping("/save")
28+
public Map<String, Object> save(@RequestParam String key, @RequestParam String value) {
29+
30+
Cache cache = simpleCacheManager.getCache("cache-1");
31+
32+
cache.put(key, value);
33+
34+
Map<String, Object> result = new HashMap<>();
35+
36+
result.put(key, value);
37+
38+
return result;
39+
}
40+
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.segmentfault.springbootlesson10.controller;
2+
3+
import com.segmentfault.springbootlesson10.entity.Person;
4+
import com.segmentfault.springbootlesson10.repository.PersonRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* TODO
12+
*
13+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
14+
* @see
15+
* @since 2017.08.04
16+
*/
17+
@RestController
18+
@RequestMapping("/person")
19+
public class PersonController {
20+
21+
@Autowired
22+
private PersonRepository repository;
23+
24+
@PostMapping("/save")
25+
public Person save(@RequestBody Person person) {
26+
27+
repository.savePerson(person);
28+
29+
return person;
30+
31+
}
32+
33+
34+
@GetMapping("/get/{id}")
35+
public Person get(@PathVariable String id) {
36+
37+
return repository.findPerson(id);
38+
39+
}
40+
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.segmentfault.springbootlesson10.entity;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 人
7+
*
8+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
9+
* @see
10+
* @since 2017.08.04
11+
*/
12+
public class Person implements Serializable {
13+
14+
private String id;
15+
16+
private String name;
17+
18+
private int age;
19+
20+
public String getId() {
21+
return id;
22+
}
23+
24+
public void setId(String id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public int getAge() {
37+
return age;
38+
}
39+
40+
public void setAge(int age) {
41+
this.age = age;
42+
}
43+
}

0 commit comments

Comments
 (0)