Guest User

Untitled

a guest
Dec 28th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mysql-username' in value "${mysql-username}"
  2.  
  3. package com.test.demo;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.ComponentScan;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.core.io.ClassPathResource;
  14. import org.springframework.core.io.Resource;
  15.  
  16. @Configuration
  17. @ComponentScan
  18. public class DemoApplication {
  19.  
  20. public static void main(String[] args) {
  21. ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApplication.class);
  22. MyController bean = applicationContext.getBean(MyController.class);
  23. bean.log();
  24. }
  25.  
  26. @Bean
  27. public PropertyPlaceholderConfigurer otherProps() {
  28. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  29. ppc.setIgnoreResourceNotFound(true);
  30.  
  31. final List<Resource> resourceList = new ArrayList<>();
  32.  
  33. resourceList.add(getOtherResourceFile());
  34.  
  35. ppc.setLocations(resourceList.toArray(new Resource[]{}));
  36.  
  37. return ppc;
  38. }
  39.  
  40. @Bean
  41. public PropertyPlaceholderConfigurer verticaProps() {
  42. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  43. ppc.setIgnoreResourceNotFound(true);
  44. ppc.setPlaceholderPrefix("${vertica-");
  45.  
  46. final List<Resource> resourceList = new ArrayList<>();
  47.  
  48. resourceList.add(getVerticaResourceFile());
  49.  
  50. ppc.setLocations(resourceList.toArray(new Resource[]{}));
  51.  
  52. return ppc;
  53. }
  54.  
  55. @Bean
  56. public PropertyPlaceholderConfigurer mysqlProps() {
  57. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  58. ppc.setIgnoreResourceNotFound(true);
  59. ppc.setPlaceholderPrefix("${mysql-");
  60. final List<Resource> resourceList = new ArrayList<>();
  61.  
  62. resourceList.add(getDatabasePropertiesFile());
  63.  
  64. ppc.setLocations(resourceList.toArray(new Resource[]{}));
  65.  
  66. return ppc;
  67. }
  68.  
  69. private Resource getOtherResourceFile() {
  70. Resource resource = new ClassPathResource("other.properties");
  71. return resource;
  72. }
  73.  
  74. private Resource getVerticaResourceFile() {
  75. Resource resource = new ClassPathResource("vertica.properties");
  76. return resource;
  77. }
  78.  
  79. private Resource getDatabasePropertiesFile() {
  80. Resource resource = new ClassPathResource("mysql.properties");
  81. return resource;
  82. }
  83. }
  84.  
  85. /*
  86. * MyController.java
  87. */
  88. package com.test.demo;
  89.  
  90. import org.springframework.beans.factory.annotation.Autowired;
  91. import org.springframework.stereotype.Component;
  92.  
  93. /**
  94. *
  95. * @author
  96. */
  97. @Component
  98. public class MyController {
  99.  
  100. @Autowired
  101. private MySqlProperties mySqlProperties;
  102. @Autowired
  103. private VerticaProperties verticaProperties;
  104. @Autowired
  105. private OtherProperties otherProperties;
  106.  
  107. public String log() {
  108. System.out.println(mySqlProperties.getUsername());
  109. System.out.println(verticaProperties.getUsername());
  110. System.out.println(otherProperties.getKey());
  111. return "hello";
  112. }
  113. }
  114.  
  115. /*
  116. * MySqlProperties.java
  117. */
  118. package com.test.demo;
  119.  
  120. import org.springframework.beans.factory.annotation.Value;
  121. import org.springframework.stereotype.Component;
  122.  
  123. /**
  124. *
  125. * @author
  126. */
  127. @Component
  128. public class MySqlProperties {
  129.  
  130. @Value("${mysql-username}")
  131. private String username;
  132. @Value("${mysql-password}")
  133. private String password;
  134.  
  135. //getter
  136. }
  137.  
  138. /*
  139. * VerticaProperties.java
  140. */
  141. package com.test.demo;
  142.  
  143. import org.springframework.beans.factory.annotation.Value;
  144. import org.springframework.stereotype.Component;
  145.  
  146. /**
  147. *
  148. * @author
  149. */
  150. @Component
  151. public class VerticaProperties {
  152.  
  153. @Value("${vertica-username}")
  154. private String username;
  155. @Value("${vertica-password}")
  156. private String password;
  157.  
  158. //getter
  159. }
  160.  
  161. /*
  162. * OtherProperties.java
  163. */
  164. package com.test.demo;
  165.  
  166. import org.springframework.beans.factory.annotation.Value;
  167. import org.springframework.stereotype.Component;
  168.  
  169. /**
  170. *
  171. * @author
  172. */
  173. @Component
  174. public class OtherProperties {
  175.  
  176. @Value("${key}")
  177. private String key;
  178. @Value("${key1}")
  179. private String key1;
  180.  
  181. //getter
  182. }
  183.  
  184. username=mysql
  185. password=mysql
  186.  
  187. username=vertica
  188. password=vertica
  189.  
  190. key=value
  191. key1=value1
  192.  
  193. @Bean
  194. public PropertyPlaceholderConfigurer otherProps() {
  195. final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  196. ppc.setIgnoreResourceNotFound(true);
  197.  
  198. final List<Resource> resourceList = new ArrayList<>();
  199.  
  200. resourceList.add(getOtherResourceFile());
  201.  
  202. ppc.setLocations(resourceList.toArray(new Resource[]{}));
  203.  
  204. return ppc;
  205. }
Add Comment
Please, Sign In to add comment