-
Hello! From what I researched, it seems that this happens because classes that are annotated with normal scoped beans such as @ApplicationScoped
public class UserService {
private final UserRepository userRepository;
@Inject
public UserUseCase(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User findById(long id) { ... }
} -- Even though Quarkus magically adds a synthetic no-args constructor, how could it overcome the final field UserRepository? There would be a problem with its initialization as it needs to receive a value, I imagine it might end up being passed a null value.. Anyway, going straight to my case, here's an example of my current situation, with 3 important base classes and 1 implementation for a "Example" partner: PartnerIntegration.java @MappedSuperclass
public abstract class PartnerIntegration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
// other common fields ...
} -- PartnerIntegrationRepository.java public interface PartnerIntegrationRepository<I extends PartnerIntegration> extends PanacheRepository<I> {
// common methods ...
} -- PartnerIntegrationProvider.java public abstract class PartnerIntegrationProvider<I extends PartnerIntegration> {
protected final PartnerIntegrationRepository<I> partnerIntegrationRepository;
protected PartnerIntegrationProvider(PartnerIntegrationRepository<I> partnerIntegrationRepository) {
this.partnerIntegrationRepository = partnerIntegrationRepository;
}
// code ...
} -- And finally the final implementation ExampleIntegrationProvider.java: @ApplicationScoped
public class ExampleIntegrationProvider extends PartnerIntegrationProvider<ExampleIntegration> {
@Inject
public ExampleIntegrationProvider(ExampleIntegrationRepository exampleIntegrationRepository) {
super(exampleIntegrationRepository);
}
// code ...
} -- The abstract class But this generated that error, I tried creating a public constructor method that passes the null parameter to the parent class, and the error disappeared, but this ends up polluting my code, since I do not want to allow that class to be instantiated without receiving a repository, so I removed the ExampleIntegrationProviderBean.java @Dependent
public class ExampleIntegrationProviderBean {
private static ExampleIntegrationProvider INSTANCE;
private final ExampleIntegrationRepository exampleIntegrationRepository;
@Inject
public ExampleIntegrationProviderBean(ExampleIntegrationRepository exampleIntegrationRepository) {
this.exampleIntegrationRepository = exampleIntegrationRepository;
}
@Produces
public ExampleIntegrationProvider exampleIntegrationProvider() {
return INSTANCE == null ?
(INSTANCE = new ExampleIntegrationProvider(exampleIntegrationRepository)) : INSTANCE;
}
} But the number of partners may grow quickly, I wouldn't like to have to create other classes like this one to produce the instances, and even if it was just one partner, it doesn't seem like a very elegant solution... Does anyone have any ideas on how I can resolve this in a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Thank you @mkouba for the detailed explanation and @manovotn for complementing it. And @manovotn, if possible, I would like to express interest in this possible feature. |
Beta Was this translation helpful? Give feedback.
Correct.
But Quarkus does not have to overcome anything here - the final field is simply set in the constructor. And if
UserRepository
is produced by a@Dependent
producer then it could benull
. There's no issue.