To Discuss or Not To Discuss
Author
Marcus HeldHi,
As you are a loyal reader of my newsletter, you probably know that I publish content on three different channels.
- This newsletter comes out on Mondays and Thursdays.
- A LinkedIn post on Tuesdays and Fridays
- And every Wednesday, I write a blog
Each Content Targets a Different Audience
The newsletter is infotainment. Its purpose is to entertain you, to start your day with a fresh thought. And it allows you to gain a deeper insight into my thoughts and perspectives.
LinkedIn is for reach. At the end of the day, I am a consultant, and this is often where I make my first contact with decision-makers.
And my blog is SEO-optimized, intended to provide quick answers to common questions. (This wasn’t always the case - but that’s a story for another time)
My Blog Yesterday Was a Puzzle
It covers event handling in Spring.
ApplicationEvents
also provide the option to formulate conditions with SpEL expressions.
For example, if you have a CustomSpringEvent
:
public class CustomSpringEvent {
private boolean publish;
public CustomSpringEvent(boolean publish) {
this.publish = publish;
}
public boolean isPublish() {
return publish;
}
}
Then with the following configuration, you can ensure that only events with the publish
flag are processed:
@EventListener(condition = "#event.publish")
public void handleEvent(CustomSpringEvent event) {
log.info("handled " + event);
}
However, I don’t want to promote this feature. SpEL expressions are expressed as a string. So, if we rename publish
to distribute
, the compiler can’t help us here. The code will still compile. But the code is flawed.
SpEL expressions are a perfect example of Murphy’s law :
“Anything that can go wrong will go wrong.”
So, what should I do?
My blog is beginner content. Those who come across this article through Google are still trying to grasp the basics of Spring.
If I explain the Condition-Feature, it will be used.
If I explain in detail why this feature should be avoided, then it’s out-of-scope.
But beginners also have the right to read this discussion and form their own opinion.
So, what should I do? 😕
This time, I didn’t delve into it. Is that the right approach? I don’t know.
Rule the Backend,
~ Marcus