Here is the code to do some tasks before web application is launched (I'm using glassfish4):
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.servlet.ServletContextEvent;
/**
*
* @author Ernestas Gruodis
*/
@Startup
@Singleton
public class ServerInit {
/**
* Do some code execution before web application starts up.
*/
@PostConstruct
public void init() {
System.out.println("Initialising");
}
/**
* Do some code execution on web application exit.
* @param sce the servlet context event.
*/
@PreDestroy
public void destroy(ServletContextEvent sce) {
}
}
But during startup I get these warnings:
WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
What is wrong here?
These warnings are caused by implicit CDI scanning in GlassFish. Resolve this by defining a beans.xml
file, where you set the bean-discovery-mode
to none
. For example, in a Maven project place beans.xml
under src/main/resources/META-INF/
, where your beans.xml
file might look like this:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="none">
</beans>
This will turn off scanning for your entire application so make sure to annotate properly where needed. You may also change the global settings of GlassFish to disable scanning for all apps using the asadmin
command:
asadmin set configs.config.server-config.cdi-service.enable-implicit-cdi=false
For more info on this I recommend reading Enable and disable implicit scanning per JAR in the glassfish issue queue in github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With