├── README.md ├── demo-springboot-web ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── loafer │ │ │ └── demo │ │ │ └── springboot │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ ├── WebApplication.java │ │ │ └── web │ │ │ └── ResponseData.java │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ └── WebApplicationTests.java └── pom.xml ├── springboot-config ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ ├── application.properties │ │ ├── config │ │ │ └── application.properties │ │ └── app.properties │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ ├── AppPropertisApplication.java │ │ └── ConfigComponent.java └── pom.xml ├── springboot-mvc-converter ├── src │ ├── main │ │ ├── resources │ │ │ └── application.properties │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── loafer │ │ │ └── demo │ │ │ └── springboot │ │ │ ├── WebApplication.java │ │ │ ├── WebConfigAdapter.java │ │ │ ├── WebConfigSupport.java │ │ │ └── convert │ │ │ └── ConvertController.java │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ └── convert │ │ └── ConvertControllerTest.java └── pom.xml ├── springboot-external-config ├── config │ ├── application.properties │ └── application-local.properties ├── src │ └── main │ │ ├── resources │ │ ├── application.properties │ │ └── application-local.properties │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ └── config │ │ ├── ExternalConfigApplication.java │ │ └── ExternalConfigComponent.java ├── docker-compose.yml ├── Dockerfile └── pom.xml ├── .gitignore ├── springboot-multidatasource ├── .gitignore ├── src │ ├── main │ │ ├── resources │ │ │ └── application.properties │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── loafer │ │ │ └── demo │ │ │ ├── Application.java │ │ │ ├── datasource │ │ │ ├── annotation │ │ │ │ └── TargetDataSource.java │ │ │ ├── DataSourceContext.java │ │ │ ├── DataSourceAspect.java │ │ │ ├── DatasourceProxyBeanProcessor.java │ │ │ └── RoutingDataSource.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── service │ │ │ └── UserService.java │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── ApplicationTests.java └── pom.xml ├── springboot-logging-log4j ├── src │ └── main │ │ ├── resources │ │ ├── application.properties │ │ └── log4j-spring.properties │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ └── Log4jApplication.java └── pom.xml ├── springboot-config-yaml ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ ├── FooComponent.java │ │ ├── ConfigComponent.java │ │ ├── ConnectionSettings.java │ │ └── YamlConfApplication.java └── pom.xml ├── springboot-logging ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ └── LoggingApplication.java └── pom.xml ├── without-starter-parent ├── src │ └── main │ │ └── java │ │ └── com │ │ └── github │ │ └── loafer │ │ └── demo │ │ └── springboot │ │ ├── hello │ │ └── web │ │ │ └── HelloController.java │ │ └── Application.java └── pom.xml └── with-starter-parent ├── src └── main │ └── java │ └── com │ └── github │ └── loafer │ └── demo │ └── springboot │ ├── hello │ └── web │ │ └── HelloController.java │ └── Application.java └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-tutorials 2 | -------------------------------------------------------------------------------- /demo-springboot-web/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | property: 2 | four: 'Value Four from YAML File' -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | property.one=Value One 2 | property.two=Value Two -------------------------------------------------------------------------------- /springboot-mvc-converter/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework=info -------------------------------------------------------------------------------- /springboot-external-config/config/application.properties: -------------------------------------------------------------------------------- 1 | property.three=Value Three Overridden 2 | property.four=Value Four -------------------------------------------------------------------------------- /springboot-config/src/main/resources/config/application.properties: -------------------------------------------------------------------------------- 1 | property.two=Value Two Overridden 2 | property.three=Value Three -------------------------------------------------------------------------------- /springboot-external-config/config/application-local.properties: -------------------------------------------------------------------------------- 1 | property.five=Value Five Overridden 2 | property.six=Value Six 3 | -------------------------------------------------------------------------------- /springboot-external-config/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | property.two=Value Two Overridden 2 | property.three=Value Three -------------------------------------------------------------------------------- /springboot-external-config/src/main/resources/application-local.properties: -------------------------------------------------------------------------------- 1 | property.four=Value Four Overridden 2 | property.five=Value Five 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.ipr 3 | *.iws 4 | .idea/ 5 | target/ 6 | pom.xml.tag 7 | pom.xml.releaseBackup 8 | pom.xml.versionsBackup 9 | pom.xml.next 10 | release.properties 11 | -------------------------------------------------------------------------------- /springboot-multidatasource/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | *.ipr 3 | *.iws 4 | .idea/ 5 | target/ 6 | pom.xml.tag 7 | pom.xml.releaseBackup 8 | pom.xml.next 9 | release.properties 10 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/app.properties: -------------------------------------------------------------------------------- 1 | property.one=Value One Overridden from app.properties 2 | property.two=Value Two Overridden from app.properties 3 | property.three=Value Three from app.properties -------------------------------------------------------------------------------- /springboot-logging-log4j/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.output.ansi.enabled=always 2 | logging.level.org.springframework=info 3 | 4 | logging.path=/home/zhaojh 5 | #logging.file=/home/zhaojh/app.log -------------------------------------------------------------------------------- /springboot-config-yaml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | property: 2 | one: 'Value One' 3 | connection: 4 | username: 'admin' 5 | remoteAddress: '192.152.1.20' 6 | foo: 7 | primitive: 100 8 | servers: 9 | - dev.bar.com 10 | - foo.bar.com -------------------------------------------------------------------------------- /springboot-logging/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #logging.path和logging.file同时存在不起作用 2 | #logging.path: 只用来更改日志文件输出位置 3 | #logging.file: 既可以改变日志文件名称,也可以改变输出位置 4 | #logging.path=/home/zhaojh 5 | #logging.file=/home/zhaojh/app.log 6 | 7 | logging.level.org.springframework=info 8 | 9 | 10 | -------------------------------------------------------------------------------- /springboot-external-config/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | springboot-external-config: 4 | image: springboot-external-config 5 | environment: 6 | - SPRING_APPLICATION_JSON={"property.five":"Value Five Overridden","property.six":"Value Six Overridden","property.seven":"Value Seven"} 7 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test1 2 | spring.datasource.username=root 3 | spring.datasource.password=123 4 | 5 | spring.datasource.names=ds1 6 | spring.datasource.ds1.url=jdbc:mysql://localhost:3306/test2 7 | spring.datasource.ds1.username=root 8 | spring.datasource.ds1.password=123 9 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-external-config/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM java:8u92-oracle 2 | 3 | COPY target/springboot-external-config-1.0-SNAPSHOT.jar /opt/springboot-external-config-1.0-SNAPSHOT.jar 4 | 5 | ADD config/ /opt/config/ 6 | 7 | WORKDIR /opt 8 | 9 | ENV SPRING_APPLICATION_JSON='{"spring.profiles.active":"local","property.five":"Value Five","property.six":"Value Six","property.seven":"Value Seven"}' 10 | 11 | CMD ["java","-jar","springboot-external-config-1.0-SNAPSHOT.jar"] -------------------------------------------------------------------------------- /springboot-mvc-converter/src/main/java/com/github/loafer/demo/springboot/WebApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author zhaojh. 8 | */ 9 | @SpringBootApplication 10 | public class WebApplication { 11 | 12 | public static void main(String[] args){ 13 | SpringApplication.run(WebApplication.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-logging/src/main/java/com/github/loafer/demo/springboot/LoggingApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author zhaojh. 8 | */ 9 | @SpringBootApplication 10 | public class LoggingApplication { 11 | 12 | public static void main(String[] args){ 13 | SpringApplication.run(LoggingApplication.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/github/loafer/demo/springboot/AppPropertisApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author zhaojh. 8 | */ 9 | @SpringBootApplication 10 | public class AppPropertisApplication { 11 | 12 | public static void main(String[] args){ 13 | SpringApplication.run(AppPropertisApplication.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/datasource/annotation/TargetDataSource.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.datasource.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by zhaojh. 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.METHOD) 13 | public @interface TargetDataSource { 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /springboot-logging-log4j/src/main/resources/log4j-spring.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=info,stdout,R 2 | 3 | LOG_PATH=${java.io.tmpdir} 4 | LOG_FILE=${LOG_PATH}/spring.log 5 | LOG_LEVEL_PATTERN=%5p 6 | LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} ${LOG_LEVEL_PATTERN} [%t] --- %c{1}: %m%n 7 | 8 | 9 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 10 | log4j.appender.stdout.Target=System.out 11 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.stdout.layout.ConversionPattern=${LOG_PATTERN} 13 | 14 | 15 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/datasource/DataSourceContext.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.datasource; 2 | 3 | /** 4 | * Created by zhaojh. 5 | */ 6 | public class DataSourceContext { 7 | private static final ThreadLocal contextHolder = new ThreadLocal<>(); 8 | 9 | public static void setDatasourceName(String datasourceName){ 10 | contextHolder.set(datasourceName); 11 | } 12 | 13 | public static String getDatasourceName(){ 14 | return contextHolder.get(); 15 | } 16 | 17 | public static void clean(){ 18 | contextHolder.remove(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-mvc-converter/src/main/java/com/github/loafer/demo/springboot/WebConfigAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.http.converter.HttpMessageConverter; 4 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author zhaojh. 10 | */ 11 | public class WebConfigAdapter extends WebMvcConfigurerAdapter { 12 | 13 | @Override 14 | public void configureMessageConverters(List> converters) { 15 | super.configureMessageConverters(converters); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /without-starter-parent/src/main/java/com/github/loafer/demo/springboot/hello/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.hello.web; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | /** 9 | * @author zhaojh. 10 | */ 11 | @Controller 12 | @RequestMapping("hello") 13 | public class HelloController { 14 | 15 | @RequestMapping("greeting") 16 | @ResponseBody 17 | public String greeting(@RequestParam(value = "name", defaultValue = "Spring Boot") String name){ 18 | return "Hello, " + name + '!'; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /with-starter-parent/src/main/java/com/github/loafer/demo/springboot/hello/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.hello.web; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | /** 9 | * @author zhaojh. 10 | */ 11 | @Controller 12 | @RequestMapping("/hello") 13 | public class HelloController { 14 | 15 | @RequestMapping(value = "greeting") 16 | @ResponseBody 17 | public String greeting(@RequestParam(value = "name", defaultValue = "Spring Boot") String name){ 18 | return "Hello, " + name + '!'; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-logging-log4j/src/main/java/com/github/loafer/demo/springboot/Log4jApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | /** 9 | * @author zhaojh. 10 | */ 11 | @SpringBootApplication 12 | public class Log4jApplication { 13 | private static final Logger logger = LoggerFactory.getLogger(Log4jApplication.class); 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | SpringApplication.run(Log4jApplication.class, args); 17 | 18 | // for (int i=0; i<20; i++){ 19 | // logger.info("{}", i); 20 | // 21 | // Thread.sleep(2000); 22 | // } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-config-yaml/src/main/java/com/github/loafer/demo/springboot/FooComponent.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author zhaojh. 7 | */ 8 | public class FooComponent { 9 | private Integer primitive; 10 | private List servers; 11 | 12 | public Integer getPrimitive() { 13 | return primitive; 14 | } 15 | 16 | public List getServers() { 17 | return servers; 18 | } 19 | 20 | public void setPrimitive(Integer primitive) { 21 | this.primitive = primitive; 22 | } 23 | 24 | public void setServers(List servers) { 25 | this.servers = servers; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return "{" + 31 | "primitive=" + primitive + 32 | ", servers=" + servers + 33 | '}'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /demo-springboot-web/src/main/java/com/github/loafer/demo/springboot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.controller; 2 | 3 | import com.github.loafer.demo.springboot.web.ResponseData; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.ApplicationArguments; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | /** 14 | * @author zhaojh. 15 | */ 16 | @RestController 17 | @RequestMapping("/hello") 18 | public class HelloController { 19 | private static final Logger logger = LoggerFactory.getLogger(HelloController.class); 20 | 21 | @RequestMapping(method = RequestMethod.GET) 22 | public ResponseData sayHello(){ 23 | return new ResponseData().success("Hello Spring Boot!"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/datasource/DataSourceAspect.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.datasource; 2 | 3 | import com.github.loafer.demo.datasource.annotation.TargetDataSource; 4 | import org.aspectj.lang.ProceedingJoinPoint; 5 | import org.aspectj.lang.annotation.Around; 6 | import org.aspectj.lang.annotation.Aspect; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | * Created by zhaojh. 11 | */ 12 | @Component 13 | @Aspect 14 | public class DataSourceAspect { 15 | 16 | @Around("@annotation(targetDataSource)") 17 | public Object changeDatasource(ProceedingJoinPoint pjp, TargetDataSource targetDataSource) throws Throwable { 18 | 19 | try { 20 | DataSourceContext.setDatasourceName(targetDataSource.value()); 21 | Object rtnValue = pjp.proceed(); 22 | DataSourceContext.clean(); 23 | return rtnValue; 24 | } catch (Throwable throwable) { 25 | DataSourceContext.clean(); 26 | throw throwable; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /with-starter-parent/src/main/java/com/github/loafer/demo/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | /** 12 | * @author zhaojh. 13 | */ 14 | @Configuration 15 | @EnableAutoConfiguration 16 | @ComponentScan 17 | public class Application implements CommandLineRunner{ 18 | private final Logger logger = LoggerFactory.getLogger(Application.class); 19 | 20 | @Override 21 | public void run(String... strings) throws Exception { 22 | logger.info("服务已启动."); 23 | } 24 | 25 | public static void main(String[] args){ 26 | SpringApplication.run(Application.class); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /without-starter-parent/src/main/java/com/github/loafer/demo/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | /** 12 | * @author zhaojh. 13 | */ 14 | @Configuration 15 | @EnableAutoConfiguration 16 | @ComponentScan 17 | public class Application implements CommandLineRunner { 18 | private final Logger logger = LoggerFactory.getLogger(Application.class); 19 | 20 | @Override 21 | public void run(String... strings) throws Exception { 22 | logger.info("服务已启动."); 23 | } 24 | 25 | public static void main(String[] args){ 26 | SpringApplication.run(Application.class); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /with-starter-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.github.loafer 9 | with-starter-parent 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 1.7 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 1.3.6.RELEASE 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.entity; 2 | 3 | public class User { 4 | 5 | public User() {} 6 | 7 | public User(Long id, String name, Integer age) { 8 | this.id = id; 9 | this.name = name; 10 | this.age = age; 11 | } 12 | 13 | private Long id; 14 | 15 | private String name; 16 | 17 | private Integer age; 18 | 19 | public Long getId() { 20 | return id; 21 | } 22 | 23 | public void setId(Long id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public Integer getAge() { 36 | return age; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", age=" + age + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /springboot-config-yaml/src/main/java/com/github/loafer/demo/springboot/ConfigComponent.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.beans.factory.config.BeanPostProcessor; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.PostConstruct; 11 | 12 | @Component 13 | public class ConfigComponent{ 14 | private static final Logger logger = LoggerFactory.getLogger(ConfigComponent.class); 15 | 16 | @Value("${property.one}") 17 | private String propertyOne; 18 | 19 | @Autowired 20 | private ConnectionSettings connectionSettings; 21 | 22 | @Autowired 23 | private FooComponent fooComponent; 24 | 25 | @PostConstruct 26 | public void postConstruct(){ 27 | logger.info("Property One: {}", propertyOne); 28 | logger.info("Connection Settings: {}", connectionSettings); 29 | logger.info("fooComponent: {}", fooComponent); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/com/github/loafer/demo/springboot/ConfigComponent.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | /** 11 | * @author zhaojh. 12 | */ 13 | @Component 14 | public class ConfigComponent { 15 | private static final Logger logger = LoggerFactory.getLogger(ConfigComponent.class); 16 | 17 | @Value("${property.one}") 18 | private String propertyOne; 19 | 20 | @Value("${property.two}") 21 | private String propertyTwo; 22 | 23 | @Value("${property.three}") 24 | private String propertyThree; 25 | 26 | @Value("${property.four}") 27 | private String propertyFour; 28 | 29 | @PostConstruct 30 | public void PostConstruct(){ 31 | logger.info("Property One: {}", propertyOne); 32 | logger.info("Property Two: {}", propertyTwo); 33 | logger.info("Property Three: {}", propertyThree); 34 | logger.info("Property Four: {}", propertyFour); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-mvc-converter/src/main/java/com/github/loafer/demo/springboot/WebConfigSupport.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.format.FormatterRegistry; 5 | import org.springframework.format.datetime.DateFormatter; 6 | import org.springframework.http.converter.HttpMessageConverter; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 8 | 9 | import java.util.List; 10 | 11 | 12 | /** 13 | * @author zhaojh. 14 | */ 15 | @Configuration 16 | public class WebConfigSupport extends WebMvcConfigurationSupport { 17 | 18 | 19 | /** 20 | * 全局日期格式化,设置后@DateFormatter的pattern、iso等属性不在起作用 21 | * @param registry 22 | */ 23 | @Override 24 | protected void addFormatters(FormatterRegistry registry) { 25 | registry.addFormatter(new DateFormatter("yyyy-MM-dd")); 26 | super.addFormatters(registry); 27 | } 28 | 29 | @Override 30 | protected void configureMessageConverters(List> converters) { 31 | super.configureMessageConverters(converters); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /without-starter-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer 8 | without-starter-parent 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-web 15 | 16 | 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-dependencies 23 | 1.3.6.RELEASE 24 | pom 25 | import 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /springboot-config-yaml/src/main/java/com/github/loafer/demo/springboot/ConnectionSettings.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | 4 | 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.validation.constraints.NotNull; 9 | 10 | @Component 11 | @ConfigurationProperties(prefix = "connection") 12 | public class ConnectionSettings { 13 | private String username; 14 | 15 | @NotNull 16 | //通过注释掉application.yml中的remoteAddress属性来验证@NotNull注解是否生效 17 | private String remoteAddress; 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public void setUsername(String username) { 24 | this.username = username; 25 | } 26 | 27 | public String getRemoteAddress() { 28 | return remoteAddress; 29 | } 30 | 31 | public void setRemoteAddress(String remoteAddress) { 32 | this.remoteAddress = remoteAddress; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "{" + 38 | "username='" + username + '\'' + 39 | ", remoteAddress='" + remoteAddress + '\'' + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springboot-config-yaml/src/main/java/com/github/loafer/demo/springboot/YamlConfApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 8 | 9 | import javax.validation.Validator; 10 | 11 | /** 12 | * @author zhaojh. 13 | */ 14 | @SpringBootApplication 15 | public class YamlConfApplication { 16 | 17 | @ConfigurationProperties("foo") 18 | @Bean 19 | public FooComponent fooComponent(){ 20 | return new FooComponent(); 21 | } 22 | 23 | // @Bean 24 | // public Validator localValidatorFactoryBean(){ 25 | // LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); 26 | // factoryBean.setProviderClass(org.hibernate.validator.HibernateValidator.class); 27 | // return factoryBean; 28 | // } 29 | 30 | public static void main(String[] args){ 31 | SpringApplication.run(YamlConfApplication.class, args); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot-external-config/src/main/java/com/github/loafer/demo/springboot/config/ExternalConfigApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.config; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | /** 15 | * @author zhaojh. 16 | */ 17 | @SpringBootApplication 18 | public class ExternalConfigApplication { 19 | private static final Logger logger = LoggerFactory.getLogger(ExternalConfigApplication.class); 20 | 21 | @Autowired 22 | private ExternalConfigComponent externalConfigComponent; 23 | 24 | public static void main(String[] args){ 25 | SpringApplication app = new SpringApplication(ExternalConfigApplication.class); 26 | 27 | Map defaultProperties = new HashMap(); 28 | defaultProperties.put("property.one","Value One"); 29 | defaultProperties.put("property.two", "Value Two"); 30 | 31 | app.setDefaultProperties(defaultProperties); 32 | app.run(args); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /demo-springboot-web/src/main/java/com/github/loafer/demo/springboot/WebApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.ApplicationArguments; 7 | import org.springframework.boot.ApplicationRunner; 8 | import org.springframework.boot.CommandLineRunner; 9 | import org.springframework.boot.SpringApplication; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | @SpringBootApplication 16 | public class WebApplication implements CommandLineRunner{ 17 | private static final Logger logger = LoggerFactory.getLogger(WebApplication.class); 18 | 19 | //注入此bean可以获得应用启动时传入的参数 20 | @Autowired 21 | private ApplicationArguments arguments; 22 | 23 | @Override 24 | public void run(String... strings) throws Exception { 25 | //pring arguments 26 | logger.info("{}", arguments.getOptionNames()); 27 | } 28 | 29 | public static void main(String[] args) { 30 | logger.info("=====befor====="); 31 | SpringApplication.run(WebApplication.class, args); 32 | logger.info("=====after====="); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /springboot-logging/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer.demo.springboot 8 | springboot-logging 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.3.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.7 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /springboot-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.github.loafer.demo.springboot 9 | springboot-config 10 | 1.0-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 1.3.6.RELEASE 16 | 17 | 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.7 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-maven-plugin 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /springboot-external-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer.demo.springboot 8 | springboot-external-config 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.3.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.7 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /springboot-external-config/src/main/java/com/github/loafer/demo/springboot/config/ExternalConfigComponent.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.config; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Component; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | /** 11 | * @author zhaojh. 12 | */ 13 | @Component 14 | public class ExternalConfigComponent { 15 | private static final Logger logger = LoggerFactory.getLogger(ExternalConfigComponent.class); 16 | 17 | @Value("${property.one}") 18 | private String propertyOne; 19 | 20 | @Value("${property.two}") 21 | private String propertyTwo; 22 | 23 | @Value("${property.three}") 24 | private String propertyThree; 25 | 26 | @Value("${property.four}") 27 | private String propertyFour; 28 | 29 | @Value("${property.five}") 30 | private String propertyFive; 31 | 32 | @Value("${property.six}") 33 | private String propertySix; 34 | 35 | @Value("${property.seven}") 36 | private String propertySeven; 37 | 38 | 39 | @PostConstruct 40 | public void postConstruct(){ 41 | logger.info("Property One: {}", propertyOne); 42 | logger.info("Property Two: {}", propertyTwo); 43 | logger.info("Property Three: {}", propertyThree); 44 | logger.info("Property Four: {}", propertyFour); 45 | logger.info("Property Five: {}", propertyFive); 46 | logger.info("Property Six: {}", propertySix); 47 | logger.info("Property Seven: {}", propertySeven); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /springboot-mvc-converter/src/main/java/com/github/loafer/demo/springboot/convert/ConvertController.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.convert; 2 | 3 | import org.springframework.format.annotation.DateTimeFormat; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.Collection; 10 | import java.util.Date; 11 | 12 | /** 13 | * @author zhaojh. 14 | */ 15 | @RestController 16 | @RequestMapping("/convert") 17 | public class ConvertController { 18 | 19 | @RequestMapping("primitive") 20 | public String primitive(Integer value){ 21 | return "Converted primitive " + value; 22 | } 23 | 24 | @RequestMapping("date/{value}") 25 | public String date(@PathVariable @DateTimeFormat Date value){ 26 | return "Converted date " + value; 27 | } 28 | 29 | @RequestMapping("date") 30 | public String date2(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date value){ 31 | return "Converted date " + value; 32 | } 33 | 34 | @RequestMapping("collection") 35 | public String collection(@RequestParam Collection values){ 36 | return "Converted collection " + values; 37 | } 38 | 39 | @RequestMapping("dateCollection") 40 | public String dateCollection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Collection values){ 41 | return "Converted date collection " + values; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /demo-springboot-web/src/test/java/com/github/loafer/demo/springboot/WebApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot; 2 | 3 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 4 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 5 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.SpringApplicationConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import org.springframework.test.context.web.WebAppConfiguration; 14 | import org.springframework.test.web.servlet.MockMvc; 15 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 16 | import org.springframework.web.context.WebApplicationContext; 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @SpringApplicationConfiguration(classes = WebApplication.class) 20 | @WebAppConfiguration 21 | public class WebApplicationTests { 22 | @Autowired 23 | private WebApplicationContext context; 24 | private MockMvc mockMvc; 25 | 26 | @Before 27 | public void setup(){ 28 | mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); 29 | } 30 | 31 | @Test 32 | public void hello() throws Exception { 33 | mockMvc.perform(get("/hello")) 34 | // .andDo(print()) 35 | .andExpect(status().isOk()) 36 | .andExpect(jsonPath("$.meta.message").value("ok")) 37 | .andExpect(jsonPath("$.data").value("Hello Spring Boot!")); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.service; 2 | 3 | import com.github.loafer.demo.datasource.annotation.TargetDataSource; 4 | import com.github.loafer.demo.entity.User; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.jdbc.core.JdbcTemplate; 7 | import org.springframework.jdbc.core.RowMapper; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.sql.ResultSet; 11 | import java.sql.SQLException; 12 | import java.util.List; 13 | 14 | 15 | @Service 16 | public class UserService { 17 | @Autowired 18 | private JdbcTemplate jdbcTemplate; 19 | 20 | public void save(User user){ 21 | jdbcTemplate.update("insert into users(id, name, age)values(?,?,?)", new Object[]{user.getId(), user.getName(), user.getAge()}); 22 | } 23 | 24 | public List findAll(){ 25 | return jdbcTemplate.query("select * from users", new RowMapper() { 26 | @Override 27 | public User mapRow(ResultSet rs, int rowNum) throws SQLException { 28 | User user = new User(); 29 | user.setId(rs.getLong("id")); 30 | user.setName(rs.getString("name")); 31 | user.setAge(rs.getInt("age")); 32 | return user; 33 | } 34 | }); 35 | } 36 | 37 | public void clean(){ 38 | jdbcTemplate.update("truncate table users "); 39 | } 40 | 41 | @TargetDataSource("ds1") 42 | public void save2(User user){ 43 | save(user); 44 | } 45 | 46 | @TargetDataSource("ds1") 47 | public List findAll2(){ 48 | return findAll(); 49 | } 50 | 51 | @TargetDataSource("ds1") 52 | public void clean2(){ 53 | clean(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /springboot-mvc-converter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer.demo.springboot 8 | springboot-mvc-converter 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.3.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.7 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /demo-springboot-web/src/main/java/com/github/loafer/demo/springboot/web/ResponseData.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.web; 2 | 3 | /** 4 | * @author zhaojh. 5 | */ 6 | public class ResponseData { 7 | private static final String OK = "ok"; 8 | private static final String ERROR = "error"; 9 | 10 | private Meta meta; 11 | private Object data; 12 | 13 | public ResponseData success(){ 14 | this.success(null); 15 | return this; 16 | } 17 | 18 | public ResponseData success(Object data){ 19 | this.meta = new Meta(true, OK); 20 | this.data = data; 21 | return this; 22 | } 23 | 24 | public ResponseData failure(){ 25 | this.failure(ERROR); 26 | return this; 27 | } 28 | 29 | public ResponseData failure(String message){ 30 | this.meta = new Meta(false, message); 31 | return this; 32 | } 33 | 34 | public Meta getMeta() { 35 | return meta; 36 | } 37 | 38 | public Object getData() { 39 | return data; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "ResponseData{" + 45 | "meta=" + meta + 46 | ", data=" + data + 47 | '}'; 48 | } 49 | 50 | public static class Meta { 51 | 52 | private boolean success; 53 | private String message; 54 | 55 | public Meta(boolean success, String message) { 56 | this.success = success; 57 | this.message = message; 58 | } 59 | 60 | public boolean isSuccess() { 61 | return success; 62 | } 63 | 64 | public String getMessage() { 65 | return message; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "Meta{" + 71 | "success=" + success + 72 | ", message='" + message + '\'' + 73 | '}'; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /demo-springboot-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.github.loafer.demo.springboot 9 | demo-springboot-web 10 | 0.0.1-SNAPSHOT 11 | jar 12 | 13 | demo-springboot-web 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 1.3.6.RELEASE 19 | 20 | 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 1.7 26 | 2.2.0 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | com.jayway.jsonpath 43 | json-path 44 | ${jsonpath.version} 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/datasource/DatasourceProxyBeanProcessor.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.datasource; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | import org.springframework.context.EnvironmentAware; 6 | import org.springframework.core.env.Environment; 7 | import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.sql.DataSource; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | /** 15 | * Created by zhaojh. 16 | */ 17 | @Component 18 | public class DatasourceProxyBeanProcessor implements BeanPostProcessor, EnvironmentAware { 19 | private Environment environment; 20 | 21 | @Override 22 | public void setEnvironment(Environment environment) { 23 | this.environment = environment; 24 | } 25 | 26 | @Override 27 | public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { 28 | return bean; 29 | } 30 | 31 | @Override 32 | public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { 33 | if(bean instanceof DataSource){ 34 | DataSource dataSourceBean = (DataSource) bean; 35 | AbstractRoutingDataSource routingDataSource = new RoutingDataSource(); 36 | routingDataSource.setDefaultTargetDataSource(dataSourceBean); 37 | ((RoutingDataSource)routingDataSource).setEnvironment(this.environment); 38 | 39 | Map targetDataSources = new HashMap<>(); 40 | targetDataSources.put("master", dataSourceBean); 41 | routingDataSource.setTargetDataSources(targetDataSources); 42 | routingDataSource.afterPropertiesSet(); 43 | return routingDataSource; 44 | } 45 | return bean; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /springboot-config-yaml/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer.demo.springboot 8 | springboot-config-yaml 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.3.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.7 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | 31 | 32 | org.hibernate 33 | hibernate-validator 34 | 5.1.3.Final 35 | 36 | 37 | 38 | javax.el 39 | javax.el-api 40 | 2.2.4 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /springboot-logging-log4j/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.loafer.demo.springboot 8 | springboot-logging-log4j 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.3.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.7 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-logging 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-log4j 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/test/java/com/github/loafer/demo/ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo; 2 | 3 | import com.github.loafer.demo.datasource.DataSourceContext; 4 | import com.github.loafer.demo.entity.User; 5 | import com.github.loafer.demo.service.UserService; 6 | import org.junit.Assert; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | /** 17 | * Created by zhaojh. 18 | */ 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest 21 | public class ApplicationTests { 22 | private static final Logger logger = LoggerFactory.getLogger(ApplicationTests.class); 23 | 24 | @Autowired 25 | private UserService userService; 26 | 27 | @Before 28 | public void setup(){ 29 | userService.clean(); 30 | userService.clean2(); 31 | } 32 | 33 | @Test 34 | public void contextLoads() { 35 | 36 | } 37 | 38 | @Test 39 | public void userServiceTest(){ 40 | userService.save(new User(1L, "aaa", 20)); 41 | logger.info("{}", userService.findAll()); 42 | } 43 | 44 | @Test 45 | public void changeDatasourceTest(){ 46 | userService.save(new User(1L, "aaa", 20)); 47 | 48 | DataSourceContext.setDatasourceName("ds1"); 49 | userService.save(new User(2L, "bbb", 26)); 50 | DataSourceContext.clean(); 51 | 52 | Assert.assertEquals(1, userService.findAll().size()); 53 | 54 | DataSourceContext.setDatasourceName("ds1"); 55 | Assert.assertEquals(1, userService.findAll().size()); 56 | DataSourceContext.clean(); 57 | } 58 | 59 | @Test 60 | public void datasourceAspectTest(){ 61 | userService.save(new User(1L, "aaa", 20)); 62 | userService.save2(new User(2L, "bbb", 26)); 63 | 64 | Assert.assertEquals(1, userService.findAll().size()); 65 | Assert.assertEquals(1, userService.findAll2().size()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /springboot-multidatasource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.github.loafer 7 | springboot-multidatasource 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | springboot-multidatasource 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-jdbc 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-aop 39 | 40 | 41 | 42 | mysql 43 | mysql-connector-java 44 | runtime 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /springboot-mvc-converter/src/test/java/com/github/loafer/demo/springboot/convert/ConvertControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.springboot.convert; 2 | 3 | import com.github.loafer.demo.springboot.WebApplication; 4 | 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import org.springframework.test.context.web.WebAppConfiguration; 14 | import org.springframework.test.web.servlet.MockMvc; 15 | import org.springframework.test.web.servlet.result.MockMvcResultHandlers; 16 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 17 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 18 | import org.springframework.web.context.WebApplicationContext; 19 | 20 | /** 21 | * @author zhaojh. 22 | */ 23 | @RunWith(SpringJUnit4ClassRunner.class) 24 | @ContextConfiguration(classes = WebApplication.class) 25 | @WebAppConfiguration 26 | public class ConvertControllerTest { 27 | private MockMvc mockMvc; 28 | 29 | @Autowired 30 | private WebApplicationContext context; 31 | 32 | @Before 33 | public void setup(){ 34 | mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); 35 | } 36 | 37 | @Test 38 | public void primitive() throws Exception { 39 | mockMvc.perform(get("/convert/primitive").param("value", "3")) 40 | .andExpect(MockMvcResultMatchers.content().string("Converted primitive 3")) 41 | .andDo(MockMvcResultHandlers.print()); 42 | } 43 | 44 | @Test 45 | public void date() throws Exception { 46 | mockMvc.perform(get("/convert/date/2016-07-25")) 47 | // .andExpect(MockMvcResultMatchers.content().string("Converted date Mon Jul 25 08:00:00 CST 2016")) 48 | .andDo(MockMvcResultHandlers.print()); 49 | } 50 | 51 | @Test 52 | public void date2() throws Exception { 53 | mockMvc.perform(get("/convert/date").param("value","2016-07-26 17:22:12")) 54 | // .andExpect(MockMvcResultMatchers.content().string("Converted date Mon Jul 25 08:00:00 CST 2016")) 55 | .andDo(MockMvcResultHandlers.print()); 56 | } 57 | 58 | @Test 59 | public void collection() throws Exception { 60 | mockMvc.perform(get("/convert/collection?values=1&values=2&values=3&values=4")) 61 | .andDo(MockMvcResultHandlers.print()); 62 | } 63 | 64 | @Test 65 | public void collection2() throws Exception { 66 | mockMvc.perform(get("/convert/collection?values=1,2,3,4")) 67 | .andDo(MockMvcResultHandlers.print()); 68 | } 69 | 70 | @Test 71 | public void dateCollection() throws Exception { 72 | mockMvc.perform(get("/convert/dateCollection?values=2016-07-24,2016-07-25")) 73 | .andExpect(MockMvcResultMatchers.content().string("Converted date collection [Sun Jul 24 08:00:00 CST 2016, Mon Jul 25 08:00:00 CST 2016]")) 74 | .andDo(MockMvcResultHandlers.print()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /springboot-multidatasource/src/main/java/com/github/loafer/demo/datasource/RoutingDataSource.java: -------------------------------------------------------------------------------- 1 | package com.github.loafer.demo.datasource; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 6 | import org.springframework.boot.bind.RelaxedPropertyResolver; 7 | import org.springframework.core.env.Environment; 8 | import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 9 | import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; 10 | import org.springframework.util.StringUtils; 11 | 12 | import javax.sql.DataSource; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | 17 | public class RoutingDataSource extends AbstractRoutingDataSource{ 18 | private static final Logger logger = LoggerFactory.getLogger(RoutingDataSource.class); 19 | private static final String DATASOURCE_PROPERTY_PREFIX = "spring.datasource."; 20 | 21 | private Environment environment; 22 | private Map targetDataSources; 23 | 24 | @Override 25 | protected Object determineCurrentLookupKey() { 26 | String dataSourceName = DataSourceContext.getDatasourceName(); 27 | logger.info("datasource [{}].", StringUtils.hasText(dataSourceName)? dataSourceName : "master"); 28 | return dataSourceName; 29 | } 30 | 31 | @Override 32 | public void setTargetDataSources(Map targetDataSources) { 33 | this.targetDataSources = new HashMap<>(targetDataSources); 34 | } 35 | 36 | @Override 37 | public void afterPropertiesSet() { 38 | buildTargetDataSources(); 39 | super.setTargetDataSources(targetDataSources); 40 | super.afterPropertiesSet(); 41 | } 42 | 43 | 44 | public void setEnvironment(Environment environment) { 45 | this.environment = environment; 46 | } 47 | 48 | private void buildTargetDataSources(){ 49 | RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(this.environment, DATASOURCE_PROPERTY_PREFIX); 50 | String targetDatasourceNames = propertyResolver.getProperty("names"); 51 | logger.info("target datasource names: {}", targetDatasourceNames); 52 | 53 | if(!StringUtils.hasText(targetDatasourceNames)){ 54 | return; 55 | } 56 | 57 | 58 | for (String name : targetDatasourceNames.split(",")){ 59 | Map subProperties = propertyResolver.getSubProperties(name + '.'); 60 | logger.info("sub properties: {}", subProperties); 61 | targetDataSources.put(name, buildDatasource(subProperties)); 62 | } 63 | } 64 | 65 | private DataSource buildDatasource(Map properties){ 66 | if(properties.containsKey("jndi-name")){ 67 | return buildJndiDatasource(properties.get("jndi-name").toString()); 68 | }else{ 69 | return buildJdbcDatasource(properties); 70 | } 71 | } 72 | 73 | private DataSource buildJdbcDatasource(Map properties){ 74 | DataSourceBuilder factory = DataSourceBuilder.create() 75 | .url(properties.get("url").toString()) 76 | .username(properties.get("username").toString()) 77 | .password(properties.get("password").toString()); 78 | 79 | return factory.build(); 80 | } 81 | 82 | private DataSource buildJndiDatasource(String datasourceName){ 83 | JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup(); 84 | return jndiDataSourceLookup.getDataSource(datasourceName); 85 | } 86 | } 87 | --------------------------------------------------------------------------------