Hot questions for Using GlassFish in cdi

Question:

I'm doing and upgrade from Glassfish 3.1.2.2 to Glassfish 4.1 for a set of Spring applications. Since I use the Spring to handle @Inject annotations, I have disabled Glassfish' CDI using this command:

asadmin set configs.config.server-config.cdi-service.enable-implicit-cdi=false

Still, when I deploy one of my applications, I get the following error message:

The lifecycle method [something] must not throw a checked exception.
Related annotation information: annotation [@javax.annotation.PostConstruct()] 
on annotated element [public void com.something.MyClass.something() throws 
java.io.IOException] of type [METHOD]. Please see server.log for more details.

The class in question is an abstract class with no implementations in the application that I'm trying to deploy, it's just something that is on my classpath.

Why is Glassfish validating my @PostConstruct when I've disabled CDI? Why is Glassfish validating @PostConstruct on something that can not become a bean? How can I prevent Glassfish from interferring with anything that I'm using Spring for?


Answer:

Annotation @PostConstruct is a general annotation used in any dependency injection mechanism. The Javadoc explicitely states that, unless used within an interceptor, it must be put on a method, which has void return type and throws no checked exceptions.

It is weird that Spring allows checked exceptions on post-construct methods, as there is not way how to handle them. But as this requirement is only a validation and can be ignored, Spring probably ignores checked exceptions and Glassfish does not. There is possibly an unnecessary Glassfish feature, that it scans and validates all classes, even if not used in CDI or any other mechanism (EJB, ...)

The best is to remove checked exceptions to align the code with the documentation and make it portable.

Question:

Problem: I keep getting the WELD exception below when trying to deploy my WAR to Glassfish 4.1.1. The application deploys properly on Wildfly 9.0.

Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.jboss.weld.exceptions.DefinitionException: WELD-001508: Cannot create an InjectionTarget from [EnhancedAnnotatedTypeImpl] public abstract interface @Path class com.example.package.MyService as it is an interface

Here, MyService is indeed an interface, defined in a separate library. My application pulls the library in so it can create a proxy against the MyService interface and make requests. MyService looks like:

@Path("api")
public interface MyService {

    @GET
    public void someMethod();
}

I tried excluding MyService from WELD by adding the following to the beans.xml file, to no avail :(

<scan>
    <exclude name="com.example.package.*"></exclude>
</scan>
  • Why is WELD trying to include that library's interface?
  • How do I tell it to ignore it?

Answer:

You can also try to create package-info.java file and use @Vetoed:

@Vetoed
package com.example.package;

import javax.enterprise.inject.Vetoed;