February 17th, 2023
Using Dynamic Resource Properties

If you want to use dynamic properties in your spring applications then it is best to make a class and use @ConfigurationProperties.

 

application.properties

				
					logging.level.org.springframework=debug
#spring.profiles.active=dev

currency-service.url=http://brains.com
currency-service.username=defaultUsername
currency-service.key=defaultKey
				
			
				
					package com.brains.springboot.learnspringboot;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import lombok.Data;

@ConfigurationProperties(prefix = "currency-service")
@Data
@Component
public class CurrencyServiceConfiguration {

	private String url;
	private String username;
	private String key;
}

				
			
				
					package com.brains.springboot.learnspringboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CourseController {

	@Autowired
	private CurrencyServiceConfiguration currencyServiceConfiguration;

	@GetMapping("/currency")
	public CurrencyServiceConfiguration getCurrencyDetails() {
		return currencyServiceConfiguration;
	}
}

				
			

Running the Application

 

Related Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *