Showing posts with label cdi. Show all posts
Showing posts with label cdi. Show all posts

Thursday, 10 July 2025

@Nonnull versus @NotNull

A quick small blog...

So, we have two (seemingly) conflicting annotations for the same thing1.

Even people who know exactly when to use what have a chance to pick the wrong one, especially with the use of Code Completion and AI nowadays.

So here's a quick rundown, but the reference below is much more in depth:

import javax.annotation.Nonnull;

@Nonnull
IDEs and Compilers use it for Static Analysis of code. (Though the RetentionPolicy is set to RUNTIME)
import jakarta.validation.constraints.NotNull;

@NotNull
Validation Frameworks (like ORMs) use it during Runtime for checks.

What about Nullable values?

import javax.annotation.Nullable;

@Nullable
IDEs and Compilers use it for Static Analysis of code.

There is no @Nullable available for jakarta.validation.constraints. By default things are nullable, unless explicitly mentioned otherwise.

When not to use @NotNull

The primary really important thing to take away from this is to not do the following:

It might work, your IDE might complain or it might not, but at runtime this will be ignored, unless this method is called by a Framework as mentioned above.

Addendum

Don't use javax.validation.constraints.NotNull. It's been superseded by Jakarta when Oracle moved Java EE to Jakarta. See [2].

Referenties

[1] Medium - Understanding @NotNull vs @Nonnull in Java: A Practical Guide to Null-Safety
https://medium.com/@mesfandiari77/understanding-notnull-vs-nonnull-in-java-a-practical-guide-to-null-safety-5e179e918f37
[2] Medium - Difference between javax.validation and jakarta.validation
https://medium.com/@ovinduuu/difference-between-javax-validation-and-jakarta-validation-df0c6dbe5cb3

Thursday, 28 September 2023

Dependency Injection

Found a good explanation of Dependency Injection, one of the pillars of CDI.

Of course, the original text by Martin Fowler2 is also well worth the read, but if you want to get into details of JEE, [1] is very good.

It works great, of course sometimes I screw it up, and then I get errors that need to be resolved, for instance:

org.jboss.weld.exceptions.DeploymentException: Exception List with 8 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001443: Pseudo scoped bean has circular dependencies. Dependency path: 
  - Managed Bean [class com.mrbear.OrderService] with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.mrbear.OrderService(AddressService, x, y, z),
  - Managed Bean [class com.mrbear.AddressService] with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 11 of [BackedAnnotatedConstructor] @Inject public com.mrbear.AddressService(a, b, c, d, e, f, g, h, I, j, OrderSupplier),
  - Managed Bean [class com.mrbear.OrderSupplier with qualifiers [@Any @Default],
  - [BackedAnnotatedParameter] Parameter 7 of [BackedAnnotatedConstructor] @Inject public com.mrbear.OrderFactory(a, b, c, d, e, f, CityTownVillageSearchService),
  - Managed Bean [class com.mrbear.CityTownVillageSearchService] with qualifiers [@Any @Default],
  - [BackedAnnotatedField] @Inject private com.mrbear.CityTownVillageSearchService.OrderSupplier,
  - Managed Bean [class com.mrbear.OrderFactory] with qualifiers [@Any @Default]
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:924)
	at org.jboss.weld.bootstrap.Validator.validatePseudoScopedInjectionPoint(Validator.java:971)
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:933)
	at org.jboss.weld.bootstrap.Validator.validatePseudoScopedInjectionPoint(Validator.java:971)
	at org.jboss.weld.bootstrap.Validator.reallyValidatePseudoScopedBean(Validator.java:933)
	at org.jboss.weld.bootstrap.Va

Funnily enough, circular dependencies seemed to work fine when using Enterprise Beans or some such. I guess it depends a little on the implementation.

References

[1] JBoss Weld Reference - Chapter 4. Dependency injection and programmatic lookup
https://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html/injection.html
[2] MartinFowler.com - Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html

Wednesday, 3 February 2021

Using CDI to extend functionality

