From f1a989c537ffb98d5247284c405976b8fb75b751 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Mon, 2 Dec 2019 20:55:13 +0800 Subject: [PATCH] profile --- .../spring/profile/ConditionalOnProfile.java | 19 +++++++++++++++++++ .../spring/profile/ProfileUtils.java | 14 ++++++++++++++ .../spring/profile/PropertyConfiguration.java | 18 ++++++++++++++++++ .../whatsmars/spring/profile/Reader.java | 17 +++++++++++++++++ .../whatsmars/spring/profile/TestSpring.java | 14 ++++++++++++++ .../resources/application-prod.properties | 1 + .../resources/application-test.properties | 1 + 7 files changed, 84 insertions(+) create mode 100644 whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ConditionalOnProfile.java create mode 100644 whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ProfileUtils.java create mode 100644 whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/PropertyConfiguration.java create mode 100644 whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/Reader.java create mode 100644 whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/TestSpring.java create mode 100644 whatsmars-spring/src/main/resources/application-prod.properties create mode 100644 whatsmars-spring/src/main/resources/application-test.properties diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ConditionalOnProfile.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ConditionalOnProfile.java new file mode 100644 index 00000000..f0b4b166 --- /dev/null +++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ConditionalOnProfile.java @@ -0,0 +1,19 @@ +package org.hongxi.whatsmars.spring.profile; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile("prod") +@Component +public class ConditionalOnProfile implements InitializingBean { + + @Value("${profile}") + private String profile; + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println(profile); + } +} diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ProfileUtils.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ProfileUtils.java new file mode 100644 index 00000000..6f825de0 --- /dev/null +++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/ProfileUtils.java @@ -0,0 +1,14 @@ +package org.hongxi.whatsmars.spring.profile; + +import org.springframework.util.StringUtils; + +public class ProfileUtils { + + private static final String PROFILE_ENV_NAME = "PROFILE"; + private static final String DEFAULT_PROFILE = "test"; + + public static String getProfile() { + String profile = System.getProperty(PROFILE_ENV_NAME); + return StringUtils.isEmpty(profile) ? DEFAULT_PROFILE : profile; + } +} diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/PropertyConfiguration.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/PropertyConfiguration.java new file mode 100644 index 00000000..f06f63e4 --- /dev/null +++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/PropertyConfiguration.java @@ -0,0 +1,18 @@ +package org.hongxi.whatsmars.spring.profile; + +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +@Configuration +public class PropertyConfiguration { + + @Bean + public PropertyPlaceholderConfigurer configurer() { + PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); + String path = String.format("application-%s.properties", ProfileUtils.getProfile()); + configurer.setLocation(new ClassPathResource(path)); + return configurer; + } +} diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/Reader.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/Reader.java new file mode 100644 index 00000000..a5325a54 --- /dev/null +++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/Reader.java @@ -0,0 +1,17 @@ +package org.hongxi.whatsmars.spring.profile; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Reader implements InitializingBean { + + @Value("${profile}") + private String profile; + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println(profile); + } +} diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/TestSpring.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/TestSpring.java new file mode 100644 index 00000000..cd761f90 --- /dev/null +++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/profile/TestSpring.java @@ -0,0 +1,14 @@ +package org.hongxi.whatsmars.spring.profile; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class TestSpring { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(PropertyConfiguration.class); + context.scan("org.hongxi.whatsmars.spring.profile"); + context.getEnvironment().setActiveProfiles(ProfileUtils.getProfile()); + context.refresh(); + } +} diff --git a/whatsmars-spring/src/main/resources/application-prod.properties b/whatsmars-spring/src/main/resources/application-prod.properties new file mode 100644 index 00000000..345c7b34 --- /dev/null +++ b/whatsmars-spring/src/main/resources/application-prod.properties @@ -0,0 +1 @@ +profile=prod \ No newline at end of file diff --git a/whatsmars-spring/src/main/resources/application-test.properties b/whatsmars-spring/src/main/resources/application-test.properties new file mode 100644 index 00000000..2eb19a44 --- /dev/null +++ b/whatsmars-spring/src/main/resources/application-test.properties @@ -0,0 +1 @@ +profile=test \ No newline at end of file -- GitLab