From e314bb7d5c6e45b8841f48b37534c32294e528e0 Mon Sep 17 00:00:00 2001 From: yyl Date: Sun, 11 Aug 2019 23:30:00 +0800 Subject: [PATCH] + autoconfigure example --- spring-boot-example-starter/.gitignore | 25 +++++++++ spring-boot-example-starter/pom.xml | 52 ++++++++++++++++++ .../.gitignore | 25 +++++++++ .../pom.xml | 54 +++++++++++++++++++ .../springboot/StarterSampleApplication.java | 12 +++++ .../src/main/resources/application.properties | 2 + .../StarterSampleApplicationTest.java | 23 ++++++++ .../autoconfigure/BootAutoConfiguration.java | 20 +++++++ .../springboot/component/HelloComponent.java | 7 +++ .../main/resources/META-INF/spring.factories | 2 + 10 files changed, 222 insertions(+) create mode 100644 spring-boot-example-starter/.gitignore create mode 100644 spring-boot-example-starter/pom.xml create mode 100644 spring-boot-example-starter/spring-boot-example-starter-sample/.gitignore create mode 100644 spring-boot-example-starter/spring-boot-example-starter-sample/pom.xml create mode 100644 spring-boot-example-starter/spring-boot-example-starter-sample/src/main/java/yyl/springboot/StarterSampleApplication.java create mode 100644 spring-boot-example-starter/spring-boot-example-starter-sample/src/main/resources/application.properties create mode 100644 spring-boot-example-starter/spring-boot-example-starter-sample/src/test/java/yyl/springboot/StarterSampleApplicationTest.java create mode 100644 spring-boot-example-starter/src/main/java/yyl/springboot/autoconfigure/BootAutoConfiguration.java create mode 100644 spring-boot-example-starter/src/main/java/yyl/springboot/component/HelloComponent.java create mode 100644 spring-boot-example-starter/src/main/resources/META-INF/spring.factories diff --git a/spring-boot-example-starter/.gitignore b/spring-boot-example-starter/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-example-starter/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-example-starter/pom.xml b/spring-boot-example-starter/pom.xml new file mode 100644 index 0000000..bbc1708 --- /dev/null +++ b/spring-boot-example-starter/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + yyl + spring-boot-example-starter + 0.0.1-SNAPSHOT + jar + + spring-boot-example-banner + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + org.springframework.boot + spring-boot-starter + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-example-starter/spring-boot-example-starter-sample/.gitignore b/spring-boot-example-starter/spring-boot-example-starter-sample/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-example-starter/spring-boot-example-starter-sample/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-example-starter/spring-boot-example-starter-sample/pom.xml b/spring-boot-example-starter/spring-boot-example-starter-sample/pom.xml new file mode 100644 index 0000000..ae6b611 --- /dev/null +++ b/spring-boot-example-starter/spring-boot-example-starter-sample/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + yyl + spring-boot-example-starter-sample + 0.0.1-SNAPSHOT + jar + + spring-boot-example-banner + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + + yyl + spring-boot-example-starter + 0.0.1-SNAPSHOT + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/java/yyl/springboot/StarterSampleApplication.java b/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/java/yyl/springboot/StarterSampleApplication.java new file mode 100644 index 0000000..89d90e8 --- /dev/null +++ b/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/java/yyl/springboot/StarterSampleApplication.java @@ -0,0 +1,12 @@ +package yyl.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StarterSampleApplication { + public static void main(String[] args) { + SpringApplication.run(StarterSampleApplication.class, args); + System.out.println("startup success"); + } +} diff --git a/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/resources/application.properties b/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/resources/application.properties new file mode 100644 index 0000000..d5267cc --- /dev/null +++ b/spring-boot-example-starter/spring-boot-example-starter-sample/src/main/resources/application.properties @@ -0,0 +1,2 @@ +#spring.banner.charset=UTF-8 +#spring.banner.location=classpath:banner.txt \ No newline at end of file diff --git a/spring-boot-example-starter/spring-boot-example-starter-sample/src/test/java/yyl/springboot/StarterSampleApplicationTest.java b/spring-boot-example-starter/spring-boot-example-starter-sample/src/test/java/yyl/springboot/StarterSampleApplicationTest.java new file mode 100644 index 0000000..eb8e057 --- /dev/null +++ b/spring-boot-example-starter/spring-boot-example-starter-sample/src/test/java/yyl/springboot/StarterSampleApplicationTest.java @@ -0,0 +1,23 @@ +package yyl.springboot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import yyl.springboot.component.HelloComponent; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class StarterSampleApplicationTest { + + @Autowired + private HelloComponent helloComponent; + + @Test + public void contextLoads() { + System.out.println("autoconfigure" + helloComponent); + helloComponent.hello(); + } +} diff --git a/spring-boot-example-starter/src/main/java/yyl/springboot/autoconfigure/BootAutoConfiguration.java b/spring-boot-example-starter/src/main/java/yyl/springboot/autoconfigure/BootAutoConfiguration.java new file mode 100644 index 0000000..dfb578f --- /dev/null +++ b/spring-boot-example-starter/src/main/java/yyl/springboot/autoconfigure/BootAutoConfiguration.java @@ -0,0 +1,20 @@ +package yyl.springboot.autoconfigure; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import yyl.springboot.component.HelloComponent; + +/** + * 自动配置类 + */ +@Configuration +public class BootAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public HelloComponent helloComponent() { + return new HelloComponent(); + } +} diff --git a/spring-boot-example-starter/src/main/java/yyl/springboot/component/HelloComponent.java b/spring-boot-example-starter/src/main/java/yyl/springboot/component/HelloComponent.java new file mode 100644 index 0000000..ce03c71 --- /dev/null +++ b/spring-boot-example-starter/src/main/java/yyl/springboot/component/HelloComponent.java @@ -0,0 +1,7 @@ +package yyl.springboot.component; + +public class HelloComponent { + public void hello() { + System.out.println("hello"); + } +} diff --git a/spring-boot-example-starter/src/main/resources/META-INF/spring.factories b/spring-boot-example-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..71111fd --- /dev/null +++ b/spring-boot-example-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +yyl.springboot.autoconfigure.BootAutoConfiguration \ No newline at end of file