Load spring context EAR avoiding web.xml configuration - java

I have an enterprise application packaged as:
EAR
WAR1
WAR2
ejb.jar
lib/core.jar
lib/module1.jar
lib/module2.jar
...
lib/moduleN.jar
WAR packages are optional and every moduleX.jar contains a spring-bean.xml configuration file.
I need to load the spring context in the core.jar and share this context between WARs. Since i don't know how many modules are packaged, i need to load the xml files at runtime.
Thanks for help.

Related

Loading classpath resources that reside at the ear level

I have a resource defined at the ear level, such as {ear}/META-INF/somefile.txt.
I would like to load that file from a war in that ear using classpath resources (getResourceAsStream). I assume you can not do this, but wanted to check if there was a way to do that that i was unaware of.

Classpath in java and spring

I am majorly confused about where the classpath is. I understand when we create a spring mvc, resources folder, or inside web-inf is considered classpath. And we can use "classpath:" inside xml files to declare the folder. However, where is this classpath exactly? How is it set ? I have been reading about it for a long time, i still couldnt manage to get a real clear image in my head how the classpath is initially determined etc.
For example when we create a war file, and deploy it on a tomcat server, all the resource files can still be read via given paths with "classpath:" in the xml files. How does this work?
Thanks.
Ok, if it's web application, the classpath begins in WEB-INF/classes. Also, jar files in WEB-INF/lib are also on the classpath.
The Classpath is where the JVM will look for class files and other resources. Since you are using Spring MVC, I assume you are deploying a Web application (ie WAR file). This means that the classpath is set by the container which is following the Servlet spec.
The classpath for a WAR file includes the WEB-INF/classes and WEB-INF/lib folders. The Java EE/Servlet container where the WAR file is deployed will also include other common folders in the classpath.
Here is how Tomcat works.
You might also want to try this StackOverflow article/answer

Share ejb jar file with multiple wars in Wildfly

I'm aware that this is a very simple issue, but as I'm new to Wildfly I haven't had success trying to accomplish it.
In the old days of JBoss 4.2, when I wanted to share an ejb jar file with multiple war files I just deployed the ejb jar file to the application server and configured jndi in my war projects with a file 'jndi.properties' placed in some source directory in the war files, like this:
jndi.properties
---------------
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
Now, with Wildfly 8.1, this approach is not working anymore. I don't know if the contents of the jndi properties file changed or if I have to do something else. So, I ask: how do I share an ejb jar file with multiple wars in Wildfly 8.1 (I don't want to use ear files for this)?
Thank you in advance.
Marcos
PS.: Cross-posted: https://developer.jboss.org/thread/249133
just deploy the jar file and add adependency to your war file (either in Manifest.MF or in jboss-deployment-structure.xml from your war to your jar. Then you should be able to do JNDI-lookups using java:global/... or using CDI for injecting the beans using #Inject (for this approach you will need to activate CDI using beans.xml)
see also:
Wildfly class loading
Wildfly deployment descriptors
CDI reference
Application deployment

How to load a Properties File when the ClassLoader is in the EJB jar and the Properties File is in war

I have this problem. I want to load a properties file located in a war file but the class loader is in another jar file. But they are compiled in one ear file. When I place the properties file in src folder of the jar file's project, it works but I wanted to put it in Web-INF/Classes for future update purposes.
Details.
Compiled in EAR
EJB
IBM Websphere
Thanks!
You war depends on your EJB but the EJB does not depends on your war.
Thus, it's not possible.
If configuration must be updated, you should externalize it.
You won't be able to modify files in your war.
A good practice is to generate EJB jars and WARs independtly of the environment.
It will enable your Exploitation team to deploy the same code in every environment. They will just need to adapt the externalized property files (DB login / password, etc...).

WAR loads differently on weblogic when inside an EAR, why?

How does WebLogic 11g load libraries in an EAR file? I have this problem with a web application, that when deployed as a WAR (with libraries it depends on in WEB-INF/lib), it works just fine. However, when it's inside an EAR file, WebLogic does not find those libraries unless I put them in APP-INF/lib. Does that mean that if I'm deploying as an EAR I'd have to pull out all JAR files from the WEB-INF/lib directory and place them in APP-INF/lib ? or is there a configuration that can be done in WebLogic to avoid this?
Thanks!
If you have JAR files that you need to share between multiple WAR files or between WAR files and EAR files then you will need to package them in the EAR.
If WAR#1 has a JAR in its WEB-INF/lib and is packaged in an EAR with WAR#2, then WAR#2 will not be able to see the JAR files in WAR#1/WEB-INF/lib.
Solving your problem will take some understanding of how Java EE classloading works in a container. You should look at this link to get an understanding, but the basic problem is that when you package your application as an EAR, you've introduced another classloader (the application classloader) into the class loading hierarchy. You can configure WebLogic to load from your webapp by using the prefer-web-inf-classes element.

Categories

Resources