Spring don´t find Interface packaging into a external jar - java

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.

Related

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

How to initialize beans in imported jar with properties from application properties

I have a spring app that imports one jar.
Within the jar there is a bean annonated with #Configuration for example and it has some fields annotated with #Value.
If I import the jar into my app, provide the app.properties file in the app(not in imported jar), how can those value be used for initializing fields in the bean?
When I start an app I got message:
Caused by: java.io.FileNotFoundException: class path resource [app.properties] cannot be opened because it does not exist
And my bean in imported jar look like:
#Configuration
#PropertySource("classpath:app.properties")
public class SomeConfig {
#Value("${some.name}")
private String field;
...
}
When the app.propeties file is in the imported jar there are no problems.
You can try with #PropertySource("classpath:/app.properties") or using placeholders as described in docs
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

java.lang.NoClassDefFoundError even though war file contains the jar containing the class

I have maven dependency to import an external project jar. The jar is imported in my project by Maven and is present in my Libraries->Maven Dependencies.
I access a static method of a class present inside this Jar from one of my classes. When I do Maven build and run the project in Tomcat that is configured inside my Eclipse, it runs fine and the class and its method is accessible.
But when I deploy the war file build by Maven to external Tomcat server, I get
14-May-2017 14:40:31.631 SEVERE [http-nio-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [apple] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.apple.fruit.security.client.TokenManager] with root cause
java.lang.NoClassDefFoundError: Could not initialize class com.apple.fruit.security.client.TokenManager
I accessed that war file and found that the war file contains that jar.
What am I doing wrong?

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

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!

Spring JPA Repository in one JAR cannot be used in another war

I have a very simple Spring JPA Repository which is packed into a JAR and stored in the local maven repository.
I can use the JPA Repository in my Unit-Tests and in a little Main-Test-Routine. It gets #Autowired without any problem.
Here is the definition of my JPA-Repository:
public interface TestRepo extends CrudRepository<Seminar , Integer > {
List<Seminar> findAll();
}
And in the applicationContext.xml of the JAR:
<jpa:repositories base-package="de.dmsb.my.core"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager">
</jpa:repositories>
There a also some other Services defined in this JAR too which can be used in the JAR and in the WAR, which includes the JAR as a dependency with maven.
But as soon as a try to #Autowire the the JPA-Repository:
#Autowired
TestRepo testRepo;
I get an runtime error that there is no suitable bean defined. Other beans work - but as soon as it comes to JPA-Repositories it doesn't work.
I mean of course you cannot #Autowire a interface - and in the WAR there is not JPA-Repository bean in the ApplicationContext when I debug it - all the other beans from the JAR are there.
Any idea what the problem might be?

Categories

Resources