Using spring boot I set value in .properties file and want that value to be used in my service class. Is there any way to get value from active profile and use it in service class?
Here is my sample code in .properties
file
spring.rabbitmq.listener.simple.retry.enabled= true
spring.rabbitmq.listener.simple.retry.initial-interval= 10s
spring.rabbitmq.listener.simple.retry.max-attempts= 2
spring.rabbitmq.listener.simple.retry.max-interval= 30s
spring.rabbitmq.listener.simple.retry.multiplier= 2
I want to get .max-attempts= 2 in my service class
Here is the method of service class where I want to max-attempts value.
public boolean validateRetryAttempts(EmailDto email)
{
@Value("${spring.profiles.active:unknown"
int maxAttempt = 2;
if (!maxRetryAttempts.containsKey(email.getEmail())) {
maxRetryAttempts.put(email.getEmail(), 1);
} else {
if ((maxRetryAttempts.get(email.getEmail()) 1)>=maxAttempt) {
maxRetryAttempts.put(email.getEmail(), maxRetryAttempts.get(email.getEmail()) 1);
return true;
} else
maxRetryAttempts.put(email.getEmail(), maxRetryAttempts.get(email.getEmail()) 1);
}
return false;
}
I have tried @Value("${spring.profiles.active: unknown")
in the method but it gives disallowed location error.
CodePudding user response:
You can't place @Value
inside a method. Valid locations are:
- Field
- Method
- Parameter
- Annotation
Having written this, please place it as a Service property:
@Service
public class Service {
@Value("${spring.rabbitmq.listener.simple.retry.max-attempts}")
private int maxAttempt;
public boolean validateRetryAttempts(EmailDto email){
if (!maxRetryAttempts.containsKey(email.getEmail())) {
maxRetryAttempts.put(email.getEmail(), 1);
} else {
if ((maxRetryAttempts.get(email.getEmail()) 1)>=maxAttempt) {
maxRetryAttempts.put(email.getEmail(), maxRetryAttempts.get(email.getEmail()) 1);
return true;
} else
maxRetryAttempts.put(email.getEmail(), maxRetryAttempts.get(email.getEmail()) 1);
}
return false;
}
}