3

I have some pojo class based on the ConfigurationProperties, prefix attribute coming from application.properties

@Data
@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(ignoreUnknownFields = true, prefix = "default")
@Component
public class AdditionalProperties {

    String name;
    String size;


}

Now I have 2 more configurations with the prefix "a" and "b" .

In any class I am autowiring the Component to use like this @Autowired private AdditionalProperties additionalProperties;

Here a and b are dynamic , those come in type property in HttpServletRequest (request.getParameter("type") : this will give u a or b )

So, what the class AdditionalProperties is autowired, it should already be loaded with the configuration based on the httpServletRequest.

What i did 1. I wrote 3 class extending the main AdditionalProperties class with the changed prefix

   @Data
   @Configuration
   @PropertySource("classpath:application.properties")
   @ConfigurationProperties(ignoreUnknownFields = true, prefix = "a")
   @Component
   public class AdditionalPropertiesA {

       String name;
       String size;


   }

   @Data
   @Configuration
   @PropertySource("classpath:application.properties")
   @ConfigurationProperties(ignoreUnknownFields = true, prefix = "b")
   @Component
   public class AdditionalPropertiesB {

       String name;
       String size;


   }

After that i tried loading the AdditionalProperties class in the constuctor while using request and BeanPropty class like this

   @Autowired
      BeanFactory bean ;


   @Autowired
      HttpServletRequest request ;

      private AdditionalProperties additionalProperties ; 

   ublic XClass() {
          String type  = request.getParameter("type") ;
          this.additionalProperties = (AdditionalProperties) bean.getBean(type);
      }

So whenever XClass is running it already has the AdditionalProperties in place .

Please let me know what other approach can be there .

Thiru
  • 2,541
  • 4
  • 25
  • 39
Mohit H
  • 927
  • 3
  • 11
  • 26
  • Check this link https://stackoverflow.com/questions/42909283/how-to-dynamically-inject-a-service-using-a-runtime-qualifier-variable – karepu Oct 16 '19 at 11:29

0 Answers0