Spring boot Configuration FileNotFoundException when extending configuration from another spring boot app - java

I have 2 spring boot apps, and I want one of them to extend the configuration properties of the other. Everything works fine in Eclipse when I run the project as a maven spring boot app, however, when I run the jar or mvn spring-boot: run, I'm getting:
org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [com.xyz.integration.app.my.Application]; nested exception is java.io.FileNotFoundException:
class path resource [com/xyz/integration/app/pdfthing/ConfigurationManager.class] cannot be opened because it does not exist
I've searched all over and tried many variations, including:
#ComponentScan (using both com.xyz.. and classpath)
#Import
Class-Path in Manifest to the external jar file (In Linux, how to execute Java jar file with external jar files?)
Here's part of the configuration class:
#Component
#ComponentScan("classpath:com/xyz/integration/app/pdfthing")
public class MyConfigurationManager extends ConfigurationManager{
I'm guessing running as a jar doesn't load the external jar in the configuration. Any thoughts/hints? Thanks!

Related

External jar as Spring application

Let say I have an external jar named data-access-0.0.1.jar that contains Spring annotation like #Component, #Bean. But this jar does NOT contain the main method to run as a Spring application (means no #SpringBootApplication, no #ComponentScan, ...).
Now I have another jar named employee.0.0.1.jar (does have the main method to run as Spring boot application - #SpringBootApplication), that use data-access-0.0.1.jar as a dependency. But somehow it does not scan #Bean, #Component in an external jar (error when starting the app, no bean with type "myComponent" found).
I think #ComponentScan in employee-0.0.1.jar should configure base packages include a package from the external jar and it should work, but I do not want to apply this mechanism. I want to somehow configure in the external jar so that any another jar that depend on it should scan the whole jar for autowiring

Spring Boot App not picking up application.properties from dependent jar

I have two spring boot apps. The first is an API library that's pulled into the second (a web application) as a dependent jar.
The first, is an API library that houses functions to create "cases" in an IBM solution. It's a standalone type jar that has a service class that exposes methods like getCaseXMLForDocId(String docId) or createCaseForAgreementNumber(String agreementNumber)
The first library called CaseInvocationAPI has an application.properties file which has several properties. For example:
caseinvocation.query.fetchNonProcessedCaseXml=SELECT Id, CaptureSource, AgreementNumber, CaptureSourceID FROM CaseInvocation WHERE ProcessIndicator IN (0, 2)
The service class has a method which makes a query, grabbing that query string from a member variable that's populated with a property from the application.properties file:
#Value("${caseinvocation.query.fetchNonProcessedCaseXml}")
private String selectNonProcessedQueryString;
The second SpringBoot app is a webapplication that has REST controllers. These controllers expose endpoints that call the CaseInvocationAPI library, specifically the CaseInvocationService class.
The problem I am having is that when the SpringBoot WEBAPPLICATION starts up, the context configuration blows up with the following error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'caseinvocation.query.fetchNonProcessedCaseXml' in string value "${caseinvocation.query.fetchNonProcessedCaseXml}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1039)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
... 45 common frames omitted
It appears that when the WebApp starts up, when it's trying to build the classes from the dependent jar, those properties are not being found.
I didn't think I had to copy each and every property out of the dependent jar application.properties file into an application.properties file in the Webapp project.
Why isn't the WebApp project (CaseInvocationWebApp) picking up the application.properties file from the dependent jar file (CaseInvocationAPI)?
I checked the compiled jar file (CaseInvocationAPI) and the application.properties file is there in the jar.
Looks like the problem was related to the fact that both the child jar and the webapp have application.properties files. I wasn't aware that the parent WebApp application.properties sort of overwrites the others (ignoring all others really).
Special thanks to Paschoal for his response.
You can see details on the answer here:
Adding multiple application.properties files
There are 3 ways (that I can think of) you can approach this:
The dependency, API library, should not have an application.properties since it's a library and not an executable Spring boot application in itself. You only define the properties in your web application's application.properties, even for the API library.
But, here the assumption is that you have access to the API library jar.
You can redefine all the properties in web application's application.properties essentially overriding them.
Explicitly configure the Spring boot application to use both the application.properties files, each for different set of properties.
Caveat: The file names must be different, as config location is classpath for both.
#SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplicationBuilder(WebApplication.class)
.properties("spring.config.location=classpath:api-application.properties,classpath:application.properties")
app.run(args);
}
}

How to make jar with Spring Bean inside, and use it in a SpringBoot Application?

I have to make a plugin manager that handle Spring Bean inside jar contains in a specific external folder, in my case it's externalPlugin.
The folder externalPlugin is in my classpath.
My problem is that spring don't load beans when there are inside the jar.
If the jar file is extracted in /externalPlugin folder, Spring boot loads correctly the beans.
Is there a specific way to create jar , that spring loads correctly bean inside JAR? or even is it possible to load bean in external Jar?
Thank you for your time.
In my spring boot application, in order to load external Bean, I use the annotation
#ComponentScan({"com.app","com.plugin"})
To know if my bean are loaded I use and print the result of
String[] beanNames = ctx.getBeanNamesForType(AbstractPlugin.class);
external plugin jars are created as follow with jar cvf command
/externalPlugin/pluginA.jar
|-META-INF/
| |-MANIFEST.mf
|
|-com/
|-plugin/
|- beanA1.class
|- beanA2.class
package com.plugin
#Component
public class BeanA1 extends AbstractPlugin{
}
package com.plugin
#Component
public class BeanA2 extends AbstractPlugin{
}
found what i needed
http://www.codevomit.xyz/bootlog/blog/how-to-provide-spring-boot-fat-jar
and specialy generate jar with eclispe

Run Spring's .class instead of .jar file

I have a Java Spring application which I built with maven, and a .jar file together with .class files were generated.
Instead of running the .jar file, is there a way for me to run the .class files instead?
I tried executing java -cp . hello.Application in the same directory as the Application.class (source code of Application.java below), but was given Error: Could not find or load main class hello.Application. Removing the hello. did not work either.
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I am doing the Spring boot application.I think you can also use maven to create a jar file and then use linux commend to run it.
you can run a class by executing follow command :
java Application
for only simple console application. to run Spring Boot application you need maven to build jar file and executing jar file to call main method to load spring boot context. Spring Boot need several requirements and dependencies to run. Spring load context and create a special structure to load. for more you can see following links:
https://spring.io/guides/gs/spring-boot/
http://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-exploded-structure/

Spring donĀ“t find Interface packaging into a external jar

I have a problem with Spring MVC.
I have a Spring bean that implements an interface packing inside a jar.
Look the schema below:
MyJarInterfaces.jar
|
+-- IService.class
In my MVN project this Package (MyJarInterfaces.jar) was described as a dependency.
In my project structure, I have my bean implements IService.class.
package com.beans.spring
public class Service implements IService {
...
}
In my Spring MVC XML configuration () I have :
<context:component-scan base-package="com.beans.spring" />
So .... when I run my project using maven and tomcat plugin (mvn clean install tomcat:run) my Tomcat throw an exception :
Exception sending context initialized event to listener instance of
class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class [com.beans.spring.Service]; nested
exception is java.io.FileNotFoundException: class path resource
[IService .class] cannot be opened because it does not exist
...
But when I copy my war file after run mvn clean install into my TomCat server it works fine.
I have this problem when I run using maven plugin.

Categories

Resources