CDI constructor injection does not work with transitional non-serializable dependencies

I like CDI design injection, but now I have found usecase where the installation of the constructor does not seem to work properly.

In my example, I have two classes. The BeanA class does not have a defined explicit scope and does not implement Serializable. The BeanB class is annotated using @SessionScoped and implements Serializable.

public class BeanA{ } @SessionScoped public class BeanB implements Serializable{ @Inject private BeanA beanA; } 

When I try to inject an instance of BeanA into a BeanB cource, I get an UnserializableDependencyException from Weld because BeanA is not serializable. This is the expected behavior.

When I mark the "beanA" field "transient", the injection works without problems:

 @Inject private transient BeanA beanA; 

Weld now throws no exceptions.

This is great for me, but my understanding problem arises when I like it when it works with the constructor installation. When I do the following, this no longer works:

 @SessionScoped public class BeanB implements Serializable{ private transient BeanA beanA; @Inject public BeanB(BeanA beanA){ this.beanA = beanA; } public BeanB(){} } 

With this code, I get an UnserializableDependencyException again. I thought that constructor injection and injection into the field of view were more or less equivalent, but obviously this is not so. What is my mistake?

+4
source share
1 answer

This seems like a mistake. Does everything work well if you make BeanA serializable? Also, which version of Weld are you using?

+1
source

Source: https://habr.com/ru/post/1390738/


All Articles