How to load resource file in a dependency jar contained in the Spring Boot jar file? - spring-boot

When packaging the Spring Boot application as a single jar, all the dependency jars are also contained in that jar file. It forms a hierarchical structure of jars. How can I access to the resource file(such as a xml) that built in one of the dependency jar without unpackaging them?

If dependency jar is in your classpath, you can read XML files like:
URL loadedResource = this.getClass().getClassLoader().getResource(“your.xml”);
InputStream inputStream = loadedResource.openStream();
OR
Resource resource=new CLassPathResource(“your.xml”);
resource.getInputStream();
Follow this stackoverfow thread
If it is properties file you want to read, you can use #PropertySource annotation:
#PropertySource("classpath:your.properties")

Related

Dependent JAR: cannot find properties file

I have a library using Spring and loading its own properties file
#Configuration
#PropertySource("classpath:connectors.properties")
When using this JAR as a Maven dependency, and putting that class under scanning, I get the following error:
java.io.FileNotFoundException: class path resource [classpath:connectors.properties] cannot be opened because it does not exist
What is the proper way in this case?
Thanks
Actually as pointed out by #Barath, the file had not been packaged in the dependent JAR, as it was present in src/test/resources and not src/main/resources

my application.properties from dependency is not available in spring boot application

I have spring boot application.
In this spring boot (fat-jar) application I have classpath:config/application.properties and classpath:application-env.properties.
I am starting this application using command:
java -Dspring.profiles.active=env -jar application.jar --some-parameter=some-value
Question: Why application.properties file is not available during initialization this application from classes (beans) localized in my dependency?
This file (application.properties) is localized in classpath:application.properties of my dependency.
I am expecting some properties with some specific prefix - using annotation:
#ConfigurationProperties(prefix = "someprefix")
and again the same in other words:
Application A have classpath:config/application.properties
+ Application A have/is using dependency to some artifact D
dependency (jar) D.jar have classpath:application.properties
this application.properies is not available during initialization of bean B with annotatin #ConfigurationProperties(prefix = "someprefix")
according to 24.3 Application property files "The classpath root" should be fine - but it is not.
This is fat-jar application - in one big jar we have everything - including folder 'lib' with all dependencies.
OK, :(
solution of my issue was trivial. My application was loading different classpath:/application.properites than I thought.
I had another dependency jar introduced to my project not-directly (it was dependency for my dependency) which contains different classpath:/application.properites.
In other words everything works fine but only one 'classpath:/application.properties' is loaded even you have two in two different dependencies.
I used /actuator/env to find out which classpath:/application.dependency was loaded.

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.

Spring component-scan not scanning jboss server lib directory

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.

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