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

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.

Related

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?

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

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

read properties from third party jar in spring boot application

I’m adding a third party jar as a maven dependency in my spring boot application A. Third party jar is a jar of another spring-boot application B. it has its own configurations saved in conf folder "B.properties".
I have used #Import({B.class}) annotation to import the beans of application B in my application but not able to access the configuration properties of application B. Is there any way to access the configuration properties of an application from a jar?
You can create your own Config Server just add #EnableConfigServer annotation and provider configurations from service A to service B.
You can review this example:
https://spring.io/guides/gs/centralized-configuration/
UPDATED Fix adding
#PropertySource("classpath:/conf/B.properties"),

Prevent SpringBoot from creating an EmbeddedServletContainer

I'm trying to convert an existing spring application into a spring boot application. this application is not a web application but somewhere deep in it's maven dependencies hierarchy it includes a spring-web jar and therefore spring-boot tries to autoconfigure a WebEnvironment and in turn complains about not finding an EmbeddedServletContainerFactory. But this is intended as I'm using spring-boot-starter instead of spring-boot-starter-web.
My questing is: how can I prevent spring-boot from autodiscovering web specific items in the classpath? I already tried something like
#EnableAutoConfiguration(exclude = { EmbeddedServletContainerAutoConfiguration.class })
but it doesn't seem to work. Debugging the startup process I see that it runs into a method called deduceWebEnvironment() which is called unconditionally. This method returns true as soon as the following classes are on the classpath:
javax.servlet.Servlet, org.springframework.web.context.ConfigurableWebApplicationContext
But again, even this classes exist in the cp, I don't want to startup a web-application.
Try new SpringApplicationBuilder(YourApp.class).setWebEnvironment(false).run(args) You can also disable the web mode via configuration in application.properties
spring.main.web-environment=false
See the documentation

Should I use `src/main/webapp` to serve static content with Spring Boot?

The documentation of Spring Boot states:
Do not use the src/main/webapp directory if your application will be
packaged as a jar.
But surprisingly the Spring Boot Sample for static web files is using the /src/main/webapp directory. And also JHipster is using the webapp folder.
So I'm confused. Is the warning in the documentation of Spring Boot outdated? Is it now considered good practice to use src/main/webapp to serve static files with Spring Boot jar applications? And if not, what is the recommended practice now when using Spring Boot in a Maven setup?
From Spring Boot Reference Documentation (emphasis mine):
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.
In some of my (Maven) projects I am currently using src/main/resources/static because it's considered part of the classpath by default and IDEs (like Eclipse) tend to like this.
Be wary because Spring Boot 2.4.x disables the default servlet (which impacts on loading of src\main\webapp and anything else at the root of your WAR file. JAR file packaging is disabled by default but there is a configuration option to all that too - see Spring boot: configure it to find the webapp folder.
https://github.com/spring-projects/spring-boot/issues/22915
The full release notes are here: https://spring.io/blog/2020/11/12/spring-boot-2-4-0-available-now
hi if i understand your question
when your are using jar packaging in spring boot
yo will put your resource in META-INF/resources
and you can use this jar package in other project and call resource ,
you can move webapp to meta-inf directory using maven and gradle build

Resources