diff --git a/Chapter 9/autoconfigure-demo/.gitignore b/Chapter 9/autoconfigure-demo/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/autoconfigure-demo/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/autoconfigure-demo/pom.xml b/Chapter 9/autoconfigure-demo/pom.xml new file mode 100644 index 0000000..b630999 --- /dev/null +++ b/Chapter 9/autoconfigure-demo/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.3.RELEASE + + + geektime.spring.hello + autoconfigure-demo + 0.0.1-SNAPSHOT + autoconfigure-demo + Demo project for Spring Boot + + + + org.springframework.boot + spring-boot-starter + + + + + + + + + + + + + + + + + + geektime.spring.hello + geektime-spring-boot-autoconfigure + 0.0.1-SNAPSHOT + + + + geektime.spring.hello + greeting + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/Chapter 9/autoconfigure-demo/src/main/java/geektime/spring/hello/AutoconfigureDemoApplication.java b/Chapter 9/autoconfigure-demo/src/main/java/geektime/spring/hello/AutoconfigureDemoApplication.java new file mode 100644 index 0000000..471ed39 --- /dev/null +++ b/Chapter 9/autoconfigure-demo/src/main/java/geektime/spring/hello/AutoconfigureDemoApplication.java @@ -0,0 +1,13 @@ +package geektime.spring.hello; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AutoconfigureDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(AutoconfigureDemoApplication.class, args); + } + +} diff --git a/Chapter 9/autoconfigure-demo/src/main/resources/application.properties b/Chapter 9/autoconfigure-demo/src/main/resources/application.properties new file mode 100644 index 0000000..b3ec4d7 --- /dev/null +++ b/Chapter 9/autoconfigure-demo/src/main/resources/application.properties @@ -0,0 +1 @@ +greeting.enabled=true \ No newline at end of file diff --git a/Chapter 9/autoconfigure-demo/src/test/java/geektime/spring/hello/AutoconfigureDemoApplicationTests.java b/Chapter 9/autoconfigure-demo/src/test/java/geektime/spring/hello/AutoconfigureDemoApplicationTests.java new file mode 100644 index 0000000..294ad9c --- /dev/null +++ b/Chapter 9/autoconfigure-demo/src/test/java/geektime/spring/hello/AutoconfigureDemoApplicationTests.java @@ -0,0 +1,16 @@ +package geektime.spring.hello; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class AutoconfigureDemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/Chapter 9/geektime-autoconfigure-backport/.gitignore b/Chapter 9/geektime-autoconfigure-backport/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/geektime-autoconfigure-backport/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/geektime-autoconfigure-backport/pom.xml b/Chapter 9/geektime-autoconfigure-backport/pom.xml new file mode 100644 index 0000000..687ecfa --- /dev/null +++ b/Chapter 9/geektime-autoconfigure-backport/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + geektime.spring.hello + geektime-autoconfigure-backport + 0.0.1-SNAPSHOT + geektime-autoconfigure-backport + Demo project for Spring Boot + + + 1.8 + 2.1.3.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + org.springframework + spring-context + + + + org.projectlombok + lombok + + + + geektime.spring.hello + greeting + 0.0.1-SNAPSHOT + provided + + + + diff --git a/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java b/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java new file mode 100644 index 0000000..8d71706 --- /dev/null +++ b/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java @@ -0,0 +1,12 @@ +package geektime.spring.hello.greeting; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GreetingAutoConfiguration { + @Bean + public static GreetingBeanFactoryPostProcessor greetingBeanFactoryPostProcessor() { + return new GreetingBeanFactoryPostProcessor(); + } +} diff --git a/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingBeanFactoryPostProcessor.java b/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingBeanFactoryPostProcessor.java new file mode 100644 index 0000000..512fa05 --- /dev/null +++ b/Chapter 9/geektime-autoconfigure-backport/src/main/java/geektime/spring/hello/greeting/GreetingBeanFactoryPostProcessor.java @@ -0,0 +1,42 @@ +package geektime.spring.hello.greeting; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.GenericBeanDefinition; +import org.springframework.util.ClassUtils; + +@Slf4j +public class GreetingBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + boolean hasClass = ClassUtils.isPresent("geektime.spring.hello.greeting.GreetingApplicationRunner", + GreetingBeanFactoryPostProcessor.class.getClassLoader()); + if (!hasClass) { + log.info("GreetingApplicationRunner is NOT present in CLASSPATH."); + return; + } + + if (beanFactory.containsBeanDefinition("greetingApplicationRunner")) { + log.info("We already have a greetingApplicationRunner bean registered."); + return; + } + + register(beanFactory); + } + + private void register(ConfigurableListableBeanFactory beanFactory) { + if (beanFactory instanceof BeanDefinitionRegistry) { + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); + beanDefinition.setBeanClass(GreetingApplicationRunner.class); + + ((BeanDefinitionRegistry) beanFactory) + .registerBeanDefinition("greetingApplicationRunner", + beanDefinition); + } else { + beanFactory.registerSingleton("greetingApplicationRunner", + new GreetingApplicationRunner()); + } + } +} diff --git a/Chapter 9/geektime-autoconfigure-backport/src/main/resources/application.properties b/Chapter 9/geektime-autoconfigure-backport/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Chapter 9/geektime-autoconfigure-backport/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/Chapter 9/geektime-spring-boot-autoconfigure/.gitignore b/Chapter 9/geektime-spring-boot-autoconfigure/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-autoconfigure/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/geektime-spring-boot-autoconfigure/pom.xml b/Chapter 9/geektime-spring-boot-autoconfigure/pom.xml new file mode 100644 index 0000000..4255c93 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-autoconfigure/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + geektime.spring.hello + geektime-spring-boot-autoconfigure + 0.0.1-SNAPSHOT + geektime-spring-boot-autoconfigure + Demo project for Spring Boot + + + 1.8 + 2.1.3.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-autoconfigure + + + + geektime.spring.hello + greeting + 0.0.1-SNAPSHOT + provided + + + + diff --git a/Chapter 9/geektime-spring-boot-autoconfigure/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java new file mode 100644 index 0000000..d6577d1 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/java/geektime/spring/hello/greeting/GreetingAutoConfiguration.java @@ -0,0 +1,18 @@ +package geektime.spring.hello.greeting; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnClass(GreetingApplicationRunner.class) +public class GreetingAutoConfiguration { + @Bean + @ConditionalOnMissingBean(GreetingApplicationRunner.class) + @ConditionalOnProperty(name = "greeting.enabled", havingValue = "true", matchIfMissing = true) + public GreetingApplicationRunner greetingApplicationRunner() { + return new GreetingApplicationRunner(); + } +} diff --git a/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..3b51475 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +geektime.spring.hello.greeting.GreetingAutoConfiguration \ No newline at end of file diff --git a/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/application.properties b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-autoconfigure/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/Chapter 9/geektime-spring-boot-starter/.gitignore b/Chapter 9/geektime-spring-boot-starter/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-starter/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/geektime-spring-boot-starter/pom.xml b/Chapter 9/geektime-spring-boot-starter/pom.xml new file mode 100644 index 0000000..9ec21f1 --- /dev/null +++ b/Chapter 9/geektime-spring-boot-starter/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + geektime.spring.hello + geektime-spring-boot-starter + 0.0.1-SNAPSHOT + geektime-spring-boot-starter + Demo project for Spring Boot + + + + geektime.spring.hello + geektime-spring-boot-autoconfigure + 0.0.1-SNAPSHOT + + + geektime.spring.hello + greeting + 0.0.1-SNAPSHOT + + + + diff --git a/Chapter 9/greeting/.gitignore b/Chapter 9/greeting/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/greeting/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/greeting/pom.xml b/Chapter 9/greeting/pom.xml new file mode 100644 index 0000000..612787d --- /dev/null +++ b/Chapter 9/greeting/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + geektime.spring.hello + greeting + 0.0.1-SNAPSHOT + greeting + Demo project for Spring Boot + + + 1.8 + 2.1.3.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + org.springframework.boot + spring-boot + + + + org.projectlombok + lombok + true + + + + org.slf4j + slf4j-api + + + + diff --git a/Chapter 9/greeting/src/main/java/geektime/spring/hello/greeting/GreetingApplicationRunner.java b/Chapter 9/greeting/src/main/java/geektime/spring/hello/greeting/GreetingApplicationRunner.java new file mode 100644 index 0000000..1afa3b7 --- /dev/null +++ b/Chapter 9/greeting/src/main/java/geektime/spring/hello/greeting/GreetingApplicationRunner.java @@ -0,0 +1,16 @@ +package geektime.spring.hello.greeting; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; + +@Slf4j +public class GreetingApplicationRunner implements ApplicationRunner { + public GreetingApplicationRunner() { + log.info("Initializing GreetingApplicationRunner."); + } + + public void run(ApplicationArguments args) throws Exception { + log.info("Hello everyone! We all like Spring! "); + } +} diff --git a/Chapter 9/greeting/src/main/resources/application.properties b/Chapter 9/greeting/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Chapter 9/greeting/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/Chapter 9/property-source-demo/.gitignore b/Chapter 9/property-source-demo/.gitignore new file mode 100644 index 0000000..4a45303 --- /dev/null +++ b/Chapter 9/property-source-demo/.gitignore @@ -0,0 +1,28 @@ +/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/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/Chapter 9/property-source-demo/pom.xml b/Chapter 9/property-source-demo/pom.xml new file mode 100644 index 0000000..19d92e3 --- /dev/null +++ b/Chapter 9/property-source-demo/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.3.RELEASE + + + geektime.spring.hello + property-source-demo + 0.0.1-SNAPSHOT + property-source-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/PropertySourceDemoApplication.java b/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/PropertySourceDemoApplication.java new file mode 100644 index 0000000..7daab2e --- /dev/null +++ b/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/PropertySourceDemoApplication.java @@ -0,0 +1,24 @@ +package geektime.spring.hello; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@Slf4j +public class PropertySourceDemoApplication implements ApplicationRunner { + @Value("${geektime.greeting}") + private String greeting; + + public static void main(String[] args) { + SpringApplication.run(PropertySourceDemoApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + log.info("{}", greeting); + } +} diff --git a/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/YapfEnvironmentPostProcessor.java b/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/YapfEnvironmentPostProcessor.java new file mode 100644 index 0000000..64ddf29 --- /dev/null +++ b/Chapter 9/property-source-demo/src/main/java/geektime/spring/hello/YapfEnvironmentPostProcessor.java @@ -0,0 +1,28 @@ +package geektime.spring.hello; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.env.PropertiesPropertySourceLoader; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +@Slf4j +public class YapfEnvironmentPostProcessor implements EnvironmentPostProcessor { + private PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader(); + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + MutablePropertySources propertySources = environment.getPropertySources(); + Resource resource = new ClassPathResource("yapf.properties"); + try { + PropertySource ps = loader.load("YetAnotherPropertiesFile", resource) + .get(0); + propertySources.addFirst(ps); + } catch (Exception e) { + log.error("Exception!", e); + } + } +} diff --git a/Chapter 9/property-source-demo/src/main/resources/META-INF/spring.factories b/Chapter 9/property-source-demo/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..43be641 --- /dev/null +++ b/Chapter 9/property-source-demo/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.env.EnvironmentPostProcessor=geektime.spring.hello.YapfEnvironmentPostProcessor \ No newline at end of file diff --git a/Chapter 9/property-source-demo/src/main/resources/application.properties b/Chapter 9/property-source-demo/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Chapter 9/property-source-demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/Chapter 9/property-source-demo/src/main/resources/yapf.properties b/Chapter 9/property-source-demo/src/main/resources/yapf.properties new file mode 100644 index 0000000..ba25e31 --- /dev/null +++ b/Chapter 9/property-source-demo/src/main/resources/yapf.properties @@ -0,0 +1 @@ +geektime.greeting=hello \ No newline at end of file diff --git a/Chapter 9/property-source-demo/src/test/java/geektime/spring/hello/PropertySourceDemoApplicationTests.java b/Chapter 9/property-source-demo/src/test/java/geektime/spring/hello/PropertySourceDemoApplicationTests.java new file mode 100644 index 0000000..7423c37 --- /dev/null +++ b/Chapter 9/property-source-demo/src/test/java/geektime/spring/hello/PropertySourceDemoApplicationTests.java @@ -0,0 +1,16 @@ +package geektime.spring.hello; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PropertySourceDemoApplicationTests { + + @Test + public void contextLoads() { + } + +}