Spring setting #value from another variable - spring

Is it possible to set #Value from another variable
For eg.
System properties : firstvariable=hello ,secondvariable=world
#Value(#{systemProperties['firstvariable'])
String var1;
Now I would like to have var2 to be concatenated with var1 and dependant on it , something like
#Value( var1 + #{systemProperties['secondvariable']
String var2;
public void message(){ System.out.printlng("Message : " + var2 );

No, you can't do that or you will get The value for annotation attribute Value.value must be a constant expression.
However, you can achieve same thing with following
In your property file
firstvariable=hello
secondvariable=${firstvariable}world
and then read value as
#Value("${secondvariable}")
private String var2;
Output of System.out.println("Message : " + var2 ) would be Message : helloworld.

As the question use the predefined systemProperties variable with EL expiration.
My guess is that the meaning was to use java system properties (e.g. -D options).
As #value accept el expressions you may use
#Value("#{systemProperties['firstvariable']}#systemProperties['secondvariable']}")
private String message;
You said
Now I would like to have var2 to be concatenated with var1 and
dependent on it
Note that in this case changing var2 will not have effect on message as the
message is set when the bean is initialized

In my case (using Kotlin and Springboot together), I have to escape "$" character as well. Otherwise Intellij IDEA gives compile time error:
Implementation gives error:
#Value("${SCRIPT_PATH}")
private val SCRIPT_PATH: String = ""
Error: An annotation argument must be a compile-time constant
Solution:
#Value("\${SCRIPT_PATH}")
private val SCRIPT_PATH: String = ""

Related

Spring boot how to append multiple properties to single value?

I have multiple properties in my application.properties file
prop.a=A
prop.b=B
prop.c=C
// and more
Now i have to add the property A to the rest of them. I am doing this like following
#Value("${prop.a}")
private String a;
#Value("${prop.b}")
private String b;
b = new StringBuffer(b).append(a).toString();
I have to individually append each string. Can i do this in the annotation? like #Value("${prop.b}" + "${prop.a}") ?
If you want to do this programmatically, you have to do this:
#Value( "${prop.a}${prop.b}" )
private String b;
You can, however, achieve this in application.properties itself this way:
prop.a=A
prop.b=${prop.a}B
prop.c=${prop.a}C
(Please note that wherever your example says prob.*,I have changed to prop.*.)

How to select the value of a string option from another option in Kbuild Kconfig files?

For Boolean config, I am using select. Is there a similar one for string?
Eventually, I would like to have something like:
config MY_VAR_STR
string
config MY_VAR_BOOL
bool
default n
config OPTION_2
bool
# Set MY_VAR_BOOL value to y
select MY_VAR_BOOL
# something like set MY_VAR "test string"
It is not possible to use select for non booleans according to kernel docs v4.15
https://github.com/torvalds/linux/blob/v4.15/Documentation/kbuild/kconfig-language.txt#L104 says:
- reverse dependencies: "select" <symbol> ["if" <expr>]
[...]
Reverse dependencies can only be used with boolean or tristate symbols.
This likely implies it is not possible at all.
It's possible to use default on string variable like that:
config MY_VAR_STR
string
default "test string" if OPTION_2
config OPTION_2
bool

JMeter BeanShell Assertion: Getting error when convert String to Long

Have a need to change the value from String to Long in BeanShell Assertion to do verification.
First Apporach
long balance_after_credit = Long.parseLong(String.valueOf("${balance_after_credit_from_db}"));
Second Approach
long balance_after_credit = Long.parseLong(vars.get("balance_after_credit_from_db"));
For instance, consider am getting a value as '743432545' for the variable balance_after_credit_from_db.
Error
org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``long token_balance_after_credit = Long.parseLong(vars.get("token_balance_after_c . . . '' : Typed variable declaration : Method Invocation Long.parseLong
Weird thing is sometimes, I didn't get errors and the script is getting passed.
Can anyone please point out where am doing a mistake. TIA.
Inlining JMeter variables into code-based scripts is not something recommended so go for 2nd approach.
How do you know that exactly String is being returned from the database all the time? It easily can be any other object type, in fact any of described in the Mapping SQL and Java Types article. The way more safe approach will be something like:
if (vars.getObject("balance_after_credit_from_db") instanceof String) {
long balance_after_credit = Long.parseLong(vars.get("balance_after_credit_from_db"));
}
else {
log.error("Unexpected \balance_after_credit_from_db\" variable type");
log.error("Expected: String, Actual: " + vars.getObject("balance_after_credit_from_db").getClass().getName());
throw new Exception("Unexpected variable type");
}
So in case of non-String JDBC query result you will be able to see the relevant message in jmeter.log file
See Debugging JDBC Sampler Results in JMeter article for more information on working with the entities coming from databases in JMeter tests
The second option
long balance_after_credit = Long.parseLong(vars.get("balance_after_credit_from_db"));
should work, provided you have a valid numeric variable value. For instance try to run something like this:
vars.put("x", "743432545");
long balance_after_credit = Long.parseLong(vars.get("x"));
It won't return any exception.
The problem is when the variable is not defined, has empty or non-numeric value. Then Long.parseLong will throw a NumberFormatException, which you shold catch and make use of (treat it as assertion failure):
String rawValue = vars.get("balance_after_credit_from_db");
long balance_after_credit = Long.MAX_VALUE; // initialize with some unrealistic value
try {
balance_after_credit = Long.parseLong(rawValue);
}
catch(NumberFormatException e) {
Failure = true;
FailureMessage = "Variable does not contain a valid long. It was " + rawValue + " instead";
}

Evaluate String value in jmeter

I want to evaluate the value of SEATID.
Find my code below.
String res2[0] = "40B";
vars.put("SEAT_ID",res2[0]);
When i am trying the get the value of SEATID in a string i am not getting the actual value of SEATID.
String otherSampler = vars.get("{"seatCode":"${SEAT_ID}");
Output is coming as : {"seatCode":"${SEAT_ID}
Expected output : {"seatCode":"40B"}
I am wring the code in Beanshell pre processor. Please help.
You need to use vars.get on SEAT_ID
String otherSampler = "{\"seatCode\":\"" + vars.get("SEAT_ID") + "\"}"
Your code is wrong. Try using this for declaring SEAT_ID variable:
vars.put("SEAT_ID","{\"seatCode\":\"" + res2[0] + "\"}");
Then you could use:
String otherSampler = vars.get(SEAT_ID);
To get:
{"seatCode":"40B"}

variable was written but never used in Swift 2.0 & Xcode 7

When I create a variable without String() to initialize the variable an error shows up alerting, "variable not initialize", same error shows up when I directly assign
var username: String = usernameTextfieldSigup.text!
And when I initialize the variable using the code below,
var username: String = String()
username = usernameTextfieldSignup.text!
warning comes up, variable 'username' was written but never used.
I know I could just ignore these warnings but I'd like a cleaner code.
I'm using Xcode7 beta5 release.
You can declare the username without a value first:
var username: String
and then assign a value to it in your initializer:
// replace with your (valid) initializer
func init() {
username = usernameTextfieldSignup.text!
}
However, I'm sure that usernameTextfieldSignup.text won't have a value until the user ha actually provided a value. So in that case I would recommend to make username an optional so you can set it's value when you need to:
var username: String?
and then when setting it:
username = usernameTextfieldSignup.text
and when accessing it:
if let uname = username {
// do something with the uname
}
Also, the variable 'username' was written but never used warning you're seeing is probably because you write to / initialise username but there's no code actually reading it, so it's kind of a useless variable according to the compiler then.
To init an empty string you have to either make it explicit or optional like so:
let string: String!
let string: String?
You can do this with any datatype.

Resources