paramupk
paramupk

Reputation: 626

Spring property value inside annotation

How can I get property value inside an annotation. For example I have an annotation

@GetMyValue(value1="Val1",intVal=10)

Now I want "Val1" and 10 to be coming from a property file. I tried

@GetMyValue(value1="${test.value}",intVal="${test.int.value}")

Which doesn't work.

I understand I can use

@Value("${test.value}")
String value;

@Value("${test.int.value}")
int intValue;

I don't want that, it need to be inside an annotation. Any suggestions?

Upvotes: 3

Views: 3712

Answers (3)

Ming
Ming

Reputation: 61

Here is how:

GetMyValue(value1="#{'${test.value}'}",intVal="#{'${test.int.value}'}")

Upvotes: 1

Liping Huang
Liping Huang

Reputation: 4476

This need involve more code works I think, but maybe you can have a workaround, for example, not hardcode the value for @GetMyValue annonation, just introduce two parameters in a config bean.

private String stringVal;
private int intVal;

then you can use this two params in you annonation by spEL.

Upvotes: 0

benbenw
benbenw

Reputation: 753

In the Spring @Value the replacement of the placeholder is not done inside the annotation but by the framework when inspecting the bean.

See

  • DefaultListableBeanFactory#doResolveDependency
  • DefaultListableBeanFactory#resolveEmbeddedValue
  • org.springframework.util.StringValueResolver

So, you have to "manually" get the annotation value1 and intVal (which should be a string in your annotation) and resolve them against your properties file.

Upvotes: 1

Related Questions