question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How are SessionScoped beans supposed to work with java.lang.Optional?

See original GitHub issue

Currently it does not seem possible to have optional values for session scopes CDI beans?!

According to: https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configexamples.asciidoc

someone always have to provide a default value. ==> Optional is not Serializable

Also this usage of Optional is questionable: void method(@ConfigProperty(name = "a.config.key") final Optional<String> value)){...} Optional should be used for return types only!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
svenhaagcommented, May 7, 2020

Thanks for your reply. I think this should be mentioned in the documentation. E.g. in the mentioned example: https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configexamples.asciidoc

Cheers

1reaction
Emily-Jiangcommented, May 6, 2020

@svenhaag The optionality is for some properties that might have not got value assigned. The best practices is that you should provide default value. If you do need to have optional value, you can use programmatic lookup e.g. ConfigProvider.getConfig.getOptionalValue() instead of injection on your SessionScoped or ConversationScoped or any other passivation scoped beans.

The injection for the type Optional is fine for non passivation scoped beans though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with scoped beans when the scope is optional?
If I have a session, I look into the cache in the session. If the bean doesn't exist, yet, I use the objectFactory...
Read more >
Introduction to Contexts and Dependency Injection - Quarkus
The type is used to build the set of bean types. In this case, it will be double and java.lang.Object . No scope...
Read more >
Overview of Bean Scopes in Quarkus | Marcel Kliemannel
An in-depth description of all built-in bean scopes provided by Quarkus, the creation of custom scopes, and how to force the initialization ...
Read more >
Configuring Managed Beans - The Java EE 6 Tutorial
When a page references a managed bean for the first time, the JavaServer Faces implementation initializes it based on a @ManagedBean annotation in...
Read more >
Beans
Suppose that we have two existing Java classes that we've been using for ... For example, any web application may have session scoped...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found