Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostConstruct method Direct Injection #752

Merged
merged 6 commits into from
Jan 6, 2025

Conversation

SentryMan
Copy link
Collaborator

generates the lookup and fetches beans for PostConstruct lifecycle methods. (Before we could only inject the BeanScope)

Given:

@Singleton
public class Minos {

  @PostConstruct
  void prepareThyself(Serpent serpent, Consumer<String> c, BeanScope b) {}
}

the following is generated:

@Generated("io.avaje.inject.generator")
public final class Minos$DI  {

  public static final Type TYPE_ConsumerString =
      new GenericType<Consumer<String>>(){}.type();

  public static void build(Builder builder) {
    if (builder.isBeanAbsent(Minos.class)) {
      var bean = new Minos();
      var $bean = builder.register(bean);
      builder.addPostConstruct(b -> $bean.prepareThyself(b.get(Serpent.class,"!serpent"), b.get(TYPE_ConsumerString), b));
    }
  }
}

@SentryMan SentryMan added the enhancement New feature or request label Jan 5, 2025
@SentryMan SentryMan added this to the 11.1 milestone Jan 5, 2025
@SentryMan SentryMan requested a review from rbygrave January 5, 2025 02:02
@SentryMan SentryMan self-assigned this Jan 5, 2025
@SentryMan SentryMan enabled auto-merge January 5, 2025 02:02
@rbygrave
Copy link
Contributor

rbygrave commented Jan 5, 2025

Why? What is the use case where this makes sense to do?

@SentryMan
Copy link
Collaborator Author

Well, we already accept a bean scope as a parameter, this just eases the process of using the existing feature.

@SentryMan
Copy link
Collaborator Author

SentryMan commented Jan 5, 2025

But to answer you directly, a use case could be to send an event to any @Observes listeners.

@rbygrave
Copy link
Contributor

rbygrave commented Jan 6, 2025

Well, we already accept a bean scope as a parameter, this just eases the process of using the existing feature.

Not quite in that the Builder isn't the same thing as the BeanScope. The BeanScope is the immutable collection of beans in the scope, and the Builder is expected to be lived for the duration of building the scope. That is, the side effect of this change is that there are lifecycle methods that register that keep a reference to the Builder.

@rbygrave rbygrave removed this from the 11.1 milestone Jan 6, 2025
@SentryMan
Copy link
Collaborator Author

SentryMan commented Jan 6, 2025

That is, the side effect of this change is that there are lifecycle methods that register that keep a reference to the Builder.

No?

 builder.addPostConstruct(b -> $bean.prepareThyself(b.get(Serpent.class,"!serpent"), b.get(TYPE_ConsumerString), b));

b here is the fully constructed beanscope, there's no reference to the builder.

We see this is the actual beanscope here

for (final var consumer : postConstructConsumers) {
consumer.accept(this);
}

@rbygrave
Copy link
Contributor

rbygrave commented Jan 6, 2025

b here is the fully constructed beanscope, there's no reference to the builder.

Ah cool !!

@rbygrave rbygrave added this to the 11.1 milestone Jan 6, 2025
@SentryMan SentryMan merged commit e6255b8 into avaje:master Jan 6, 2025
7 checks passed
@SentryMan SentryMan deleted the postConstructBeans branch January 7, 2025 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants