Guest User

Untitled

a guest
Nov 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. <util:properties id="Job1Props"
  2. location="classpath:job1.properties"></util:properties>
  3.  
  4. <util:properties id="Job2Props"
  5. location="classpath:job2.properties"></util:properties>
  6.  
  7. java -jar myapp.jar app.config.location=file:./config
  8.  
  9. <util:properties id="Job2Props"
  10. location="{app.config.location}/job2.properties"></util:properties>
  11.  
  12. //psuedo code
  13.  
  14. if (StringUtils.isBlank(app.config.location)) {
  15. System.setProperty(APP_CONFIG_LOCATION, "classpath:");
  16. }
  17.  
  18. -Dspring.config.location=your/config/dir/
  19.  
  20. -Dspring.config.location=classpath:job1.properties,classpath:job2.properties
  21.  
  22. @Configuration
  23. public class PropertiesConfiguration {
  24.  
  25.  
  26. @Bean
  27. public PropertyPlaceholderConfigurer properties() {
  28. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  29. // ppc.setIgnoreUnresolvablePlaceholders(true);
  30. ppc.setIgnoreResourceNotFound(true);
  31.  
  32. final List<Resource> resourceLst = new ArrayList<Resource>();
  33.  
  34. resourceLst.add(new ClassPathResource("myapp_base.properties"));
  35. resourceLst.add(new FileSystemResource("/etc/myapp/overriding.propertie"));
  36. resourceLst.add(new ClassPathResource("myapp_test.properties"));
  37. resourceLst.add(new ClassPathResource("myapp_developer_overrides.properties")); // for Developer debugging.
  38.  
  39. ppc.setLocations(resourceLst.toArray(new Resource[]{}));
  40.  
  41. return ppc;
  42. }
  43.  
  44. @PropertySource(ignoreResourceNotFound=true,value="classpath:jdbc-${spring.profiles.active}.properties")
  45. public class DBConfig{
  46.  
  47. @Value("${jdbc.host}")
  48. private String jdbcHostName;
  49. }
  50. }
  51.  
  52. java -jar target/myapp.jar --spring.config.location=classpath:file:///C:/Apps/springtest/jdbc.properties,classpath:file:///C:/Apps/springtest/jdbc-dev.properties
  53.  
  54. set spring.profiles.active=dev
  55.  
  56. set spring.profiles.active=default
  57.  
  58. $ chmod 766 application.properties
  59.  
  60. $ chown tomcat application.properties
  61.  
  62. import org.slf4j.Logger;
  63. import org.slf4j.LoggerFactory;
  64. import org.springframework.beans.factory.annotation.Value;
  65. import org.springframework.context.annotation.Bean;
  66. import org.springframework.core.io.ClassPathResource;
  67. import org.springframework.core.io.PathResource;
  68. import org.springframework.core.io.Resource;
  69.  
  70. import java.io.IOException;
  71. import java.util.Properties;
  72.  
  73. import static java.util.Arrays.stream;
  74.  
  75. @Configuration
  76. public class PropertiesConfig {
  77.  
  78. private static final Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);
  79.  
  80. private final static String PROPERTIES_FILENAME = "user.properties";
  81.  
  82. @Value("${properties.location:}")
  83. private String propertiesLocation;
  84.  
  85. @Bean
  86. Properties userProperties() throws IOException {
  87. final Resource[] possiblePropertiesResources = {
  88. new ClassPathResource(PROPERTIES_FILENAME),
  89. new PathResource("config/" + PROPERTIES_FILENAME),
  90. new PathResource(PROPERTIES_FILENAME),
  91. new PathResource(getCustomPath())
  92. };
  93. // Find the last existing properties location to emulate spring boot application.properties discovery
  94. final Resource propertiesResource = stream(possiblePropertiesResources)
  95. .filter(Resource::exists)
  96. .reduce((previous, current) -> current)
  97. .get();
  98. final Properties userProperties = new Properties();
  99.  
  100. userProperties.load(propertiesResource.getInputStream());
  101.  
  102. LOG.info("Using {} as user resource", propertiesResource);
  103.  
  104. return userProperties;
  105. }
  106.  
  107. private String getCustomPath() {
  108. return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + PROPERTIES_FILENAME;
  109. }
  110.  
  111. }
  112.  
  113. import org.slf4j.Logger;
  114. import org.slf4j.LoggerFactory;
  115. import org.springframework.beans.factory.annotation.Value;
  116. import org.springframework.context.annotation.Bean;
  117. import org.springframework.context.annotation.Configuration;
  118. import org.springframework.core.io.ClassPathResource;
  119. import org.springframework.core.io.PathResource;
  120. import org.springframework.core.io.Resource;
  121.  
  122. import java.io.IOException;
  123. import java.util.Map;
  124. import java.util.Properties;
  125.  
  126. import static java.util.Arrays.stream;
  127. import static java.util.stream.Collectors.toMap;
  128.  
  129. @Configuration
  130. class PropertiesConfig {
  131.  
  132. private final static Logger LOG = LoggerFactory.getLogger(PropertiesConfig.class);
  133. private final static String[] PROPERTIES_FILENAMES = {"job1.properties", "job2.properties", "job3.properties"};
  134.  
  135. @Value("${properties.location:}")
  136. private String propertiesLocation;
  137.  
  138. @Bean
  139. Map<String, Properties> myProperties() {
  140. return stream(PROPERTIES_FILENAMES)
  141. .collect(toMap(filename -> filename, this::loadProperties));
  142. }
  143.  
  144. private Properties loadProperties(final String filename) {
  145. final Resource[] possiblePropertiesResources = {
  146. new ClassPathResource(filename),
  147. new PathResource("config/" + filename),
  148. new PathResource(filename),
  149. new PathResource(getCustomPath(filename))
  150. };
  151. final Resource resource = stream(possiblePropertiesResources)
  152. .filter(Resource::exists)
  153. .reduce((previous, current) -> current)
  154. .get();
  155. final Properties properties = new Properties();
  156.  
  157. try {
  158. properties.load(resource.getInputStream());
  159. } catch(final IOException exception) {
  160. throw new RuntimeException(exception);
  161. }
  162.  
  163. LOG.info("Using {} as user resource", resource);
  164.  
  165. return properties;
  166. }
  167.  
  168. private String getCustomPath(final String filename) {
  169. return propertiesLocation.endsWith(".properties") ? propertiesLocation : propertiesLocation + filename;
  170. }
  171.  
  172. }
  173.  
  174. @Configuration
  175. @Profile("one")
  176. @PropertySource("file:/{selected location}/app.properties")
  177. public class TestClass {
  178.  
  179. @Autowired
  180. Environment env;
  181.  
  182. @Bean
  183. public boolean test() {
  184. System.out.println(env.getProperty("test.one"));
  185. return true;
  186. }
  187. }
  188.  
  189. test.one = 1234
  190.  
  191. @SpringBootApplication
  192.  
  193. public class TestApplication {
  194.  
  195. public static void main(String[] args) {
  196. SpringApplication.run(testApplication.class, args);
  197. }
  198. }
  199.  
  200. spring.profiles.active = one
  201.  
  202. @Value("${test.one}")
  203. String str;
  204.  
  205. spring.profiles.active=local
  206.  
  207. spring.data.mongodb.host=localhost
  208. spring.data.mongodb.port=27017
  209. spring.data.mongodb.database=users
  210. spring.data.mongodb.username=humble_freak
  211. spring.data.mongodb.password=freakone
  212.  
  213. spring.rabbitmq.host=localhost
  214. spring.rabbitmq.username=guest
  215. spring.rabbitmq.password=guest
  216. spring.rabbitmq.port=5672
  217.  
  218. rabbitmq.publish=true
  219.  
  220. mvn spring-boot:run -Drun.profiles=local
  221. mvn spring-boot:run -Drun.profiles=qa
  222. mvn spring-boot:run -Drun.profiles=prod
Add Comment
Please, Sign In to add comment