Skip to content

Commit 2c1ddb6

Browse files
authored
Add io.avaje.inject @PostConstruct/@PreDestroy annotations (#71)
1 parent 1d30568 commit 2c1ddb6

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

inject-test/src/test/java/org/example/coffee/factory/Configuration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import io.avaje.inject.Bean;
44
import io.avaje.inject.Factory;
5+
import io.avaje.inject.PostConstruct;
6+
import io.avaje.inject.PreDestroy;
57
import org.example.coffee.CoffeeMaker;
68

7-
import javax.annotation.PostConstruct;
8-
import javax.annotation.PreDestroy;
99
import javax.inject.Inject;
1010

1111
/**
@@ -41,7 +41,6 @@ BFact buildB(AFact afact, CoffeeMaker maker) {
4141
return new BFact(afact, maker);
4242
}
4343

44-
4544
@PostConstruct
4645
void initFactory() {
4746
countInit++;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.avaje.inject;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.METHOD;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* The <code>PostConstruct</code> annotation is used on a method that needs to be executed
12+
* after dependency injection is done to perform any initialization.
13+
* <p>
14+
* Note that we can equally use any <code>PostConstruct</code> annotation - so we can use
15+
* the one from <code>javax.annotation</code>, <code>jakarta.annotation</code> or this one.
16+
* </p>
17+
* <p>
18+
* Only one method in a given class can be annotated with this annotation.
19+
* <p>
20+
* The method on which the <code>PostConstruct</code> annotation is applied must fulfill
21+
* the following criteria:
22+
* <ul>
23+
* <li>The method must not have any parameters.</li>
24+
* <li>The method may be public, protected or package private.</li>
25+
* <li>The method must not be static.</li>
26+
* </ul>
27+
*/
28+
@Documented
29+
@Retention(RUNTIME)
30+
@Target(METHOD)
31+
public @interface PostConstruct {
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.avaje.inject;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.METHOD;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* The <code>PreDestroy</code> annotation is used on a method as a callback notification
12+
* to signal that the instance is in the process of being removed by the container.
13+
* <p>
14+
* Note that we can equally use any <code>PreDestroy</code> annotation - so we can use
15+
* the one from <code>javax.annotation</code>, <code>jakarta.annotation</code> or this one.
16+
* <p>
17+
* The method annotated with <code>PreDestroy</code> is typically used to release resources
18+
* that it has been holding.
19+
* <p>
20+
* The method on which the <code>PreDestroy</code> annotation is applied must fulfill the
21+
* following criteria:
22+
* <ul>
23+
* <li>The method must not have any parameters.</li>
24+
* <li>The method may be public, protected or package private.</li>
25+
* <li>The method must not be static.</li>
26+
* </ul>
27+
*/
28+
@Documented
29+
@Retention(RUNTIME)
30+
@Target(METHOD)
31+
public @interface PreDestroy {
32+
}

0 commit comments

Comments
 (0)