Separate properties for a shared spring library contained inside a spring boot application - spring

I created a shared library in spring that I deploy as an artifact in an artifactory. I use this shared library artifact in another spring boot application as a pom dependency. Shared library has it's own properties files under src/main/resources
I am having problems with the following:
When I try to load the spring boot application, it is not able to load the properties for the shared library and expects all the properties shared library needs in the outer spring boot application. How can fix this and have shared library always read its own properties file ?

Use #PropertySource annotation to provide the two sources for your app:
#PropertySources({
#PropertySource("classpath:yourSharedLib.properties"),
#PropertySource("classpath:youApp.properties")
})

Related

Spring Boot - configure properties of a jar included as dependency into another jar

I have a Spring Boot web app A and its dependent on a Spring Boot jar library B. I have some properties that I want to configure within B and don't want the client apps (e.g. the web app A) to configure them. I have these properties files in B.
application-dev.properties
application-stage.properties
applicatino-live.properties
The issue is that these properties are not recognized when the library is added as a dependency in web app A. What is the way to achieve this?
The PropertySource annotation can be used in a Configuration class on webapp A to achieve what you're looking for:
#Configuration
#PropertySource("classpath:application-dev.properties"),
#PropertySource("classpath:application-stage.properties"),
#PropertySource("classpath:application-live.properties")
public class WebappAConfiguration {
}
I also found the order that spring boot looks for externalized configuration helpful here.

How to add class with #resource annotation from a dependancy JAR to spring-boot application

i have two projects one for cache and other to use it. I have added the dependency of cache project as jar in my spring-boot application. but the classes mentioned with #Resouce i am not able to access in my spring boot application. how to resolve it?

Spring framework library

Can I make a library with Spring framework, and then include that library in an application that uses the Spring framework?
Yes you can make a library that uses Spring, and then include a dependency on that jar in another application created with Spring. You will want a build tool that handles dependencies, like Maven or Gradle, and probably a repository manager like Nexus or Artifactory to store artifacts that you create.
You have to make sure that the jar gets included in the component scan performed by the hosting application. See the Spring reference documentation on Importing configurations.
If the library has its own Configuration, importing the Confuguration gets it included in the component scan.
Alternatively create a marker interface in your library like this:
#ComponentScan
public interface MyLibrary {}
then in the including application have a Configuration class annotated with
#ComponentScan(basePackageClasses= { MyLibrary.class })
and the including application will scan all Components in the package hierarchy starting from the package of the marker interface.
Spring is open source so you can contribute to it. Refer to https://github.com/spring-projects/spring-framework/blob/master/CONTRIBUTING.md for more details.
Yes, any Java based applicaiton (like spring framework) can be packaged as a JAR file and published to a repository (or store it locally to start simple)
This Jar file can be included as a dependency in another Java based application (like spring framework)
To add dependencies you can either use Maven or place it in a directory and add it to local classpath for the next application to use the library.
Your library becomes a reusable library (usually a JAR file) for all other java based applications

Can a Library jar read properties from Spring Cloud Config Server?

I have a shared Library that needs some properties from a .properties file. This shared Library will be used by a Spring Boot application. We already have a Spring Cloud Config server which is serving the properties to this Spring Boot application.
Now, we want the properties for the Library also to be fetched from the Config Server, rather than packaging it's properties file along with the library jar file.
Can I let my Library file pick-up properties from the Spring Cloud Config server ? The Spring Cloud Config server documentation states the following.. seeing it, looks like only a 'Spring Boot' application can talk to the Spring Cloud Config Server to fetch properties from there.
Any pointers or suggestions ?
can be used with any application running in any language
To use these features in an application, just build it as a Spring
Boot application that depends on spring-cloud-config-client
Ref: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_client
No, a Library cannot talk to the Spring Cloud Config Server to get the properties file. Instead the config server should have those Library file's properties define din the using application's properties file.
Library: x.jar
Application: y.jar
Property file in Config Server: y.properties / y.yml
This property file will have the properties for classes present both in x.jar and in y.jar

Initialize Spring Application Context for spring projects packaged as a jar

We have several java projects. Most of them are built with Struts 2.0 framework and few built with Spring 3.2. We want to consolidate all the back-end integration service into a separate project using spring 3.2 and import this jar file on all the projects. Here are my questions
What is the best way to initialize spring application-context for a jar based spring project? This jar is utilized by multiple web-project that are built using Struts and other non spring MVC frameworks.
I read How to package spring based library for reuse?. However, this question didn't answer on how to auto-load the application context when you a call a Service from the built spring-example.jar file.
For example. I have a WeatherService.java class in spring-framework.jar file. I want to import the spring-framework.jar file into another Struts-MVC based application and call WeatherService.java from an Action Class. I want the spring bean configuration to initiate automatically when calling the WeatherService.
You can use #Import annotation if using Java configurations or <import> tag if using XMl configs. With this approach you can reuse import one Spring context into another one.
Link to documentation:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-using-import
EDIT:
If you are using maven, place your application-context.xml into src/main/resources. If not, make sure that it's on classpath.
Than if you are using XML config do
<import resource="classpath:application-config.xml"/>
or if you are using Java config do
#ImportResource("classpath:application-config.xml")

Resources