Dependent JAR: cannot find properties file - spring

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

Related

How to include a standalone jar that uses spring and beans as a dependency in maven project for jenkins plugin?

I want to include a standalone jar file (which uses spring), inside my java code to develop a jenkins plugin. When I add this as a dependency and I run the plugin ,I get errors like: Caused: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/XXX/YYY/jmsclient3/XXXclient-jms-config.xml]
It says cant find the class path resource [com/XXX/YYY/jmsclient3/XXXclient-jms-config.xml], because it doesnt exist.
But this file is clearly there in the jar. Its a bean file(I dont know much about it).

Could not open ServletContext resource in Spring boot application [duplicate]

I have a Spring Boot application using Google Pub Sub API. I need to inject Google credentials and other properties using file credentials.json. I put the file in my src/main/resources (otherwise, it will not put the file in the built jar) like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/credentials.json
However, when I build the jar, this file is placed in the root directory and this path is no longer valid. So I am able to run my application from Eclipse, since by the that time, the file is still in my resources directory but I can't run it as a standalone jar after built because the path is suddently just file:credentials.json.
Is there some easy way how to specify the path as relative so it works both in IDE and when running my jar? I can inject the path through env. variables but I would do so only if absolutely necessary.
If you use the classpath prefix then Spring will look for the file on your classapth.
If you put the file in src/main/resources then Maven will, by default, copy it to the root of your classpath and it will then be addressable as follows:
spring.cloud.gcp.credentials.location=classpath:credentials.json
This should hold true whether ...
You are running in your IDE; your IDE's Maven integration will copy the file from src/main/resources to the root of your classpath - typically target/classes
You are running a built JAR; Maven will copy the file from src/main/resources to the root of your JAR

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

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")

Eclipse indirectly referenced .class file

When i build the project i am getting this error
The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirectly referenced from required .class files
Vaelyr answers above is correct
but To be very specific, you need to add spring-core-4.0.5.RELEASE.jar into your eclipse build path.
Use the spring version that corresponds to your other spring jars.
The class you are trying to use is not apparently on your classpath. Add your Spring jar files to your sources classpath and it should work. If that's not the case do Project -> Clean as well.

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.

Resources