Skip to content

Commit

Permalink
更新 Spring Boot 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
javastacks committed Aug 14, 2020
1 parent a09e8ae commit 2e9426b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>spring-boot-druid</module>
<module>javastack-spring-boot-starter</module>
<module>javastack-spring-boot-starter-sample</module>
<module>spring-boot-properties</module>
</modules>
<packaging>pom</packaging>

Expand All @@ -31,7 +32,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<version>2.2.0.RELEASE</version>
</parent>

<properties>
Expand Down
33 changes: 33 additions & 0 deletions spring-boot-druid/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-best-practice</artifactId>
<groupId>cn.javastack</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-druid</artifactId>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.javastack.springboot.banner;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
* 微信公众号:Java技术栈
*/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.CONSOLE)
.run(args);
}

@Primary
@Bean
@ConfigurationProperties("spring.datasource.druid.one")
public DataSource dataSourceOne() {
return DruidDataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceTwo() {
return DruidDataSourceBuilder.create().build();
}


}
15 changes: 15 additions & 0 deletions spring-boot-properties/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-best-practice</artifactId>
<groupId>cn.javastack</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-properties</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cn.javastack.springboot.properties;

import cn.javastack.springboot.properties.props.TomProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
* 微信公众号:Java技术栈
*/
@SpringBootApplication
@EnableConfigurationProperties
public class Application {

@Autowired
private TomProperties tomProperties;

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

@Bean
public CommandLineRunner commandLineRunner() {
return (args) -> {
System.out.println(tomProperties);
};
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cn.javastack.springboot.properties.props;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
* 微信公众号:Java技术栈
*/
@ConstructorBinding
@ConfigurationProperties(prefix = "tom")
public class TomProperties {

private String name;
private String sex;
private int age;
private String country;
private Date entryTime;

public TomProperties(String name,
String sex,
int age,
@DefaultValue("China") String country,
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date entryTime) {
this.name = name;
this.sex = sex;
this.age = age;
this.country = country;
this.entryTime = entryTime;
}

public String getName() {
return name;
}

public String getSex() {
return sex;
}

public int getAge() {
return age;
}

public String getCountry() {
return country;
}

public Date getEntryTime() {
return entryTime;
}

@Override
public String toString() {
return "TomProperties{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", country='" + country + '\'' +
", entryTime=" + entryTime +
'}';
}

}
5 changes: 5 additions & 0 deletions spring-boot-properties/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tom:
name: Tom
sex: man
age: 18
entry-time: 2012-12-12 12:00:00

0 comments on commit 2e9426b

Please sign in to comment.