I just start a CDI project. In this project a Beans2 is injected inside a Beans1. But the Beans2 has a method that create a file. This method instantiates the File Object like this :
new File('myPathFile');
Because this instantiation is not managed by the CDI container, Bean2 is not injected into Beans1. I try to make a producer to inject a File into the Beans2 but do i need to do the same thing for all java base class that i will use?
Is there another solution to simply use class that do not need to be inject?
Bean1 :
@Dependant
public class Bean1 implements Serializable {
private @Inject @Bean2Producer Bean2 bean2;
public void someMethod() {
bean2.foo();
}
}
Bean2 :
@Dependant
public class Bean2 extends AbstractClass implements Serializable {
private @Inject @PathDir String pathDir;
public Bean2(String param1, boolean param2) {
super(param1, param2);
}
public void foo() {
File file = new File(pathDir);
}
}
pathDir Producer :
@ApplicationScoped
public class ProjectProducer implements Serializable {
@Produces
@PathDir
public String getPathDir() {
try {
return PropsUtils.geetProperties().getProperty(PATH_DIR);
} catch (Exception e) {
e.printStackTrace();
}
}
}
PathDir annotation :
@Qualifier
@Target({FIELD, METHOD, PARAMETER, CONSTRUCTOR, TYPE})
@Retention(RUNTIME)
@Documented
public @interface PathDir {}
In this example, PathDir is not Inject if new File is invoke in foo() method.