Lookup a EJB Bean using JNDI - java

I trying inject a EJB into my class using a InitialContext (JNDI). For this i use a Netbeans insert code mechanism:
after that Netbeans know witch injection have to use. #EJB annotation or JNDI Lookup. In my Example i have a simple, not managed class and what i want to do is inject a EJB Bean using JNDI. So Netbeans generate code for me as bellow:
problem is that. When Netbeans generate code for me. He change a web.xml file and add there ejb-local-ref node:
and when i trying a turn on my web application. I run glasfish and i always get following error:
Exception while deploying the app [mavenproject1-ear] : Error: Unresolved <ejb-link>: mavenproject1-ejb-1.0-SNAPSHOT#LanguagesFacade
i really dont know what to do. Can someone help with this issue. I will greatful for help.

Just remove the <ejb-local-ref> all together. It's defined for dependency injection and this is not your case here since you use JNDI to lookup the bean and set it in langaugeFacade variable. Just remove it and things will be fine.

Related

CGLIB errors converting a weblogic spring web app to springboot app

I am attempting to convert an existing spring weblogic application to a spring boot embedded tomcat application.
There are lots of moving parts so it's hard to show any code, I'm hoping there is some general answer that might clue me in to the issue.
Under weblogic, using the spring-framework 4.3.6.RELEASE libraries, the application deploys fine. It has no problems creating the different service, repository and component beans.
However, when I migrate it to Spring Boot 1.5.1.RELEASE, I get the following error:
2017-06-21 17:08:16,402 [ERROR] SpringApplication reportFailure (815) - Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'alertEventServiceImpl': Unsatisfied dependency expressed through field 'alertEventDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'alertEventDaoImpl' defined in URL [jar:file:/Users/username/Development/source/carma-war/target/carma-war-2.0.0-SNAPSHOT.war!/WEB-INF/lib/protocol-manager-1.8.0-SNAPSHOT.jar!/org/ihc/hwcir/protocol/dao/AlertEventDaoImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class org.ihc.hwcir.protocol.dao.AlertEventDaoImpl
Many of our service classes are final as they shouldn't be extended. Since there are so many that are final, I wanted to minimize the amount of code in our different libraries that we modify to make this work.
I thought because the bean creation process works under weblogic, it should work under spring boot.
Things I have tried to force not using the cglib proxies:
All implementations implement interfaces already
In beans created via xml, added <aop:scoped-proxy proxy-target-class="false"/>
In beans created through annotations, added (example for service bean)
#Service
#Scope(proxyMode = ScopedProxyMode.INTERFACE)
However, in the end, I'm perplexed as to why spring can create beans (the classes marked as final) under the weblogic container but unable to do so under the embedded tomcat spring-boot container.
Spring Boot by default uses class based proxies, which will not work with final classes/methods.
To disable this add spring.aop.proxy-target-class=false to the application.properties to enable JDK Dynamic Proxies instead of class based proxies. (And revert your modifications).
NOTE: To have everything take into account the spring.aop.proxy-target-class you might need to upgrade to Spring Boot 1.5.3 as some final patches where made to include this property in parts that were missed in previous versions.
See the following issues for more information 8434, 8869 and 8887.
I was unable to make this work using M. Deinums' answer using spring.aop.proxy-target-class=false.
What worked for me was to add in the application.properties file
spring.dao.exceptiontranslation.enabled=false
Please note that this option disables proxy creation for repositories.
And in my spring boot application configurer the annotation to handle transactions without using a proxy class.
#EnableTransactionManagement(proxyTargetClass = false)
This is using Spring Boot version 1.5.1.RELEASE.

refresh property placeholder mechanism at Spring

I have a Spring context xml file and a specific bean into it that created by a PropertyPlaceholderConfigurer mechanism.
My problem is: When the application is working related properties file changes and I don't want restart application. I want reload related bean without restart application.
What is best solution for my goal?
you can have a look at ReloadablePropertiesAnnotation on github https://github.com/jamesemorgan/ReloadablePropertiesAnnotation
this should just work by annotating your property like
#ReloadableProperty("dynamicProperty.myProperty")
private String myProperty;
you'll have to add some spring configuration as well see the example on github or in this blog http://www.morgan-design.com/2012/08/reloadable-application-properties-with.html
Under the hood, this is using Guava's EventBus to update the properties of your beans after the bean has been created.

How to use Spring Data JPA in a simple Servlet?

