CDI Injection raises "WELD-001408" for multimodule project - java-ee-6

I have a project with some web artifacts (.war) and I'm facing problems to Inject simple POJOs. The following code represents the simplest working case:
.war (WAR1)
public class MyManagedBean {
#Inject
private MyPojo myPojo;
}
POJO in .war (WAR1)
public class MyPojo {
public void doSomething() {}
}
The problem occurs when I add one more web artifact (.war --> WAR2) with the same POJO (MyPojo) packaged. Semantically I want my POJO in each web artifact, not an external archive (.jar). Basically the newest added component (WAR2) is identical to WAR1, i.e., it simulates as if it existed a multimodule project. At runtime, container raises the following error when trying to use a module:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [MyPojo] with qualifiers [#Default] at injection point [[field] #Inject private com.projectx.MyManagedBean.myPojo]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:270) [:6.1.0.Final]
at org.jboss.weld.bootstrap.Validator.validateInjectionTarget(Validator.java:299) [:6.1.0.Final]
at org.jboss.weld.manager.InjectionTargetValidator.addInjectionTarget(InjectionTargetValidator.java:34) [:6.1.0.Final]
at org.jboss.weld.manager.BeanManagerImpl.createInjectionTarget(BeanManagerImpl.java:1055) [:6.1.0.Final]
at org.jboss.weld.manager.BeanManagerImpl.fireProcessInjectionTarget(BeanManagerImpl.java:1249) [:6.1.0.Final]
at org.jboss.weld.integration.injection.Jsr299SimpleNonContextualInjector.inject(Jsr299SimpleNonContextualInjector.java:66) [:6.1.0.Final]
at org.jboss.injection.manager.core.DefaultInjectionContext.proceed(DefaultInjectionContext.java:58) [:1.0.0-alpha-7]
at org.jboss.injection.manager.core.DefaultInjectionManager.inject(DefaultInjectionManager.java:58) [:1.0.0-alpha-7]
at org.jboss.injection.manager.core.DefaultInjectionManager.inject(DefaultInjectionManager.java:64) [:1.0.0-alpha-7]
at org.jboss.web.tomcat.service.TomcatInjectionContainer.processInjectors(TomcatInjectionContainer.java:410) [:6.1.0.Final]
at org.jboss.web.jsf.integration.injection.JBossDelegatingInjectionProvider.inject(JBossDelegatingInjectionProvider.java:77) [:1.0.3]
... 66 more
Some scenarios details:
If I remove WAR2, the injection made by WAR1 starts to work.
Both of artifacts (WAR1 and WAR2) have the same POJOs and beans.xml (/WEB-INF).
Is there any point am I missing? All I need is the possibility to inject a class, like a "*.Helper" for example. As I know, CDI allows to Inject even primitive types.
JDK 1.7
JBoss 6.1.0
Weld 1.1.Beta2

Unfortunately using JBoss 6.1.0 AS packaged with Weld 1.1.Beta2 really doesn't work. As a test, tried the same implementation with Glassfish 3.1.2 and CDI injection worked like a charm.

Related

Wildfly 14 CDI: WELD-001408 on 3rd part JAR after migration from JBoss 7

We are migrating a few old systems written in Java 5, 6 and 7 to Java 8 and from JBoss 7 to Wildfly 14.
After the migration, I get all the time this kind of error:
WELD-001408: Unsatisfied dependencies for type InterfaceTypeConverterProvider with qualifiers #Named
I understand that from CDI 1.2 things changed and the #Inject don't work as it used to be and it need some refactoring.
I got many errors like this, some of them are classes in my own project that try to Inject other classes also inside my project, those ones I can fix.
The problem is: My project loads some classes that tries to inject other classes from outside, that are jar dependencies that I have no control over it and I can't change the code on those jars.
For example:
11:15:54,552 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.deployment.unit."myApp-war-9.2-JAVA8-SNAPSHOT.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."myApp-war-9.2-JAVA8-SNAPSHOT.war".WeldStartService: Failed to start service at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1728)
at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1556)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.weld.exceptions.DeploymentException: Exception List with 46 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type InterfaceTypeConverterProvider with qualifiers #Named
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] #Inject public thirdPartJar.converter.context.AbstractConverter.setTypeConverterProvider(#Named InterfaceTypeConverterProvider)
at thirdPartJar.converter.context.AbstractConverter.setTypeConverterProvider(AbstractConverter.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
- Managed Bean [class thirdPartJar.converter.context.TypeConverterProvider] with qualifiers [#Any #Default]
The error shows the problem in the class thirdPartJar.converter.context.AbstractConverter and I can't touch the code on that one... so, what should I do ?
Is it possible to downgrade Wildfly Weld or force it to use an older version of CDI ?
This is my beans.xml with the discover all but it still can't find the implementations.
<?xml version="1.0" encoding="UTF-8"?>
<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"
version="1.2" bean-discovery-mode="all">
</beans>
The beans.xml always only applies to the archive it's in (be it jar or war). So you're setting the bean-discovery-mode only for your own archive, not for the 3rd party one.
Easiest option: Repackage the 3rd party jar and include a fitting beans.xml.
Non-intrusive option: Write a producer that supplies the expected beans. Normally, this can be a simple method:
#Produces
#Named
public InterfaceTypeConverterProvider createInterfaceTypeConverterProvider() {
return new InterfaceTypeConverterProvider();
}
I take it that there is no injection within the class. Otherwise it should already have been cdi enabled.

Gettting WELD Exception on server startup of weblogic where as using Google Guice for DI in Jersey based application

I am using Weblogic 12b as App server. My application uses Jersey 2.5.1 with Guice3 in my project. I have a class called Application derived from org.glassfish.jersey.server.ResourceConfig. On server startup I am getting error as below:
Caused By: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ServiceLocator with qualifiers #Default
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] #Inject public Application(ServiceLocator)
at Application.<init>(Application.java:22)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateProducer(Validator.java:417)
at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:36)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.validate(InjectionTargetFactoryImpl.java:135)
It seems it is taking WELD in place of google Guice for DI.
Same issue I am getting in business Tier where EJB classes are composed of Java Classes and they are injected using #Inject.
I have even tried to change he import #Inject to google inject but the exception changed but not resolved.
I tried to use beans.xml in web-inf
#ApplicationPath("/")
public class Application extends ResourceConfig {
#Inject
public Application(final ServiceLocator serviceLocator) {
}
}
You need to effectively disable CDI.
You can do this by adding a WEB-INF/beans.xml file to your application that contains:
<?xml version="1.0" encoding="UTF-8"?>
<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>
Note the bean-discovery-mode="none".
If your project contains additional jars with classes that might look CDI beans then you will also need to add a similar META-INF/beans.xml file to those as well.
However, I suspect that this may lead to other unrelated issues. Normally application servers like to control the lifecycle of your classes and this includes those related to JAX-RS.

