When I create a class annotated with @Context and run a Micronaut application, the @PostConstruct method works.
Example:
package com.example;
import io.micronaut.context.annotation.Context;
import javax.annotation.PostConstruct;
@Context
public class ClassHello1 {
@PostConstruct
public void sayHello() {
System.out.println("Hello from ClassHello1");
}
public void doSmth() {
System.out.println("Doing something...");
}
}
When I remove @Context annotation from the class ClassHello1 and create a bean with a scope @Context inside @Factory class, @PostConstruct method inside ClassHello1 doesn't work.
Example:
package com.example;
import io.micronaut.context.annotation.Context;
import io.micronaut.context.annotation.Factory;
@Factory
public class FactoryClass {
@Context
public ClassHello1 classHello1() {
return new ClassHello1();
}
}
-------
package com.example;
import javax.annotation.PostConstruct;
public class ClassHello1 {
@PostConstruct
public void sayHello() {
System.out.println("Hello from ClassHello1");
}
public void doSmth() {
System.out.println("Doing something...");
}
}
Even if I create another @Context bean and call a method doSmth() of ClassHello1 bean, @PostConstruct in ClassHello1 doesn't work anyway.\
package com.example;
import io.micronaut.context.annotation.Context;
import jakarta.inject.Inject;
import javax.annotation.PostConstruct;
@Context
public class ClassHello2 {
@Inject
private ClassHello1 classHello1;
@PostConstruct
public void init() {
classHello1.doSmth();
}
}
In this example doSmth() method of classHello1 bean is invoked, but annotated with @PostConstruct sayHello() doesn't work.
May you explain to me how can I instantiate ClassHello1 in @Factory class and make its @PostConstruct method work?
Thank you.