I recently wanted to change the behaviour of the framework.

Now, usually this means having to change code in the framework, but as this is something I'd rather not do, I found a way around this.

The framework, just as the code I write, uses CDI.

So it was a simple case of telling CDI to inject my Bean, instead of the Framework Bean.

And it worked right out of the box.

I was pretty surprised.

References

Contexts & Dependency Injection for Java
http://cdi-spec.org/

Thursday, 7 January 2016

Vaadin 7

Vaadin1 is an open source framework for developing web applications2 running in your browser. What is interesting is that it uses Java to do it.

It feels like you're programming like Swing in Java, but the results are transferred to your webbrowser using Vaadin. Vaadin is able to do this with the help of the Google Web Toolkit3.

Vaadin can apparently be run on the Google App Engine, if you like.

Vaadin 7 supports the JSR-286 Portlet specification. Several portals are supported. Vaadin also has some deeper integration with Liferay Portal11, in which I am very interested, as it is the one that my website is running on.

I did find a youtube video12 that introduces Vaadin in combination with JEE and NetBeans.

Upgrading to Vaadin 7

One of the problems I encountered, when using Vaadin 74 is that most of the resources available are still referring to Vaadin 6. It is important to notice the differences.

The points in [4] that have been most relevant to me are:
  • com.vaadin.Application class no longer exists. The main entry point to your application is now a com.vaadin.ui.UI, which replaces Application and its main window.
  • Also com.vaadin.terminal.gwt.server.ApplicationServlet has been replaced with com.vaadin.server.VaadinServlet in web.xml and its parameter "application" with "UI" pointing to your UI class, and the application is ready to go. Of course you can use annotations, if you prefer.

Testing

As it is Java, I should have no problem Stubbing the Vaadin framework and junit-testing my code. This to me is a big bonus.

Persistence

Persistence in Vaadin is possible by using the JPAContainer5.

JEE and CDI

I use the Servlet to inject a lot of things I need, and then provide those injected things to my UI. Works flawlessly6.

JEE and JPA together in Vaadin

I found helpful information on how to create a EjbEntityProvider here at [7]. That I can use as a base class for EJBBeans that I can inject in my Servlet and henceforth use in my UI for the JPAContainer.

A full downloadable example is available at [8].

Debugging

I have not had to debug anything, everything just works. But, for those interested, you can find it at [9].

FAQ

I get a "Failed to load the bootstrap javascript: ./VAADIN/vaadinBootstrap.js?v=7.5.5"
See [10]. Extend the @WebServlet annotation withe more than one urlPattern. See the example above.

References

[1] Vaadin
https://vaadin.com/home
[2] Wikipedia - Vaadin
https://en.wikipedia.org/wiki/Vaadin
[3] Google Web Toolkit
http://www.gwtproject.org/
[4] Vaadin Wiki - Mirating from Vaadin 6 to Vaadin 7
https://vaadin.com/wiki/-/wiki/Main/Migrating+from+Vaadin+6+to+Vaadin+7
[5] Book of Vaadin - Chapter 19 - JPAContainer
https://vaadin.com/book
[6] Vaadin Wiki - JEE6 integration with Vaadin 7
https://vaadin.com/wiki/-/wiki/Main/JEE6+integration+with+Vaadin+7
[7] Using JPA Container 2.0 in a JEE6 server
https://vaadin.com/web/matti/blog/-/blogs/using-jpa-container-2-0-in-a-jee6-server
[8] Vaadin Forum - Unable to follow J6EE wiki - NullPointerException
https://vaadin.com/forum/#!/thread/386542/389263?_19_redirect
[9] Book of Vaadin - Chapter 11.3. Debug Mode and Window
https://vaadin.com/book/-/page/advanced.debug.html
[10] Book of Vaadin - 4.9. Deploying an Application
https://vaadin.com/book/-/page/application.environment.html
[11] Liferay
http://www.liferay.com/
[12] YouTube - Java EE 7 with NetBeans and Vaadin
https://www.youtube.com/watch?v=3TompuzySD8