Spring boot jackson auto configuration linkage error wildfly

A.war -> Is a simple spring boot REST app using version 1.2.6.RELEASE ( internally using spring 4.1.7 RELEASE, jackson 2.4.6 )
B.jar -> Is a wildfly9 shared module (stored in wildfly/modules) which is packaged using maven shaded plugin containing spring framework (without spring boot meaning using the old way of adding spring projects dependencies) and jackson classes of same version as above. (Its shaded because non-spring and non-container projects can just add this jar in their class-path and use it - it works)
A.war has maven dependency scope <provided> on B.jar and has jboss-deployment-structure.xml with <dependencies> <module name = "B"> </dependencies>
A.war deployment fails with below error.
Note: A.war deploys perfectly fine when i remove B.jar jboss dependency. So adding B.jar is causing this issue. And If i don't use spring boot in A.war (meaning if i use simple spring webmvc and context dependencies directly) with B.jar jboss dependency, A.war deploys perfectly too
Can anyone explain me what the error below says and how can i investigate more ?
As far as my understanding on wildlfy classloading, A.war and B.jar should be loaded in different module class loaders, Is A.war complaining about jackson classes of B.jar here in the error ?
Caused by: java.lang.LinkageError: loader constraint violation: when resolving method
"org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.modulesToInstall([Lcom/fasterxml/jackson/databind/Module;)Lorg/springframework/http/converter/json/Jackson2ObjectMapperBuilder;"
the class loader (instance of org/jboss/modules/ModuleClassLoader) of the current class,
org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration,
and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class,
org/springframework/http/converter/json/Jackson2ObjectMapperBuilder,
have different Class objects for the type der used in the signature
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration.configureModules(JacksonAutoConfiguration.java:259)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration.jacksonObjectMapperBuilder(JacksonAutoConfiguration.java:186)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration$$EnhancerBySpringCGLIB$$b993caa0.CGLIB$jacksonObjectMapperBuilder$1()
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration$$EnhancerBySpringCGLIB$$b993caa0$$FastClassBySpringCGLIB$$5262bf2.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
at org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration$$EnhancerBySpringCGLIB$$b993caa0.jacksonObjectMapperBuilder()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 108 more
After investigating more jboss classloading behavior, jackson classes inside B.jar are conflicting with same jackson classes from jboss restesay module.
so after excluding them in jboss-deployment-structure.xml as below this problem is fixed..
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider"/>
<module name="org.jboss.resteasy.resteasy-jackson2-provider"/>
</exclusions>

