Cannot inject EJB specified in applicationcontext.xml in jboss 7 - java

I'm trying to migrate my Java EE application from jboss 5.1 to jboss 7.1 and getting a deployment error.
My EAR has 2 EJB modules and 1 WAR module. both EJB modules gets deployed without any issue. but when i'm trying to deploy with the WAR module it says
Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name "className" Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'java:global/ear/ejb/FacadeBean!com.ejb.business.SearchFacade' is defined: not found in JNDI environment
because of copyright issues i'm unable to disclose the full class names. but "classname" is defined as a bean in my spring application context file and it has a dependency injection of SearchFacade which again has some dependencies.
whats confusing is that in the jboss log it shows that SearchFacade is deployed without any issues.
i'm finding so hard to find a solution for this issue. any help will be greatly appreciated.
thanks in advance.

Looks like spring is not able to determine that the EJB component is in that EJB Jar and you need to enforce that the components in that EJB jar are initialized and started before the components in you WAR.
Did you tried to set element into your application*xml and see if that helped?
Something like this in your application.xml
<application-name>EAR</application-name>
<initialize-in-order>true</initialize-in-order>
<module><ejb>EJB.jar</ejb></module>
<module><web><web-uri>WAR.war</web-uri><context-root>....

Related

Application Deployment failing on cloud foundry due to entityManagerFactory bean creation failed

I am facing below error while deploying spring boot microservice on cloud foundry:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/ge/pd/reports/config/HibernateConfig.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
As I checked many resolution on stack overflow and many other portals for the solution, All the solution are related to jar version conflicts.
I have eliminated all the conflicts jar from the application and updated with all the jar versions which is required.
I am using below jar and its version:
hibernate-core-5.0.12.Final.jar - 5.0.12
hibernate-jpa-2.1-api-1.0.0.Final.jar - 2.1
hibernate-entitymanager-5.0.12.Final.jar - 5.0.12
openjpa-2.4.0.jar - 2.4.0
Please Provide the Solution.
This indeed looks like a dependency conflict, more specifically, between Hibernate 5.0.12 (which uses JPA 2.1) and OpenJPA 2.4.0 (which uses JPA 2.0). Try removing the OpenJPA dependency.
As this SO thread shows, the said method was introduced in JPA 2.1.

Spring Boot 2 Upgrade not able to resolve class name as property

We have an application where one of our internal libraries has defined a bean like this
<bean id="myBean" class="${myBean.type}"/>
We have a Spring Cloud Config Server which feeds properties to this application on startup, which also contains the property myBean.type. This setup is currently working fine with no issues. I then made the following change to my pom
Earlier
<spring.boot.version>1.5.16.RELEASE</spring.boot.version>
<spring.cloud.version>Edgware.RELEASE</spring.cloud.version>
<spring.version>4.3.19.RELEASE</spring.version>
Now
<spring.boot.version>2.0.9.RELEASE</spring.boot.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring.cloud.version>Finchley.SR2</spring.cloud.version>
Then I started getting this error on startup
An attempt was made to call the method org.springframework.beans.factory.support.BeanDefinitionBuilder.addConstructorArg(Ljava/lang/Object;)Lorg/springframework/beans/factory/support/BeanDefinitionBuilder; but it does not exist. Its class, org.springframework.beans.factory.support.BeanDefinitionBuilder, is available from the following locations:
jar:file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar!/org/springframework/beans/factory/support/BeanDefinitionBuilder.class
It was loaded from the following location:
file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.BeanDefinitionBuilder
Looking at this, I excluded the artifact org.apache.cxf:cxf-api:jar:2.7.18:compile from all the jars that depended on it & upgraded cxf jars versions to 3.2.5. Now the startup is going ahead but it is giving me the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${myBean.type}] for bean with name 'myBean' defined in class path resource [xyz.xml]; nested exception is java.lang.ClassNotFoundException: ${myBean.type}
I am not sure if removal of cxf-api is causing this issue, or upgrade of Spring Boot 2, or is it something else that is going wrong here!

Error in Bluemix Liberty when injecting a simple CDI managed bean into a servlet

When injecting a simple CDI managed bean into a servlet (starting from the generated code sample "SimpleServlet"), when accessing the servlet, Bluemix Liberty logs this error :
"SimpleServlet cannot be injected as class
wasdev.sample.servlet.SimpleServlet is not in a bean archive."
This is so even when a beans.xml file with discovery "all" is used.The target looks like this
Reason for Error :
The error is due to not having a beans.xml in the built application war i.e. JavaHelloWorldApp.war
Currently, for performance reasons, the beans.xml file is always required in order to use CDI on Bluemix.
Solution :
Copied the bean.xml inside the /src/main/webapp/WEB-INF/beans.xml then built the code.
This made sure that the bean.xml is packaged into the war file i.e. JavaHelloWorldApp.war and the error is resolved.
To verify bean.xml is deployed correctly to Bluemix, select the java application on Bluemix console, click and open the java application dashboard. Select Runtime, select Files tab, browse and verify that the bean.xml is deployed correctly inside WEB-INF as shown in the snapshot below.

