How do we load additional jar at runtime along with boot jar.
Primary jar: Main.jar
Additional jar: Support.jar
Main project is a gradle boot project.
Support project is NOT a gradle project but is given compile time dependencies to the required jars.
Contents of Support project:
#RestController
#RequestMapping("/test")
public class CustomService implements WebMvcConfigurer {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public #ResponseBody String get() {
return "Done!!";
}
}
What i tried:
java -cp Support.jar:Main.jar -Dloader.path=Support.jar -Xbootclasspath/p:alpn-boot-8.1.11.v20170118.jar -Dloader.main=com.abc.app.MyApplication org.springframework.boot.loader.PropertiesLauncher
The boot starts up fine but the endpoint is not registered.
NOTE:
I had mentioned annotation scanning.
#SpringBootApplication
#ComponentScan("com.abc")
public class MyApplication {
....
}
Also the Main.jar will be run from various places by various users. Each user might provide his own version of Support.jar. So, hardcoding the dependency into the gradle file of Main project is not feasible.
Try adding #ComponentScan(basePackages=full.name.of.customservice.package) to your spring application configuration, or make CustomService the same package as your #SpringApplication class
try using this - org.xeustechnologies.jcl.JarClassLoader from https://github.com/kamranzafar/JCL
JCL is a configurable, dynamic and extensible custom classloader that loads java classes directly from Jar files and other sources.
Related
Let's say I have an external jar (that supposed to work in spring boot env) that has this simple class:
#Component
#ConfigurationProperties("test")
public class NetworkConfig {
//getters/setters
...
}
Now I use this jar as dep in a Spring project (NOT Spring Boot!!).
I have an application.properties file in that project and want to load properties from it to this class and it should be available in a context. How would I do it?
I also need to mention that external jar is my lib and I can modify it if needed.
#ConfigurationPropertiesScan ("path_to_property")
I have following situation. JDK 17, Spring-Boot: 2.6.2. A gradle multi-project. One project is a library (java-library, no spring boot plugin). Another project is a spring boot application with spring boot plugin. Generally, the spring dependency management plugin is not used, gradle platform concept is used instead. Application project includes dependency to library project per "implementation(project(':.."
Library:
Library project and spring boot application project have different packages.
like:
library root package is a.b.c and application root package is a.b.d
Library project has in root package of the package hierarchy a configuration class (for example a.b.c.LibraryConfig) which is annotated with #Configuration annotation and with #ComponentScan annotation, which has attribute "basePackageClass" pointing to this configuration class:
#Configuration
#ComponentScan(basePackageClass = LibraryConfig.class)
public class LibraryConfig {
Inside of library hierarchy (so in packages a.b.c.*) are services and rest controllers.
Application:
Application has in root package of the package hierarchy (for example in a.b.d) application class and in sub-package "config" it has configuration class (would be for example a.b.d.config.AppConfig), which imports the configuration class from library:
#Configuration
#Import(a.b.c.LibraryConfig.class)
public class AppConfig {
Problem:
Classes from the library are there in runtime - we are able to load them.
No any component of the library is in context (not registered), neither services nor rest controllers.
What are we missing?
We have tried different constellations of imports and scans. Also added component scan to application class and set there particular packages. Nothing helped.
The problem was, that the main application was using spring boot indexing annotation processor and library - not. So after enabling the annotation processor for library problem was solved.
I have 2 Projects.
All the API contract (interfaces) defines in the demo-parent project (spring boot application)
The implementation for those defined in the demo-child project (spring boot application)
demo-parent is a dependency for demo-child, defined in the pom.xml of demo-child
In demo-parent :
AccessAPI.java
public interface AccessAPI {
void call();
}
SpringDemoParentApplication.java
#SpringBootApplication
public class SpringDemoParentApplication {
#Autowired
private AccessAPI accessAPI;
public static void main(String[] args) {
SpringApplication.run(SpringDemoParentApplication.class, args);
}
#PostConstruct
void exec() {
accessAPI.call();
}
}
In demo-child I have the implementation for the service :
AccessAPIImpl.java
#Service
public class AccessAPIImpl implements AccessAPI {
#Override
public void call() {
System.out.println("Executing from AccessAPIImpl");
}
}
Goal trying to achieve :
I must be able to build these projects independently, and pass the demo-child project jar via classpath to pick the implementation and inject all the implementation when running of demo-parent .
Such as :
>java -jar demo-parent.jar -cp demo-child.jar
I expected that the implementation would be picked up from the demo-child and autoinjected but It is not working as expected.
Note : I dont want to add demo-child dependency on demo-parent, the implementations/dependency must be picked up at runtime.
Please check the Git repository :
https://github.com/anthonyvk/spring-demo-child
https://github.com/anthonyvk/spring-demo-parent
TL; TR:
java -cp demo-parent.jar -Dloader.path=demo-child.jar org.springframework.boot.loader.PropertiesLauncher
if the demo-child.jar is a Spring Boot application, i.e. if the jar is repacked by the Spring Boot plugin, you need to change the loader.path:
java -cp demo-parent.jar -Dloader.path='demo-child.jar!/BOOT-INF/classes' org.springframework.boot.loader.PropertiesLauncher
Explanation:
By default, Spring Boot uses JarLauncher, which looks only in BOOT-INF/lib/ inside the jar archive, -cp argument has no effect. The commands above switch to PropertiesLauncher, which honors loader.path parameter (-cp equivalent). For details see The Executable Jar Format
By the way, the project structure seems to be a bit problematic:
adding spring boot application (demo-parent.jar) as a dependency to demo-child.jar is tricky, as demo-parent.jar is repacked by the Spring Boot plugin and maven cannot find AccessAPI class
there are runtime circular dependencies, even if not explicitly specified in the POM
I don't know detailed requirements, but if possible, I would suggest:
extracting interfaces to a separate project, let's say demo-api
making demo-parent and demo-child depended on demo-api, not on each other
making demo-api and demo-child regular jar's, not Spring Boot applications (removing Spring Boot plugin)
I am developing a Spring Boot application using STS with the Gradle plugin.
I have a different configuration for tests, to prevent our Selenium tests from having to login.
So in src/test/java/etc I have something like this:
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
#EnableWebSecurity
public static class SecurityConfig extends WebSecurityConfigurerAdapter
{
#Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().anyRequest().permitAll();
}
}
Whereas in src/main/java I have an equivalent class that configures login etc, requiring login for all pages.
If I run the application through the Gradle plugin (bootRun), everything works fine.
However, if I run or debug it through Eclipse directly (e.g. right clicking on the project, Run As->Spring Boot App or by clicking
the run/debug buttons in the Spring or Java view) then the test config is applied, so access is granted to all pages without login.
I'm guessing that the test classes are being included in the classpath when I start the application this way.
Is there an easy way to prevent this from happening?
When you run the test from eclipse, the classpath is prepared by eclipse (and not by maven or gradle).
Eclipse only uses one classpath per project and does not know anything about dependency scopes (like 'compile' or 'test').
So the classpath always contains any resources of a referenced project.
You cannot change this behavior of eclipse.
You need to use naming conventions, profile etc. to avoid accidental use of test resources.
You can append #TestComponent to you test configuration class. These bean configurations will be skipped during component scan of your application. Depending on the component scan configuration, you need to define an #ComponentScan exclude filter:
excludeFilters = #ComponentScan.Filter(value = TestComponent.class, type = FilterType.ANNOTATION))
I have a library as a jar packaging Maven project which offers services.
The #Configuration class to instantiate service beans:
#Configuration
public class JpaService {
#Bean
public UserRoleServiceImpl userRoleService() {
return new UserRoleServiceImpl();
}
#Bean
public UserServiceImpl userService() {
return new UserServiceImpl();
}
}
I reckoned I needed to have the beans instantiation outside of the jar archive.
So I had a copy of this class in the test part of the project, and another copy in another war packaging Maven project using the library.
But what if I instantiated the services in the jar library itself. I would need to only do it once, be it for testing or for all client projects using it.
UPDATE: Two questions...
Should all component scanning only be done from the war ? Or should the jar service components be scanned from the jar ?
And what if two components (one in the jar and one in the war) have the same class name in the same package ?
I dont think i fully understand your question, but if you are aiming to add beans to your application context that is outside the jar then what you have to do is use the #ComponentScan annotation, and specify the package you want to scan, the package can be in a different jar, the only thing required is that you anotate the clases you want to include with #Service, #Componenet or even #Configuration
example:
#Configuration
#ComponentScan(basePackages={"com.somepackacge.controller",
...
you can include as much packages as you like.
By the way dont copy your clases from one place to the other, maintining that will be a headache in the futute, if you want to include your configuration in your tests you can always do :
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = MyConfigClass.class)
Where MyConfigClass is the class u used before with the component scan
Hope it helps