Questions tagged [spring-el]
The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.
979
questions
156
votes
4
answers
67k
views
Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)
I'm a little confused concerning when to use ${...} compared to #{...}. Spring's documentation only uses #{...}, but there are plenty of examples that use ${...}. Furthermore, when I started with SpEL ...
71
votes
8
answers
150k
views
How to fill HashMap from java property file with Spring @Value
Is it possible to use Spring @Value, to map values from properties file to the HashMap.
Currently I have something like this, and mapping one value is not a problem.
But I need to map custom values ...
60
votes
3
answers
66k
views
How do I get a property value from an ApplicationContext object? (not using an annotation)
If I have:
@Autowired private ApplicationContext ctx;
I can get beans and resources by using one of the the getBean methods. However, I can't figure out how to get property values.
Obviously, I can ...
57
votes
12
answers
189k
views
Thymeleaf using path variables to th:href
Here's my code, where I'm iterating through:
<tr th:each="category : ${categories}">
<td th:text="${category.idCategory}"></td>
<td th:text="${category.name}"></...
53
votes
9
answers
64k
views
How does Spring 3 expression language interact with property placeholders?
Spring 3 has introduced a new expression language (SpEL) which can be used in bean definitions. The syntax itself is fairly well specified.
What isn't clear is how, if at all, SpEL interacts with the ...
50
votes
6
answers
71k
views
Spring Boot - How to disable @Cacheable during development?
I'm looking for 2 things:
How to disable all caching during development with Spring boot "dev" profile. There doesn't seem to be a general setting to turn it all off in application....
50
votes
1
answer
24k
views
Calling static methods from Spring Security Expressions?
I'm looking for a way to extend Spring Security Expressions to support an existing security infrastructure. I'm aware you can extend the MethodSecurityExpressionRoot as described here, but I also ...
44
votes
6
answers
62k
views
Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'
I want to use the @Value annotation to inject a Double property such as:
@Service
public class MyService {
@Value("${item.priceFactor}")
private Double priceFactor = 0.1;
// ...
and using ...
43
votes
6
answers
153k
views
Concatenate multiple node values in xpath
I have a XML that looks like this
<element1>
<element2>
<element3>
<element4>Hello</element4>
<element5>World</element5&...
36
votes
2
answers
25k
views
Compare Enums in SpEL
At Spring's @Cacheable annotation I want to specify an unless condition.
However my return value is neither a primitive type nor a Java bean, but an Enum.
How can I compare for equality with another ...
31
votes
8
answers
45k
views
Thymeleaf + Spring : How to keep line break?
I'm using Thymeleaf template engine with spring and I'd like to display text stored throught a multiline textarea.
In my database multiline string are store with "\n" like this : "Test1\nTest2\n...."
...
29
votes
1
answer
85k
views
Thymeleaf: show text if the attribute and property exists
Is there a simple way in thymeleaf to show the content of an attribute property if the property and the attribute exist? If there's an attribute "error" with a property "summary" in my html page, I'd ...
26
votes
3
answers
59k
views
How do I set JobParameters in spring batch with spring-boot
I followed the guide at http://spring.io/guides/gs/batch-processing/ but it describes a job with no configurable parameters. I'm using Maven to build my project.
I'm porting an existing job that I ...
26
votes
1
answer
14k
views
No bean resolver registered in the context to resolve access to bean
I'm trying to implement method security using Java Config, but I'm getting a error:-
org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the ...
19
votes
7
answers
29k
views
Spring 3: Inject Default Bean Unless Another Bean Present
I would like to configure Spring via XML such that if a particular bean exists, it will be injected into the target bean. If it does not exist, a different, default bean, will be injected.
For ...
19
votes
3
answers
45k
views
Spring Boot SpEL ConditionalOnExpression check multiple properties
Question:
How can I use Spring Expression Language to check that 2 Boolean properties are true?
For example, checking that a single property is true would use the syntax:
Example
@...
19
votes
5
answers
23k
views
Pass method argument in Aspect of custom annotation
I'm trying to use something similar to org.springframework.cache.annotation.Cacheable :
Custom annotation:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
...
18
votes
4
answers
18k
views
Spring cacheable - filter out empty collections using SpEL
I wanted to know is there a way to use SpEL in order to filter out values like empty collections.
My cache currently filters out null values:
@Cacheable(value = "groupIdToGroupNames",unless = "#...
18
votes
4
answers
32k
views
SpEL @ConditionalOnProperty string property empty or nulll
I am currently having trouble with my dataSource bean creation on condition of String property from my applications.yaml file.
Ideally, I would only like to create the dataSource bean only if the url ...
18
votes
5
answers
32k
views
Getting the user home path in application.properties in Spring Boot
This should be a quite straight forward task, but after doing quite a bit of research I'm finding it hard to find any way to do this.
I just want to create a log file in the current user's home ...
18
votes
6
answers
67k
views
SpEL parsing a valid expression, there is still more data in expression
I am using Spring Expression Language (SpEL) and created a sample program. The code snippet is
ExpressionParser parser=new SpelExpressionParser();
Expression expression=parser.parseExpression("Hello ...
18
votes
2
answers
22k
views
Programmatically evaluate a bean expression with Spring Expression Language
I have a simple Spring Bean Expression, which evaluates fine when I define it inside an application context file:
<bean id="myConfigBean" class="com.example.myBeanConfigBean">
<property ...
18
votes
3
answers
37k
views
How to define default null value in application.yml in Spring Boot
I'm trying to define the default value as null value in application.yml with SpringBoot version 1.3.0.RELEASE. The goal is be able to refer it with a class with ConfigurationProperties annotation
-- ...
16
votes
2
answers
20k
views
How to access system properties in Thymeleaf template?
I need to access system properties in a Thymeleaf template. It would be nice if this was possible so that I don't have to populate the spring mvc model explicitly with properties. I'm trying to use ...
16
votes
1
answer
11k
views
Use instanceof in Thymeleaf
Is there a way to use the Java instanceof operator in Thymeleaf?
Something like:
<span th:if="${animal} instanceof my.project.Cat" th:text="A cat"></span>
<span th:if="${animal} ...
16
votes
3
answers
21k
views
How do I escape values in SpEL?
I am writing some SpEL statements in XML, and I can't get the parser to determine when I need to escape a character.
I've attempted the following:
<... property="someProperty" value="#{ someBean....
16
votes
8
answers
18k
views
How to escape SpEL dollar signs in Spring YAML configuration?
In a Spring YAML configuration file, I need to have a parameter like
csv:
file:
pattern: /some/path/${app-name}.csv
where the ${app-name} is dynamically replaced in run time by the Java code, ...
16
votes
4
answers
6k
views
How can localized messages in Thymeleaf be processed using SpEL
I am a beginner with ThymeLeaf and have not used SpEL too much except for @PreAuthorize annotations, so please be so kind to help me out.
I am using ThymeLeaf (version 2.1.2) together with Spring (4....
15
votes
2
answers
40k
views
Handle null values in spring expression language
I have the following code using spring expression language:
StandardEvaluationContext stdContext = new StandardEvaluationContext();
stdContext.setVariable("emp", filterInputData);
ExpressionParser ...
15
votes
1
answer
8k
views
How do I reference a nested type in SpEL?
Given a class containing an enum:
public class MyClass {
public enum NestedEnum {
value1(1),
value2(2);
private int code;
private NestedEnum(int code) {
...
15
votes
3
answers
14k
views
Spring Expression Language - Java 8 forEach or stream on list
Is it possible for stream or forEach on list in SpEL?
e.g.
List<String> x = new LinkedList<>(Arrays.asList("A","AAB"));
ExpressionParser parser = new SpelExpressionParser();
...
15
votes
1
answer
12k
views
Spring Expression Language in custom annotation
I want to use Spring Expression Language in a custom Annotation.
This annotation will be consumed by a custom Aspect.
Check this out:
@StatisticEventTrigger(value = TestStatisticEvent.class, ...
14
votes
1
answer
5k
views
Can Spring EL (SpEL) be configured to ignore null objects in the middle of an expression
If I have the expression: obj1.obj2.obj3
And obj2 is null, then expression fails with an exception. Is there any way to configure SpEL to just return null?
14
votes
1
answer
8k
views
Accessing static variable in spring annotations using spel
I have a value in annotation, for which I want to assign a static variable
I tried something like this
@Cacheable(value = "#com.test.App.VALUE")
public List someCachableMethod() { }
After ...
14
votes
1
answer
21k
views
EL1008E: Property or Field 'timestamp' cannot be found on object of type 'java.util.HashMap' - maybe not public?
When I use Spring Boot's global exception handler, I got this:
org.springframework.expression.spel.SpelEvaluationException: EL1008E:
Property or field 'timestamp' cannot be found on object of ...
14
votes
2
answers
6k
views
Where can I find an implementation of Java EL 3.0
The Expression Language version 3.0 is currently being designed. It adds various cool features like mapping and filtering collections by a lambda expression. Spring EL has a comparable feature which ...
13
votes
2
answers
18k
views
how to use Spring-EL in @Value when using constants to resolve a property
I'm trying to use a constant to define a property and then resolving it with the @Value annotation.
I defined the constant in an interface:
public interface InternalConstant{
public static final ...
13
votes
2
answers
9k
views
Why doesn't this stream & lambda expression work with SpEL declaration?
I'm trying to use a Java 8 stream and lambda expression in a Spring @Cache annotation.
I'm trying to use the following:
@CacheEvict(value = "tags", allEntries = true,
condition = "#entity.getTags()....
13
votes
1
answer
2k
views
Get java annotation attribute value on Spring SpEL expression
I need to implement an authorization expression on multiple controllers. For this I have decided to create a personalized annotation that facilitates its use. The problem is that the authorization ...
13
votes
1
answer
3k
views
Spel not supported on spring annotation @Scheduled.fixedDelayString
I want to configure the fixedDelay in seconds in my properties file, then I want to convert it to millis in @Scheduled annotation.
I expected this to work:
@Scheduled(fixedDelayString = "#{${my....
12
votes
3
answers
47k
views
How to replace a placeholder in a String with a SimpleDateFormat Pattern
In a given String like this
".../uploads/${customer}/${dateTime('yyyyMMdd')}/report.pdf"
I need to replace a customer and a yyyyMMdd timestamp.
To replace the customer placeholder, I could use the ...
12
votes
1
answer
15k
views
what's the best way to use application constants in spring xml configuration?
I want to use my application constants within spring xml configuration.
I know to do that with spring SpEl with something like this:
<bean class="example.SomeBean">
<property name="...
12
votes
1
answer
14k
views
How to reference a constant in Spring Expression Language
i am quite new to Spring and i have a bean declaration as follows.
<bean id="mybean" class="" scope="prototype">
<property name='typeOf' value='#{typeOfBuilder.getKeyFor("...
12
votes
2
answers
23k
views
Spring expression language (SpEL): parse String to int
I've got a (String, obviously) property expressed in minutes that I want to convert to an int before I do some arithmetic and inject it into my Spring bean. Right now I have this SpEL expression:
#{T(...
12
votes
3
answers
10k
views
How to pass dynamic topic name to @KafkaListener(topics) from environment variable
I'm writing a Kafka consumer. I need to pass the environment variable topic name to @KafkaListener(topics = ...). This is what I have tried so far:
import org.springframework.beans.factory.annotation....
12
votes
1
answer
1k
views
Referencing a property of the current bean in Spring EL
I'd like to create a number of beans from a single class, all to be instantiated in the current application context, each based on prefixed properties in a properties file. I've given an example of ...
11
votes
1
answer
16k
views
How to JUnit tests a @PreAuthorize annotation and its spring EL specified by a spring MVC Controller?
I've defined this method in my Spring MVC Controller :
@RequestMapping(value = "{id}/content", method=RequestMethod.POST)
@PreAuthorize("principal.user.userAccount instanceof T(com.anonym.model....
11
votes
3
answers
12k
views
Using spring3 @Value to access PropertyPlaceholderConfigurer values?
I'm trying to set the value of a string in a spring bean using @Value, when my property source is a subclass of PropertyPlaceholderConfigurer. Anyone know how to do this ?
10
votes
2
answers
6k
views
spring expression read file content
How to use spring expression to read file content and put it into a string?
I would like to do the following.
For example,
@Value("classpath:myquery.sql")
File f;
@Value("#{org.apache.commons.io....
10
votes
2
answers
17k
views
#root and #this in SpEL
In Spring 3 SpEL, #this and #root were introduced.
The variable #root is always defined and refers to the root context
object. Although #this may vary as components of an expression are
...