Glassfish 3 EJB Injection from the jar archive fails

I have a strange problem with EJB injection in the glassfish 3. Maybe I just not completely understand what I do :)
So this is a problem: My project consists of 2 modules that will be assembled with gradle.
Module A
Module B
Module A is a usual glassfish module that also works fine. Module B contains general purpose staff. Module B is also a dependency of A. Module A will be deployed to glassfish as a *.war archive and Module B is in the appropriate lib folder as *.jar archive:
module-a.war and somewere inside of it ../lib/module-b.jar
What I want is: Create in the Module B a "general purpose" stateless bean and use it in the Module A. But it doesn't work...
In the Module B I created a bean:
#Stateless
public class GeneralPurposeBean {}
and I try to use it in the Module A as follows:
...
#EJB
private GeneralPurposeBean genPurpBean;
...
So how I already mentioned the GeneralPurposeBean is in the *.jar
Each time when I try to use the bean it fails with following exception:
javax.ejb.CreateException: Could not create stateless EJB
When I move the bean to the Module A it works fine but I want share this bean with other modules, that will be developed in the future. Can someone explain to me what is wrong here? So the bean will be recognized but it can't be created. What I found out through debugging is that
JCDIServiceImpl#_createJCDIInjectionContext
Doesn't recognize the bean as an enterprise bean. So everything in the *.war that directly accessible will be properly created but not what lies in the *.jar's.
For the case someone has the same problem:
If you treat one of your modules as a dependency and this module contains EJB beans you want be injected the solution for my problem was to put /META-INF/beans.xml file into the module. Otherwise container doesn't recognize the beans as EJB.
That's it.

Annotation-specified bean name conflicts with existing, non-compatible bean definition

I'm using Spring 2.5.4 and am creating a Java application that I'm deploying onto Weblogic.
I have a class in an external library (which included in the WEB-INF/classes directory of the resulting WAR file of my application) that I want to use in my code. I've created an instance variable for an object of the class in my code and added the #Autowired annotation and a getter and setter. In my application context file I have declared a bean of the library class' type and added the following:
<context:annotation-config />
<context:component-scan base-package="com.mycompany" />
... in order to register an AutowiredAnnotationBeanPostProcessor that will scan the classes and process the annotation.
When I try and deploy the application, I get the following error:
java.lang.IllegalStateException: Annotation-specified bean name 'myBean' for bean
class [com.mycompany.package.ClassName] conflicts with existing, non-compatible
bean definition of same name and class [com.mycompany.otherPackage.ClassName]
I think this is because there's a class in the library which has the same name as one in my application code (both class' package names start with "com.mycompany"). Nb. this is NOT the class that I have added, but a different one. Is there any way I can circumvent this problem without changing the name of the class in my application?
Thanks for any assistance.
Old question but throwing my 2c of bad experience with similar problem.
If you have 2 classes with same name, but in different packages was there a time when you had your other class referenced by the failing Spring context? If so, I'd recommend to clean the AS cached files (typically the place where the WAR is extracted), clean/rebuild your WAR and deploy again. Restarting the app server is also recommended.
I found that application servers and web containers alike (Weblogic, WAS, Jboss, Tomcat) tend to leave behind the old classes and when application is deployed those stale .class files are loaded in JVM via some old references, which most of the time messes up the Spring context loader.
Typical scenario is when you have renamed/moved a class from one package to another, or even kept the package name the same but moved it to another module (jar). In such cases cached (left over) files in the AS work directory can cause big headaches. Wiping out the work directory in your AS should resolve the issue outright.
You should use #qualifier to avoid this kind of conflict please refer section 3.9.3.
I fixed the problem by removing the autowiring completely and accessing the bean by explicitly creating a reference to it through the application context and the getBean() method.
This would better fit as a comment to #Pavel Lechev's answer, but I don't have enough rep to comment yet.
For other's finding this, here's what I did to solve this problem. I am using Wildfly 9.0.2.Final and, IntelliJ IDEA 2016.1.3 Build #IU-145.1617. These steps should presumably work with JBoss as well.
Stop Wildfly server.
Navigate to $WILDFLY_HOME/standalone/. Delete the three following folders: lib/, log/ and temp/.
In IntelliJ, Build > Build Artifacts > All Artifacts > Clean (or just the artifacts you are deploying).
In IntelliJ, Build > Rebuild Project
Restart Wildfly and redeploy your artifact(s).
These steps remedied my issue of duplicate bean names detected in the Spring context after refactoring a package name upstream from a couple of Controllers.

Categories

Resources