8

I am trying read ldap properties from a ldap-TEST.properties file and trying to bind it to a java config class.for that i had specified @PropertSource and defined a static Bean for propertysourcesplaceholderconfigurer. still i am getting the Could not resolve placeholder spring.profiles.active in value classpath:/ldap-${spring.profiles.active}.properties below are project files please help me

@Configuration
@PropertySource("classpath:/ldap-${spring.profiles.active}.properties")
public class LdapConfig { 
 @Autowired
 Environment env;
@Bean
public LdapContextSource contextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(env.getRequiredProperty("ldap.url"));
    contextSource.setBase(env.getRequiredProperty("ldap.base"));
    contextSource.setUserDn(env.getRequiredProperty("ldap.userDn"));
    contextSource.setPassword(env.getRequiredProperty("ldap.password"));
    contextSource.afterPropertiesSet();
    return contextSource;
}

@Bean
public LdapTemplate ldapTemplate() {
    return new LdapTemplate(contextSource());
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

}

//ldap-TEST.properties file
ldap.base=dc=example,dc=com
ldap.password=password
ldap.port=839
ldap.userDn=cn=read-only-admin,dc=example,dc=com
ldap.url=ldap://ldap.forumsys.com:389

my main application

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
 }

}

ankit
  • 2,591
  • 2
  • 29
  • 54
user2307155
  • 185
  • 1
  • 3
  • 12
  • Have you checked the value of ${spring.profiles.active} ? – Oreste Viron Aug 21 '18 at 07:30
  • Check the answer from question: https://stackoverflow.com/questions/32196451/environment-specific-application-properties-file-in-spring-boot-application – MystyxMac Aug 21 '18 at 07:31
  • -Dspring.profiles.active=TEST while starting your application pass the parameter – Abdul Aug 21 '18 at 07:47
  • @OresteViron in application.properties i had defined spring.profiles.active=TEST – user2307155 Aug 21 '18 at 09:14
  • @Abdul how can i pass -Dspring.profiles.active=TEST i am deploying my application to jboss i am not running from command line could you please tell me where can i give the parameter – user2307155 Aug 21 '18 at 09:16
  • Try to log the value of ${spring.profiles.active}. I suspect there is more than one active profile. So it doesn't load the right config file. – Oreste Viron Aug 21 '18 at 09:18
  • @OresteViron i tried logging the parameter it is showing only TEST as active profile. I also tried replacing spring.profiles.active with test.profile(i had added one test.profile=TEST in application.properties file). still i am seeing the same error – user2307155 Aug 21 '18 at 09:45
  • while building i am not seeing this issue i could see only when i running the code it propbably while running it is unable to read the value – user2307155 Aug 21 '18 at 11:45

4 Answers4

2

You can not use properties like ${spring.profiles.active} inside string value of Type annotation in spring. such properties would be injected into annotations like @Value which are for properties or methods.

Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11
2

The value behind spring.profiles.active is actually an array. So even if the value was correctly expanded, there would be corner cases when it wouldn't work the way you want.

It'd be nice if the paths configured via @PropertySource would work the same way the application.properties|yml does, but that is not the case at the moment (there is an active issue on GitHub about that). So alternatives have to be considered:

  1. The simplest alternative would be to use the conventional files names application.properties|yml and application-{profile}.properties|yml. I do not see any good reason not to do it, but I do not know your project requirements so...
  2. A bit more complicated, get the configured profiles using Java code, and configure the Spring environment programmatically. See this SO answer for more details.
KevinLH
  • 328
  • 3
  • 10
  • i had just replaced ${spring.profiles.active} ${test.profile} and called @PropertySource("classpath:/ldap-${test.profile}.properties") defined test.prfoile=TEST still i am getting the same error – user2307155 Aug 21 '18 at 09:12
  • This is strange, the way you describe it, it should work. While I am not sure Mahdi's answer is right (I haven't seen it said in the Spring documentation that `${spring.profiles.active}` can be used, but it does not say it won't either), using another variable is a good way to eliminate this potential cause. It looks like your `application.properties` is not even read. Could it be that it is simply not in the right place? – KevinLH Aug 21 '18 at 14:49
  • I mean if you put annotation over class, not for it’s properties ot methods, you may not be able to use properties inside strings. Other reason is that you are defining Properties resources, so the first step is reading resources, the second step is puting resource values inside properties defined in string. My question is that how you can put value inside string while you have not specified the properties resource? Of course you wouldn’t be able to do it – Mahdi Rajabi Aug 21 '18 at 20:08
  • The value of that kind of variables in Spring can come from a lot of places, including environment variables or command-line arguments that can be read by Spring as soon as the application starts. A good question indeed though is whether variables put in `application.properties` can be used there, and you may be right that it is not the case. But I have actually used a strategy similar to the question in the past (variable in `@PropertySource`), and it did work at least with a value provided through a command-line argument or Tomcat context configuration. – KevinLH Aug 22 '18 at 08:35
0

If you are running on local machine, then Just change property file name application-default.property for temporary immediate work.

For permanent solution you have to check that your project is running on docker or not:

if running on docker then use below command:

  1. mvn clean package -DskipTests=true && sudo docker build
  2. sudo docker run -d -e spring.profiles.active="local" -e <other key and value of bootstrap.property file>

else if running on cloud server then follow below steps:

  1. in bootstrap.property file put spring.profiles.active=local
  2. rename application file to application-local.properties

you can also refer: 2.3.3. Profile Specific Files part at configuration document

ankit
  • 2,591
  • 2
  • 29
  • 54
0

I had the same problem while trying to test my Spring Boot application, this was my solution, taken from https://www.baeldung.com/spring-profiles#2-using-springprofilesactive

public class AppConfig {
  @Value("${spring.profiles.active:test}")
  private String activeProfile;
}
@SpringBootTest
@ActiveProfiles("test")
public class TestingWebApplicationTest {
  @Test
  public void contextLoads() {
  }
}
sergio.nava
  • 175
  • 2
  • 14