I'm very new to Spring, but I am working on a project which is using Spring Data JPA to generate repositories for JPA entities.
I'm currently adding a simple module to be able to show some data on a webpage. I have added a Servlet, but I am having trouble accessing the repositories from there.
I have added a ContextLoaderListener in web.xml, I'm referencing the jpa:repositories and persistence.xml in the applicationContext.xml, but I'm currently stuck with this exception:
No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.
But when I add an EntityManagerFactory in persistence.xml I get the following cryptic message:
java.lang.IllegalAccessError: tried to access field
org.hibernate.engine.spi.CascadeStyle.STYLES from class
org.hibernate.engine.spi.EJB3CascadeStyle
My question is: is what I am trying to do even possible? And if so, how?
Or should I just bite the bullet and use Spring MVC or something else entirely?
Note: this is just for a one-page web site and I'm trying to keep it as simple as possible.
In order to use Spring Data JPA you need to configure the underlying JPA implementation as you would typically do in Spring, see for example, infrastructure.xml and META-INF/persistence.xml in spring-data-jpa-showcase (since Spring 3.1 you can get rid of persistence.xml if you use packagesToScan property of LocalContainerEntityManagerFactoryBean).
Your second problem with IllegalAccessError looks like a classloading problem caused by presence of different versions of Hibernate jars in classpath.

Can't Lookup Weblogic11

I am using Weblogic 11g, EJB3.0.
I am trying to do simple look up from one deployment to another in the same machine. But no success.
This is the code:
In one deployment this is the target class:
#CallByReference
#Stateless (mappedName = "ejb/SyncOperatorsBean")
#Local ({SyncOperatorsBeanLocal.class})
#Remote ({SyncOperatorsBeanRemote.class})
#JNDIName("ejb/SyncOperatorsBean") //added
public class SyncOperatorsBean implements SyncOperatorsBeanLocal,SyncOperatorsBeanRemote
...
Now in the second deployment, this is how I do the lookup in order to reach the first deployment:
SyncOperatorsBeanRemote SyncOperatorsBean = (SyncOperatorsBeanRemote) context
.lookup("ejb/SyncOperatorsBean#com.mirs.sbngenerate.beans.SyncOperatorsBeanRemote");
SyncOperatorsBean.executeSyncOperation();
That's the exception:
javax.naming.NameNotFoundException: While trying to lookup 'ejb.SyncOperatorsBean#com.mirs.sbngenerate.beans.SyncOperatorsBeanRemote' didn't find subcontext 'SyncOperatorsBean#com'. Resolved 'ejb'; remaining name 'SyncOperatorsBean#com/mirs/sbngenerate/beans/SyncOperatorsBeanRemote'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
Now I can see the bean SyncOperatorsBean in the console's JNDI TREE. But still have the above exception.
I can't use Injection since the calling class is out of the container (inside quartz job).
Any idea?
Take a look at the JNDI tree for the managed server(s) where the EJB is deployed and see if you can find it. Also, are you using the URL of the Admin Server or the managed server where the EJB is deployed?
I have fixed the problem by taking off the #JNDI annotation.
and adjust the lookup command like that:
SyncOperatorsBeanRemote SyncOperatorsBean =
(SyncOperatorsBeanRemote) context
.lookup("ejb/SyncOperatorsBean#com.mirs.sbnsync.beans.SyncOperatorsBeanRemote");
SyncOperatorsBean.executeSyncOperation();
more over I had to add the target class jar to the server lib dir.(weird but thats what I had to do)

Error injecting entity manager with Wicket/JPA

I have an app using Wicket for the presentation layer with CDI/Weld, JPA 2.0, EJB 3.1 etc. (Java EE 6) deployed on GlassFish v3.0.1.
When I try to inject an EJB into a wicket page using #EJB I get the following error:
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName
When I try to inject using #Inject, I get the following error:
java.lang.IllegalStateException: Unable to convert ejbRef for ejb UserRepository to a business object of type class
I believe the problem is stemming from JPA. I am using the exact same configuration that I used with a JSF application which worked properly, so I am lost as to what the issue could be. The connection pools are set up properly and pinging correctly through GlassFish, I have included wicket-weld on the classpath and I have even tried using the old Java EE 5 wicketstuff project for wicketstuff-javaeeapi with the same results.
Any help would be appreciated.
If you get the exception:
Unable to retrieve EntityManagerFactory for unitName
It might mean it is not detecting your persistence.xml file. Make sure it's in the WEB-INF\classes\META-INF directory.
You can verify that your app has JPA enabled by going to the Admin Console in GlassFish, go to the Applications section and see if it shows something like [ejb, web, weld, jpa] for your app. If it doesn't show jpa then it's not finding your JPA config file.
Could always try to lookup the EJB via its standard "java:global" name. That should at least let you rule out wicket as a possible source of issues and get you a little closer to a working system.

Categories

Resources