0

I need to set two values in Spring bean property value having same name.

Currently I have

I am using this property name as this in Java class : private String siteUid;

My requirement is to add another property name with diffrent value

Please suggest, if I can write both property values, and how can I use the same in Java class

2

3 Answers 3

1

You can use the @Value annotation.

If you have a properties file that contains:

foo.bar.property1=hello foo.bar.property2=world 

You can use in your Java class:

@Component public class SomeClass { @Value("${foo.bar.property1}") private String variable1; // will be set to "hello" @Value("${foo.bar.property2}") private String variable2; // will be set to "world" } 

Note that the names of the actual variables (i.e. variable1 and variable2) are irrelevant; they can be whatever you want. The important part is that the string contained in @Value matches the key in your properties file.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, silly mistake by me. I'll edit
0

Its not really clear what do you mean by adding. In general as long as you're supposed to use @Value annotation the most flexible approach by far, is using Values in autowired constructor:

@Component public class MyComponent { private String myProperty; public MyComponent(@Value('${app.value1}') String value1, @Value('${app.value2}') String value2) { // here you can even concatenate the value (hence I've said that its not clear what is 'add' but in general you can manipulate however you want this.myProperty = value1 + value2; } } 

Comments

0

You could use Spring Expression Language here to add/concat two property

@Value("#{'${property1}'.concat('${property2}')}") String parameter

Here concat is the String method. As the requirement is not very clear to me, so you could experiment with other string methods to achieve what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.