5

I'm working with Spring Expression Language (SpEL) and noticed that expressions like @bar.int and @bar.getInt() both seem to access values from a Spring bean.

What is the actual difference between these two expressions?

#{@bar.int}

#{@bar.getInt()}

Any clarification would be appreciated!

I understand both may resolve to the same value if getInt() is a standard getter method, but are there any behavioral differences in how SpEL interprets these?

Also, are there any implications related to Spring Security, method invocation restrictions, or best practices when choosing between them?

1 Answer 1

8

Lets take this example:

@Component("bar")
public class BarService {

    private int foo = 42;
    private boolean baz = true;
}

The difference is that using #{@bar.getFoo()} will search only for the getFoo() method, and if this method is absent - Spring will throw an exception.

If you use #{@bar.foo}, Spring will first search for the getter named getFoo(), and if absent - will try to access the property directly. If the property is public, Spring will return its value; otherwise, it will throw an exception.

For boolean properties, if you use #{@bar.baz}, Spring will try to find not only the getBaz() method, but also isBaz(), before attempting to access the property directly.

In summary, method notation directly calls the specified method while property notation attempts to find appropriate getters methods and after that attempts to access the property directly.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.