I'm new in here and also new to the CDI world, and the first task I got in my job was to find out a way to controlled CDI uploading.
We are using both EJB 3.1 and CDI 1.0, and because they are controlled by different containers, we can control when and in what order the EJB Managed Beans will be up by using @Startup and @Singleton annotations.
But the @Inject CDI bean I have declared in my class is coming as null since the CDI Container hasn't started yet.
I have been trying for several days now to look up for solutions and the one I found here did not worked (still came as null).
We are using Java EE 6 and running the application on WebSphere Application Server 8.
Please, if you could help me find a way to control CDI uploading inside and regardless of the EJB?
here is a sample code of it:
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Singleton
@Startup
public class BaseStartupLoader{
/**
* Default constructor.
*/
@Inject @MyStartup
BaseStartUp myStartup;
private static Logger m_logger = LoggerFactory.getLogger(BaseStartupLoader.class);
public BaseStartupLoader() {
}
@PostConstruct
public void init(){
String applicationName = null;
try {
applicationName = myStartup.getClass().getName();
myStartup.load();
} catch (IllegalAccessException e) {
m_logger.error("Faild to load data into preload system. "+e);
} catch (InstantiationException e) {
m_logger.error("Faild to load data into preload system. "+e);
} catch (ClassNotFoundException e) {
m_logger.error("Faild to load data into preload system - Class "+ applicationName + "Not found. "+e);
}
}
}
Here is the BaseStartup Interface:
public interface BaseStartUp {
public void load() throws IllegalAccessException, InstantiationException, ClassNotFoundException;
}
The Qualifier and Implementation:
@Retention(RetentionPolicy.RUNTIME)
@Target ({ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
@Qualifier
@Dependent
public @interface MyStartup {
}
@MyStartup
public class MyStartUpLoader implements BaseStartUp {
@Inject
SomeConfigLoader config;
@Override
public void load() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
conifg.init();
}
}