Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'mysql-username' in value "${mysql-username}"
- package com.test.demo;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- @Configuration
- @ComponentScan
- public class DemoApplication {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApplication.class);
- MyController bean = applicationContext.getBean(MyController.class);
- bean.log();
- }
- @Bean
- public PropertyPlaceholderConfigurer otherProps() {
- final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
- ppc.setIgnoreResourceNotFound(true);
- final List<Resource> resourceList = new ArrayList<>();
- resourceList.add(getOtherResourceFile());
- ppc.setLocations(resourceList.toArray(new Resource[]{}));
- return ppc;
- }
- @Bean
- public PropertyPlaceholderConfigurer verticaProps() {
- final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
- ppc.setIgnoreResourceNotFound(true);
- ppc.setPlaceholderPrefix("${vertica-");
- final List<Resource> resourceList = new ArrayList<>();
- resourceList.add(getVerticaResourceFile());
- ppc.setLocations(resourceList.toArray(new Resource[]{}));
- return ppc;
- }
- @Bean
- public PropertyPlaceholderConfigurer mysqlProps() {
- final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
- ppc.setIgnoreResourceNotFound(true);
- ppc.setPlaceholderPrefix("${mysql-");
- final List<Resource> resourceList = new ArrayList<>();
- resourceList.add(getDatabasePropertiesFile());
- ppc.setLocations(resourceList.toArray(new Resource[]{}));
- return ppc;
- }
- private Resource getOtherResourceFile() {
- Resource resource = new ClassPathResource("other.properties");
- return resource;
- }
- private Resource getVerticaResourceFile() {
- Resource resource = new ClassPathResource("vertica.properties");
- return resource;
- }
- private Resource getDatabasePropertiesFile() {
- Resource resource = new ClassPathResource("mysql.properties");
- return resource;
- }
- }
- /*
- * MyController.java
- */
- package com.test.demo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- /**
- *
- * @author
- */
- @Component
- public class MyController {
- @Autowired
- private MySqlProperties mySqlProperties;
- @Autowired
- private VerticaProperties verticaProperties;
- @Autowired
- private OtherProperties otherProperties;
- public String log() {
- System.out.println(mySqlProperties.getUsername());
- System.out.println(verticaProperties.getUsername());
- System.out.println(otherProperties.getKey());
- return "hello";
- }
- }
- /*
- * MySqlProperties.java
- */
- package com.test.demo;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- /**
- *
- * @author
- */
- @Component
- public class MySqlProperties {
- @Value("${mysql-username}")
- private String username;
- @Value("${mysql-password}")
- private String password;
- //getter
- }
- /*
- * VerticaProperties.java
- */
- package com.test.demo;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- /**
- *
- * @author
- */
- @Component
- public class VerticaProperties {
- @Value("${vertica-username}")
- private String username;
- @Value("${vertica-password}")
- private String password;
- //getter
- }
- /*
- * OtherProperties.java
- */
- package com.test.demo;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- /**
- *
- * @author
- */
- @Component
- public class OtherProperties {
- @Value("${key}")
- private String key;
- @Value("${key1}")
- private String key1;
- //getter
- }
- username=mysql
- password=mysql
- username=vertica
- password=vertica
- key=value
- key1=value1
- @Bean
- public PropertyPlaceholderConfigurer otherProps() {
- final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
- ppc.setIgnoreResourceNotFound(true);
- final List<Resource> resourceList = new ArrayList<>();
- resourceList.add(getOtherResourceFile());
- ppc.setLocations(resourceList.toArray(new Resource[]{}));
- return ppc;
- }
Add Comment
Please, Sign In to add comment