Spring Boot, filling two equal component-classes from different properties-files

spring boot application.properties example
how to read external properties file in spring boot
spring boot configuration properties
spring boot multiple properties files
spring boot load properties file
spring boot system properties
spring boot environment variables
spring boot application configuration

I have a problem with a spring boot application. The application uses saml for communication with an other server. Therefor I have a class with component annotation to read some properties from a custom properties file (not from application.properties) for communication information like url. I'm not allowed to post the real code, so this is an example in pseudocode.

@Component
public class A implements ConfigInterface {

@Value ("${placeholder1 from properties-File}")
private ... value1;
@Value ("${placeholder2 from properties-File}")
private ... value2;

...
}

The controller for usage looks like:

@Controller
public class controller {
@Autowired
private ConfigInterface config;

public method ( ... ) {
    ...
    createAMessage(config);
    ...
}
}

My Task is to copy the class A into a class B and load similar properties from a different properties-file. Here an example again:

@Component ("classAProperties")
public class A implements ConfigInterface {

@Value ("${placeholder1 from properties-File1}")
private ... value1;
@Value ("${placeholder2 from properties-File1}")
private ... value2;

...
}



@Component ("classBProperties")
public class B implements ConfigInterface {

@Value ("${placeholder1 from properties-File2}")
private ... value1;
@Value ("${placeholder2 from properties-File2}")
private ... value2;

...
}

Usage:

@Controller
public class controller {

@Autowired
@Qualifier("classAProperties")
private ConfigInterface configA;

@Autowired
@Qualifier("classBProperties")
private ConfigInterface configA;

public methodA ( ... ) {
    ...
    createAMessage(configA);
    ...
}

public methodB ( ... ) {
    ...
    createAMessage(configB);
    ...
}
}

The problem is, how do I can tell class A to load the properties only from properties-file1 and class B only from properties-file2? I've tried to use @PropertiesSource-annotation in both classes A and B but at the end it always uses the properties from class B in both methods of the controller.

Edit:

Both Properties-files are in resources-folder. The properties-files look like:

propertiesA.properties
placeholder1 = URL for destination
placeholder2 = Path to keystore
...

propertiesB.properties
placeholder1 = URL for other destination
placeholder2 = Path to other keystore
...

You can use @ConfigurationProperties as below,

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="props.a")
public class A implements ConfigInterface {
  private ... value1;
  private ... value2;
  //getters setters
}

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="props.b")
public class B implements ConfigInterface {
  private ... value1;
  private ... value2;
  //getters setters
}

Configuring a Spring Boot Module with @ConfigurationProperties, Spring Boot offers many different options to pass parameters like these into an application. In this article, we choose to create an application.properties file with the let it be part of a component scan by adding the @Component annotation: have two @ConfigurationProperties classes bound to the same� A Singleton in Spring Boot. We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot.


On class A you can use @PropertySource("classpath:file1.properties") and on class B you can use @PropertySource("classpath:file2.properties") annotation. This might help you.

Also, Try using @Configuration annotation also along with @PropertySource if not used already.

How to Inject a Property Value Into a Class Not Managed by Spring , In that case, we can use ClassLoader-based configuration loading or simply instantiate our classes in another bean and set required params� In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let's get started to build a simple example with Java and Maven What you'll need JDK 8+ or OpenJDK 8+ Maven 3+ Stack Java Spring Boot Init project structure and


The simplest thing to do would be to use different properties names. Failing that I believe you would need to remove the @Component annotations for A and B and use the alternative mechanism of initializing the beans via @Configuration classes as below:

@Configuration
@PropertySource("propsA")
public class ConfigurationA{

    @Bean
    public ClassA classA(){
        return new ClassA();
    }
}

@Configuration
@PropertySource("propsB")
public class ConfigurationB{

    @Bean
    public ClassB classB(){
        return new ClassB();
    }
}

21. Externalized Configuration, You can use properties files, YAML files, environment variables and @ Component public class MyBean { @Value("${name}") private String name; // As mentioned above, command line properties always take precedence over other property same locations as standard application.properties , with profiles specific files� Full Code with Spring Boot 1.5.8.RELEASE: 10_spring-boot-two-database2.zip. What you need for run this: Maven 3; Java 8; Mysql 5.x; What the code will do: Connect to two databases. Give example how to control db transactions across databases. Warning: You should know how to connect a single database, and control db transaction with Spring Boot


Spring Boot Reference Documentation, Class Conditions; Bean Conditions; Property Conditions; Resource Use Dependency Injection in Hibernate Components; 9.9.9. Configure jOOQ with Two DataSources It can also be problematic if the same filename is used (but with different If possible, always try to use the equivalent Java-based configuration. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


Getting Started, These are just a few examples of the automatic configuration Spring Boot provides. two annotations that results in web requests returning data rather than a view. The Spring Initializr creates a simple application class for you. start adding beans based on classpath settings, other beans, and various property settings. PropertyEditor is originally part of the JavaBeans specification. Spring also heavily uses the PropertyEditors to be able to represent properties in a different way than the object itself e.g. parsing human readable inputs from http request params or displaying human readable values of pure java objects in view layer. Spring has a number of built-in […]


Getting Started, You could also use it to see how to build a library (that is, a jar file that is not an and for Gradle you will want a settings.gradle including the same directories: of the two projects serves as a library that the other project (the application) will use. application.properties ), you can also add a @ConfigurationProperties class. Spring Boot 2.2 As, of Spring Boot 2.2 , Spring finds and registers @ConfigurationProperties classes via classpath scanning . Therefore, there is no need to annotate such classes with @Component (and other meta-annotations like @Configuration) or even use the @EnableConfigurationProperties: