Skip to content

Add io.avaje.inject @PostConstruct/@PreDestroy annotations #71

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

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import io.avaje.inject.PostConstruct;
import io.avaje.inject.PreDestroy;
import org.example.coffee.CoffeeMaker;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

/**
Expand Down Expand Up @@ -41,7 +41,6 @@ BFact buildB(AFact afact, CoffeeMaker maker) {
return new BFact(afact, maker);
}


@PostConstruct
void initFactory() {
countInit++;
Expand Down
32 changes: 32 additions & 0 deletions inject/src/main/java/io/avaje/inject/PostConstruct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.avaje.inject;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* The <code>PostConstruct</code> annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization.
* <p>
* Note that we can equally use any <code>PostConstruct</code> annotation - so we can use
* the one from <code>javax.annotation</code>, <code>jakarta.annotation</code> or this one.
* </p>
* <p>
* Only one method in a given class can be annotated with this annotation.
* <p>
* The method on which the <code>PostConstruct</code> annotation is applied must fulfill
* the following criteria:
* <ul>
* <li>The method must not have any parameters.</li>
* <li>The method may be public, protected or package private.</li>
* <li>The method must not be static.</li>
* </ul>
*/
@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}
32 changes: 32 additions & 0 deletions inject/src/main/java/io/avaje/inject/PreDestroy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.avaje.inject;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* The <code>PreDestroy</code> annotation is used on a method as a callback notification
* to signal that the instance is in the process of being removed by the container.
* <p>
* Note that we can equally use any <code>PreDestroy</code> annotation - so we can use
* the one from <code>javax.annotation</code>, <code>jakarta.annotation</code> or this one.
* <p>
* The method annotated with <code>PreDestroy</code> is typically used to release resources
* that it has been holding.
* <p>
* The method on which the <code>PreDestroy</code> annotation is applied must fulfill the
* following criteria:
* <ul>
* <li>The method must not have any parameters.</li>
* <li>The method may be public, protected or package private.</li>
* <li>The method must not be static.</li>
* </ul>
*/
@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface PreDestroy {
}