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

Thursday, 21 September 2023

How to get started with Java as quickly as possible

Apparently, Java 21 has some new things1 to make it easier to get started with Java2 right away.

So this is the ultimate blogpost of mine to get started quickly and easily.

Fair warning: I'm using the macos version of Java, so the directory structure might be a bit different compared to other Operating Systems.

Step 1. Download java

Go to https://jdk.java.net/21 and download the build you need.

Step 2. Unpack

Unpack the archive somewhere.

% tar zxvf Downloads/openjdk-21_macos-x64_bin.tar.gz

It will unpack in directory jdk-21.jdk.

Step 3. Write a small Java program

In this instance, we'll assume the file is called HelloWorld.java.

void main() {
  System.out.println("Hello, world.");
}

Step 4. Run the small Java program

% ~/jdk-21.jdk/Contents/Home/bin/java --enable-preview --source 21 ./HelloWorld.java
Note: ./HelloWorld.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
Hello, world.

Congratulations, you've run your first Java Program!

The other way to do it, which admittedly is even quicker, is using the Java Playground3.

References

[1] JEP 445: Unnamed Classes and Instance Main Methods (Preview)
https://openjdk.org/jeps/445
[2] Paving the on-ramp
https://randomthoughtsonjavaprogramming.blogspot.com/2022/10/paving-on-ramp.html
[3] dev.java - Java Playground
https://dev.java/playground/

Tuesday, 19 September 2023

Java 21 Release - Launch Event

All right! Java 21 is scheduled for General Availability1 on 2023/09/19! That's today!

Apparently there's an appropriate launch event2 3 as well!

And it's a Long Term Supported release (LTS), so that's good.

Hoping to see support for it everywhere.

References

[1] OpenJDK Java 21
https://openjdk.org/projects/jdk/21/
[2] Java 21 Launch Event
https://dev.java/community/java-21-launch/
[3] YouTube.com - Java 21 Launch Event
https://www.youtube.com/watch?v=E8NV68ihJyY

Thursday, 7 September 2023

10 Common Mistakes Java Developers Make when Writing SQL

Just some references to very important articles about common mistakes Java Developers make when trying to write SQL statements.

Just putting it here, in case I forget.

Because let's be honest here, I've committed quite a few in my time.

References

JAVA, SQL AND JOOQ. - 10 Common Mistakes Java Developers Make when Writing SQL
https://blog.jooq.org/10-common-mistakes-java-developers-make-when-writing-sql/
JAVA, SQL AND JOOQ. - 10 More Common Mistakes Java Developers Make when Writing SQL
https://blog.jooq.org/10-more-common-mistakes-java-developers-make-when-writing-sql/
JAVA, SQL AND JOOQ. - Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)
https://blog.jooq.org/yet-another-10-common-mistakes-java-developer-make-when-writing-sql-you-wont-believe-the-last-one/