Jboss as7 to WildFly migration weld unsatisfied dependencies for type

I'm trying to migrate a jboss as 7 application to WildFly.
For some reason I am getting the weld unsatisfied dependencies for type Set.
For just about every class that uses the #Inject (Using guice).
I can not think of any way for solving this and have been stuck for several hours now.
Exception 0:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type 'classNameHere' with qualifires #Default
at injection point [BackedAnnotatedField] #Inject cant.tell.du.services.data.syly.nbp.syll.gkfsn
at cant.tell.du.services.data.syly.nbp.syll.gkfsn(syll.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProbloms(Validator.java:368)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:289)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:135)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:166)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:514)
Ok, now I got the point that you are using guice instead of cdi.
You could try to exclude the weld subsystem from your deployment like so:
<exclude-subsystems>
<subsystem name="weld" />
</exclude-subsystems>
This should disable CDI for your deployment and you should not run into the same problems anymore.

How to debug NoSuchMethodError exception ? org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)

When I try to initialize a Hibernate 4 SessionFactory in a servlet:
Configuration config;
ServiceRegistry registry;
SessionFactory factory;
config = new Configuration();
config = config.addAnnotatedClass(Star.class); // <-- Exception here.
I get an exception:
SEVERE: Servlet.service() for servlet [hu.adamsan.store.TestHibernate] in context with path [/TestProject] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at hu.adamsan.store.TestHibernate.doGet(TestHibernate.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at
When I looked up in Google, I could not find a solution, but similar errors were present when there was a mismatch with the hibernate version. I've made a similar project, with the exact same maven dependencies, with the same code initializing hibernate in a doGet method, and it worked.
maven dependencies:
mysql-connector-java 5.1.26
hibernate-core 4.2.6.Final
hibernate-validator 5.0.1.Final
commons-beanutils 1.8.3
commons-collections 3.2.1
log4j 1.2.17
slf4j-api 1.7.5
Does anyone have an idea, what could be wrong?
Can someone offer general advice, how to handle, debug such obscure errors?
When you get a NoSuchMethodError in external libraries, it is usually due to having multiple versions of the same dependency on the classpath. This might be due to a transitive dependency, so detecting it through just looking at the dependency in the pom.xml of your project is not enough. If you do a mvn dependency:tree, it will list all transitive dependencies as well, so look for multiple versions of the same dependency.
It might also be that the same class is implemented is multiple dependencies, so google it to find out which jars contain it. http://www.jarfinder.com/index.php/java/info/org.hibernate.cfg.Configuration