Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Value annotation

Tags:

spring

I have 2 property files a.properties and b.properties I've added the to application context as:

<context:property-placeholder location="classpath:a.properties" />
<context:property-placeholder location="classpath:b.properties"/>

First file with properties contains db connection details(this works well) Second - contains properties that are used by some specific bean. In that bean I use these properties via @Value annotation

@Value("#{qw.er}")
private String someA;    
@Value("#{as.df}")
private String someB;

However I get exception during startup:

 org.springframework.expression.spel.SpelEvaluationException: 
EL1008E:(pos 0): Field or property 'qw' cannot be found on object of type 
'org.springframework.beans.factory.config.BeanExpressionContext'

What I'm doing wrong?

Is it possible yo use 2 context:property-placeholder in one file?

PS: Properties qw.er and as.df exist only in file b.properties

like image 480
maks Avatar asked Jul 17 '13 12:07

maks


People also ask

What is the use of @value annotation?

One of the most important annotations in spring is @Value annotation which is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL).

How does @value work in spring boot?

Spring @Value with methodsWhen the @Value annotation is found on a method, Spring context will invoke it when all the spring configurations and beans are getting loaded. If the method has multiple arguments, then every argument value is mapped from the method annotation.

Can I use @value in @service?

@Value annotation can be used within classes annotated with @Configuration , @Component and other stereotype annotations like @Controller , @Service etc.

Can we use @value in interface?

No, this is not (directly) possible. The default value of an annotation property must be a compile-time constant. The values that you are trying to inject from application.


1 Answers

It is explained here

After defining your properties you should use

@Value("${qw.er}")
private String someA;

Notice $ sign.

like image 90
GokcenG Avatar answered Nov 08 '22 19:11

GokcenG