├── .gitignore
├── Chapter 1
└── helloworld
│ ├── .gitignore
│ ├── pom-without-parent.xml
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── geektime
│ │ │ └── demo
│ │ │ └── HelloWorldApplication.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── geektime
│ └── demo
│ └── HelloWorldApplicationTests.java
├── Chapter 2
├── datasource-demo
│ ├── .gitignore
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── geektime
│ │ │ │ └── spring
│ │ │ │ └── data
│ │ │ │ └── datasourcedemo
│ │ │ │ └── DataSourceDemoApplication.java
│ │ └── resources
│ │ │ ├── application.properties
│ │ │ ├── data.sql
│ │ │ └── schema.sql
│ │ └── test
│ │ └── java
│ │ └── geektime
│ │ └── spring
│ │ └── data
│ │ └── datasourcedemo
│ │ └── DatasourceDemoApplicationTests.java
├── multi-datasource-demo
│ ├── .gitignore
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── geektime
│ │ │ │ └── spring
│ │ │ │ └── data
│ │ │ │ └── multidatasourcedemo
│ │ │ │ └── MultiDataSourceDemoApplication.java
│ │ └── resources
│ │ │ └── application.properties
│ │ └── test
│ │ └── java
│ │ └── geektime
│ │ └── spring
│ │ └── data
│ │ └── multidatasourcedemo
│ │ └── MultiDataSourceDemoApplicationTests.java
└── pure-spring-datasource-demo
│ ├── .gitignore
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ └── geektime
│ │ └── spring
│ │ └── data
│ │ └── datasourcedemo
│ │ └── DataSourceDemo.java
│ └── resources
│ └── applicationContext.xml
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | pom.xml.tag
3 | pom.xml.releaseBackup
4 | pom.xml.versionsBackup
5 | pom.xml.next
6 | release.properties
7 | dependency-reduced-pom.xml
8 | buildNumber.properties
9 | .mvn/timing.properties
10 |
11 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
12 | !/.mvn/wrapper/maven-wrapper.jar
13 |
--------------------------------------------------------------------------------
/Chapter 1/helloworld/.gitignore:
--------------------------------------------------------------------------------
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 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/Chapter 1/helloworld/pom-without-parent.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | geektime.demo
7 | helloworld
8 | 0.0.1-SNAPSHOT
9 | HelloWorld
10 | Demo project for Spring Boot
11 |
12 |
13 | 1.8
14 |
15 |
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-dependencies
21 | 2.1.1.RELEASE
22 | pom
23 | import
24 |
25 |
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-web
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-test
37 | test
38 |
39 |
40 |
41 |
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-maven-plugin
46 | 2.1.1.RELEASE
47 |
48 |
49 |
50 | repackage
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/Chapter 1/helloworld/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.1.RELEASE
9 |
10 |
11 | geektime.demo
12 | helloworld
13 | 0.0.1-SNAPSHOT
14 | HelloWorld
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-maven-plugin
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter 1/helloworld/src/main/java/geektime/demo/HelloWorldApplication.java:
--------------------------------------------------------------------------------
1 | package geektime.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RestController;
7 |
8 | @SpringBootApplication
9 | @RestController
10 | public class HelloWorldApplication {
11 |
12 | public static void main(String[] args) {
13 | SpringApplication.run(HelloWorldApplication.class, args);
14 | }
15 |
16 | @RequestMapping("/hello")
17 | public String hello() {
18 | return "Hello World!";
19 | }
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/Chapter 1/helloworld/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/geektime-geekbang/geektime-spring-samples/24af291bc7e211142d0596c44bade729f8a7e804/Chapter 1/helloworld/src/main/resources/application.properties
--------------------------------------------------------------------------------
/Chapter 1/helloworld/src/test/java/geektime/demo/HelloWorldApplicationTests.java:
--------------------------------------------------------------------------------
1 | package geektime.demo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class HelloWorldApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/.gitignore:
--------------------------------------------------------------------------------
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 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.1.RELEASE
9 |
10 |
11 | geektime.spring.data
12 | datasource-demo
13 | 0.0.1-SNAPSHOT
14 | datasource-demo
15 |
16 |
17 |
18 | 11
19 |
20 |
21 |
22 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-jdbc
35 |
36 |
37 | org.projectlombok
38 | lombok
39 |
40 |
41 | com.h2database
42 | h2
43 | runtime
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-maven-plugin
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/src/main/java/geektime/spring/data/datasourcedemo/DataSourceDemoApplication.java:
--------------------------------------------------------------------------------
1 | package geektime.spring.data.datasourcedemo;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.boot.CommandLineRunner;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.jdbc.core.JdbcTemplate;
9 |
10 | import javax.sql.DataSource;
11 | import java.sql.Connection;
12 | import java.sql.SQLException;
13 |
14 | @SpringBootApplication
15 | @Slf4j
16 | public class DataSourceDemoApplication implements CommandLineRunner {
17 | @Autowired
18 | private DataSource dataSource;
19 |
20 | @Autowired
21 | private JdbcTemplate jdbcTemplate;
22 |
23 | public static void main(String[] args) {
24 | SpringApplication.run(DataSourceDemoApplication.class, args);
25 | }
26 |
27 | @Override
28 | public void run(String... args) throws Exception {
29 | showConnection();
30 | showData();
31 | }
32 |
33 | private void showConnection() throws SQLException {
34 | log.info(dataSource.toString());
35 | Connection conn = dataSource.getConnection();
36 | log.info(conn.toString());
37 | conn.close();
38 | }
39 |
40 | private void showData() {
41 | jdbcTemplate.queryForList("SELECT * FROM FOO")
42 | .forEach(row -> log.info(row.toString()));
43 | }
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | management.endpoints.web.exposure.include=*
2 | spring.output.ansi.enabled=ALWAYS
3 |
4 | spring.datasource.url=jdbc:h2:mem:testdb
5 | spring.datasource.username=sa
6 | spring.datasource.password=
7 | spring.datasource.hikari.maxPoolSize=5
8 | spring.datasource.hikari.minIdle=5
9 | spring.datasource.hikari.idleTimeout=600000
10 | spring.datasource.hikari.connectionTimeout=30000
11 | spring.datasource.hikari.maxLifetime=1800000
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 | INSERT INTO FOO (ID, BAR) VALUES (1, 'aaa');
2 | INSERT INTO FOO (ID, BAR) VALUES (2, 'bbb');
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/src/main/resources/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE FOO (ID INT IDENTITY, BAR VARCHAR(64));
--------------------------------------------------------------------------------
/Chapter 2/datasource-demo/src/test/java/geektime/spring/data/datasourcedemo/DatasourceDemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package geektime.spring.data.datasourcedemo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class DatasourceDemoApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/Chapter 2/multi-datasource-demo/.gitignore:
--------------------------------------------------------------------------------
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 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/Chapter 2/multi-datasource-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.1.RELEASE
9 |
10 |
11 | geektime.spring.data
12 | multi-datasource-demo
13 | 0.0.1-SNAPSHOT
14 | multi-datasource-demo
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-actuator
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-jdbc
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-web
33 |
34 |
35 |
36 | com.h2database
37 | h2
38 | runtime
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | true
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-test
48 | test
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-maven-plugin
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/Chapter 2/multi-datasource-demo/src/main/java/geektime/spring/data/multidatasourcedemo/MultiDataSourceDemoApplication.java:
--------------------------------------------------------------------------------
1 | package geektime.spring.data.multidatasourcedemo;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
9 | import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
10 | import org.springframework.boot.context.properties.ConfigurationProperties;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
13 | import org.springframework.transaction.PlatformTransactionManager;
14 |
15 | import javax.annotation.Resource;
16 | import javax.sql.DataSource;
17 |
18 | @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
19 | DataSourceTransactionManagerAutoConfiguration.class,
20 | JdbcTemplateAutoConfiguration.class})
21 | @Slf4j
22 | public class MultiDataSourceDemoApplication {
23 |
24 | public static void main(String[] args) {
25 | SpringApplication.run(MultiDataSourceDemoApplication.class, args);
26 | }
27 |
28 | @Bean
29 | @ConfigurationProperties("foo.datasource")
30 | public DataSourceProperties fooDataSourceProperties() {
31 | return new DataSourceProperties();
32 | }
33 |
34 | @Bean
35 | public DataSource fooDataSource() {
36 | DataSourceProperties dataSourceProperties = fooDataSourceProperties();
37 | log.info("foo datasource: {}", dataSourceProperties.getUrl());
38 | return dataSourceProperties.initializeDataSourceBuilder().build();
39 | }
40 |
41 | @Bean
42 | @Resource
43 | public PlatformTransactionManager fooTxManager(DataSource fooDataSource) {
44 | return new DataSourceTransactionManager(fooDataSource);
45 | }
46 |
47 | @Bean
48 | @ConfigurationProperties("bar.datasource")
49 | public DataSourceProperties barDataSourceProperties() {
50 | return new DataSourceProperties();
51 | }
52 |
53 | @Bean
54 | public DataSource barDataSource() {
55 | DataSourceProperties dataSourceProperties = barDataSourceProperties();
56 | log.info("bar datasource: {}", dataSourceProperties.getUrl());
57 | return dataSourceProperties.initializeDataSourceBuilder().build();
58 | }
59 |
60 | @Bean
61 | @Resource
62 | public PlatformTransactionManager barTxManager(DataSource barDataSource) {
63 | return new DataSourceTransactionManager(barDataSource);
64 | }
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/Chapter 2/multi-datasource-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | management.endpoints.web.exposure.include=*
2 | spring.output.ansi.enabled=ALWAYS
3 |
4 | foo.datasource.url=jdbc:h2:mem:foo
5 | foo.datasource.username=sa
6 | foo.datasource.password=
7 |
8 | bar.datasource.url=jdbc:h2:mem:bar
9 | bar.datasource.username=sa
10 | bar.datasource.password=
--------------------------------------------------------------------------------
/Chapter 2/multi-datasource-demo/src/test/java/geektime/spring/data/multidatasourcedemo/MultiDataSourceDemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package geektime.spring.data.multidatasourcedemo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class MultiDataSourceDemoApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/Chapter 2/pure-spring-datasource-demo/.gitignore:
--------------------------------------------------------------------------------
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 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/Chapter 2/pure-spring-datasource-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | geektime.spring.data
8 | pure-spring-datasource-demo
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 5.1.3.RELEASE
13 |
14 |
15 |
16 |
17 | org.springframework
18 | spring-context
19 | ${spring.version}
20 |
21 |
22 | org.springframework
23 | spring-jdbc
24 | ${spring.version}
25 |
26 |
27 |
28 | org.apache.commons
29 | commons-dbcp2
30 | RELEASE
31 |
32 |
33 | com.h2database
34 | h2
35 | RELEASE
36 | runtime
37 |
38 |
39 |
--------------------------------------------------------------------------------
/Chapter 2/pure-spring-datasource-demo/src/main/java/geektime/spring/data/datasourcedemo/DataSourceDemo.java:
--------------------------------------------------------------------------------
1 | package geektime.spring.data.datasourcedemo;
2 |
3 | import org.apache.commons.dbcp2.BasicDataSourceFactory;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.context.ApplicationContext;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.context.support.ClassPathXmlApplicationContext;
9 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
10 | import org.springframework.transaction.PlatformTransactionManager;
11 | import org.springframework.transaction.annotation.EnableTransactionManagement;
12 |
13 | import javax.sql.DataSource;
14 | import java.sql.Connection;
15 | import java.sql.SQLException;
16 | import java.util.Arrays;
17 | import java.util.Properties;
18 |
19 | @Configuration
20 | @EnableTransactionManagement
21 | public class DataSourceDemo {
22 | @Autowired
23 | private DataSource dataSource;
24 |
25 | public static void main(String[] args) throws SQLException {
26 | ApplicationContext applicationContext =
27 | new ClassPathXmlApplicationContext("applicationContext*.xml");
28 | showBeans(applicationContext);
29 | dataSourceDemo(applicationContext);
30 | }
31 |
32 | @Bean(destroyMethod = "close")
33 | public DataSource dataSource() throws Exception {
34 | Properties properties = new Properties();
35 | properties.setProperty("driverClassName", "org.h2.Driver");
36 | properties.setProperty("url", "jdbc:h2:mem:testdb");
37 | properties.setProperty("username", "sa");
38 | return BasicDataSourceFactory.createDataSource(properties);
39 | }
40 |
41 | @Bean
42 | public PlatformTransactionManager transactionManager() throws Exception {
43 | return new DataSourceTransactionManager(dataSource());
44 | }
45 |
46 | private static void showBeans(ApplicationContext applicationContext) {
47 | System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
48 | }
49 |
50 | private static void dataSourceDemo(ApplicationContext applicationContext) throws SQLException {
51 | DataSourceDemo demo = applicationContext.getBean("dataSourceDemo", DataSourceDemo.class);
52 | demo.showDataSource();
53 | }
54 |
55 | public void showDataSource() throws SQLException {
56 | System.out.println(dataSource.toString());
57 | Connection conn = dataSource.getConnection();
58 | System.out.println(conn.toString());
59 | conn.close();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/Chapter 2/pure-spring-datasource-demo/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # geektime-spring-samples
2 | 极客时间课程示例
3 |
--------------------------------------------------------------------------------