Spring component-scan not scanning jboss server lib directory - spring

I have a jar file (kept in jboss-home/server/default/lib) and a war file (kept in jboss-home/server/default/deploy). The jar file contains a servlet which initializes the spring context. The servlet is initialized from the war file.
The problem is that the #Component (and #Service, etc) annotations in the jar file are not scanned. It gives NoSuchBeanDefinitionException error. I have declared the following in context xml.
<context:component-scan base-package="com.abc.mypack" />
I have also selected "Add directory entries" when building the jar using eclipse.
If I change to xml based configuration, it works. Or, if I move the jar file to WEB-INF/lib of the war file, it works.
Is there a way to scan the jboss server lib directory for components?
I am using spring 3.1 and Jboss AS 6.

Those annotations don't scan libraries. They scan packages, which means that the libraries containing those packages need to be on the classpath.
Most-likely the libraries you're referring to are on different classloaders.
I would suggest packaging the jar within the war's WEB-INF/lib directory.

Related

spring boot System.getProperty("java.class.path") doesn't include lib/ jars

I've got a spring boot app and I'm building a myApp.jar using the spring-boot antlib. When I jar -tf myApp.jar I see that I have a jar called lib/foo.jar. Yet when I print out System.getProperty("java.class.path") I don't see that jar file on the classpath. I also get a ClassNotFound exception from URLClassLoader when the code attempts to use this class for the first time. I'm using the JarLauncher since that's what the antlib defaults to.
Any ideas why this jar file would not be on the classpath?
You won't see a bundled JAR in System.getProperty('java.class.path'). The class path specifies where the JVM will look in the filesystem for classes you attempt to load.
Spring Boot uses fat JARs, which are loaded in a completely different way. Refer to the Spring Boot documentation.

mvn jboss:deploy fails with NoClassDefFoundError

When deploying an EJB project to JBoss using JBoss Maven Plugin, I get a NoClassDefFoundError on my JBoss' console about a class that is in one of the dependencies of this EJB project.
This dependency is declared with a Compile scope. Is there another scope that I should use so that my dependencies are also deployed to JBoss? Or how should I solve this?
The error looks like this:
DEPLOYMENTS IN ERROR:
Deployment "vfszip:/Users/hordine/projects/SoftBudget/soft-budget-ejb/target/soft-budget-ejb.jar/" is in error due to the following reason(s): java.lang.NoClassDefFoundError: br/com/pedra/j2eepatterns/facade/IEntityService
I understand that you have packaged the EJB as a jar file. This means there is no any provided dependency as same as a stand-alone jar.
AFAIU, there may be 3 possible ways as the following: -
Package them as an ear and put those dependencies inside the ear/lib. Please see Maven EAR Plugin for further information.
Package them as an war and put those dependencies inside the WEB-INF/lib. Please see Maven WAR Plugin for further information.
Package them as an jar and put those dependencies inside the classpath or application server lib. Please refer to your application server document.
EDITED:
If you are using the JavaEE 6, then it is possible to package the EJB as a war file. The The Java EE 6 Tutorial: Packaging Enterprise Beans in WAR Modules told us as
Enterprise beans often provide the business logic of a web application. In these cases, packaging the enterprise bean within the web application’s WAR module simplifies deployment and application organization. Enterprise beans may be packaged within a WAR module as Java programming language class files or within a JAR file that is bundled within the WAR module.
To include enterprise bean class files in a WAR module, the class files should be in the WEB-INF/classes directory.
To include a JAR file that contains enterprise beans in a WAR module, add the JAR to the WEB-INF/lib directory of the WAR module.
WAR modules that contain enterprise beans do not require an ejb-jar.xml deployment descriptor. If the application uses ejb-jar.xml, it must be located in the WAR module’s WEB-INF directory.
I hope this may help.

spring where to store xml config files

I'm developing a spring project and I've just encountered serious problems with loading XML config files within junit (but they are accessible from the web controllers) - can't load XML files.
I listed my classpath (in junit tests) and found that, among the rest, there are 2 directories included:
/var/www/Java/lyricsBase/build/web/WEB-INF/classes/
/var/www/Java/lyricsBase/build/test/classes/
There is just one file I want to include in my test:
/WEB-INF/lyricsBaseApp-servlet.xml
and it imports 3 files below:
/WEB-INF/hibernate.xml
/WEB-INF/dataSource.xml
/WEB-INF/beans.xml
I can clearly see that deploying my project on tomcat copies confg files to /var/www/Java/lyricsBase/build/web/WEB-INF/ABC.xml but this directory is not in the classpath, /var/www/Java/lyricsBase/build/web/WEB-INF/classes is included instead. I read that WEB-INF should not be in the classpath (it's just a Java EE principle). OK - so where should I put my config files to access them easily both from web controllers and junit tests? Any good practices?
Place the config files under WEB-INF/classes on the war package, and they will be loaded into the classpath. Consider deploying your code as an exploded war, so the config files can be changed easily.
Also consider using maven war project for your packaging. If you do, you can just place your config files under src/main/resources under the source code, and they will be packaged into WEB-INF/classes for you.
I had the same problem and from then on started putting them in META-INF under resources. This is where the spring template projects which don't run in Tomcat put them and seemed to solve my issues.

where to place applicationcontext.xml file in a jar (spring project)

I have created a spring project. I need to create a jar file of this project. I can able to create a jar file but applicationcontext.xml is placed outside when i open the jar. but where i need to exactly place the applicationcontext.xml in the jar, so that it gets loaded and I can use the spring bean inside the jar.
How to specify in pom.xml, that applicationcontext.xml should be placed inside the META-INF folder?
You need to place your applicationcontext.xml file under the src/main/resources/META-INF folder in your maven project.
I put it here -
src/main/resources/
and it worked

Spring AppContext in Maven

I am using Maven for creating my project structure. The following is the way I am doing
Generate Archetype
Have the following modules - Ear, War and WarSource (I am deleting the src, ejb and jar folders)
So my EAR will have 2 modules - War and WarSource which inturn have src/main/java and src/main/resource folders
Question is - where should my application context reside so that I avoid the File Not Found error during runtime.
Thanks
If you're using spring mvc it will be in the war src/main/webapp/WEB-INF/ directory. Also if you're using spring you may not even need an ear file, you should read up on that to be sure you're not adding unnecessary complexity.

Resources