Why spring.jpa.properties. prefix is not always required in Spring Boot properties - spring

I learning Spring boot and found that in some examples use the same properties with the prefix spring.jpa.properties while other do it without prefix.
For instance:
The article explains second level cahche https://www.baeldung.com/hibernate-second-level-cache and autor shows example of needed properties ( example on autor's gitHub):
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
But it did not work for me, and I spent few hours loking for the reason, but then i noticed, someone use prefix spring.jpa.properties. to get it working (Exact moment from video lesson):
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
Which perfectly worked for me.
So my questions are:
why are they working in different way?
how to understand which properties in which cases requires this prefix?
is it probably something related to my project settings OR Spring Boot version?
any other suggestions are appreciated :)
Thank you in advance!

There are a couple of things in play here. First the tutorial you use is using plain Spring not Spring Boot. Which means a manually configured EntityManagerFactory on which you directly can set the provider specific properties. Like hibernate.cache.use_second_level_cache.
As you decided to use Spring Boot you don't have a manually configured EntityManagerFactory (although you could!). Which means you are using an autoconfigured EntityManagerFactory. Properties for this reside in the spring.jpa namespace for properties.
As it isn't feasible to specify properties for each and every extension for Hibernate (or other JPA providers) only some commonly used ones are exposed like spring.jpa.generate-ddl and a few provider specific ones in the spring.jpa.hibernate namespace. However to set all the other properties in an autoconfigured way there needed to be something else. Hence the spring.jpa.properties prefix. Anything specified in there will be passed on as is (without the said prefix ofcourse) to the EntityManagerFactory for configuration.

Related

Spring boot 2.4 Reverse document order

In spring boot 2.4, later loaded properties override earlier properties.
But, for my need, I am importing document inside document using spring.config.import. Say, import abc.properties from application.properties.
But, I need to override, later loaded properties with earlier loaded ones.
Say, application.properties should override abc.properties. not the other way arpund.
Please help me on how to do this.
Thanks in advance.
I resolved the issue with EnvironmentPostProcessor, as it seems to be not doable simply with spring.config.import.
As, both yaml (and in terns json) and properties, property source parser is readily available in spring boot, this required very little coding.

Valid syntax for autowired properties in spring

I have been looking for an explanation of how Spring Framework will look for #AutoWired properties. I have noticed that when using the hikariCP library that setting either of the properties below will result in a correct max pool size.
spring.datasource.hikari.maximumPoolSize=3
or
spring.datasource.hikari.maximum-pool-size=3
Does Spring use both camel case and hyphen separated to lookup autowired properties? Where might I find an explanation in the spring docs?
That isn't a feature of the core Spring Framework but rather a feature of Spring Boot.
Spring Boot has a feature called relaxed binding which is used for properties mapped to a class annotated/used with #ConfigurationProperties. Each property that is being bound can be used with camel-case, dashes or even uppercase with dashes (generally used for environment variables!).
So all of these will work in the same way (the latter is often used to pass it as an environment or system variable).
spring.datasource.hikari.maximumPoolSize=3
spring.datasource.hikari.maximum-pool-size=3
SPRING_DATASOURCE_HIKARI_MAXIMUMPOOLSIZE=3
There are valid and invalid properties, but Spring's #ConfigurationProperties, which used mostly for loading configurations, works with both forms of properties.
The issue can be if any framework, library, etc. use property separately, without #ConfigurationProperties. In this case, you will have runtime exception (or null value).
Most default, base, popular properties are here: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

Any way to split Spring Boot configuration into multiple properties files without having to specify an environment variable/system property

New to Spring Boot here, long-time Spring Framework user though.
I'm looking for a way to split my externalised configuration into multiple .properties files, for better readability and manageability.
I already saw this SO answer: having the ability to specify a list of configuration file names in spring.config.name (which, by the way, doesn't seem to be mentioned in Boot reference documentation, correct me if I'm wrong) would solve my problem perfectly, however that configuration property can be specified only via system properties or environment variables. If I try to specify it inside my application.properties file, it gets ignored. The same happens for spring.config.additional-location. I understand this happens because, when application.properties is read, it's too late to tell Spring Boot to search for different externalised configuration file names. However this is not a proper solution, because the way I split my configuration should be an "implementation detail" that the consumer of my application shouldn't be aware of, so I don't expect the consumer to specify an external parameter otherwise my application breaks out-of-the-box.
I think that a way to do this should be provided. Perhaps some import mechanism for .properties files or the ability to specify spring.config.name even in application.properties (some known and reasonable limitations would be acceptable).
The best I could find out is to use #PropertySource, but this is not profile aware: unless you use some ugly nested class hack, or you put spring.profiles.active variable in the resource name (which will break if multiple profiles have been activated), you won't get the benefit you have for application.properties profile-specific files.
I was not able to find an "official way" to do this, apart from some statements from Spring Boot devs that say that they're rather promoting the use of a single (possibly giant...) externalised configuration file. It seems like this position is not so popular, judging from the post reactions on GitHub, and IMHO it really seems to be a basic feature missing. I have been working with multiple properties files in Spring Framework (using XML configuration) for years and I never felt that having an only huge file would have been better.
If I understand it right, in Boot 1.x this was in some way possible using the location attribute of #ConfigurationProperties, which is however missing in Boot 2.x.
Any suggestion?
Have you tried with Spring Profile?
What you can do is create application-file1.properties/yml, application-file2.properties/yml and put it in config location and then add spring.profile.active=<your env profiles>,file1,file2.
It will load the files.
This profile entry can be in bootstrap.yml, or JVM args to application, in Manifest-<env>.yml in case of Pivotal Cloud Foundry. Not sure on AWS and other cloud provider.
Hope this will help.

Programmatically configure Spring Boot app

what's the easiest way to get the spring boot goodness but not try to autoconfigure all the things? For instance, to only run flyway with our already configured properties (some loaded via Consul + Spring Cloud), I was hoping I could do something like:
#Configuration
#Import({DataSourceAutoConfiguration.class, FlywayAutoConfiguration.class})
public class FlywaySetup {}
and have the main method just call SpringApplication.run(FlywaySetup.class)
The problem with this is it picks up all the Component Scan / crazy long list of other dependencies. Any way to specifically configure the dependencies (but still get the nicities of the framework)
If you run this app, it shouldn't use component scan at all. There's nothing that triggers component scan in spring boot besides #ComponentScan (that's available on #SpringBootApplication).
It would help if you could provide more accurate details rather than "crazy long list of other dependencies.". Running that FlywaySetup should only load those two configuration classes (important: these are not handled as auto-configuration anymore). If you have component scan, there's something else you're not showing.
You can exclude auto-configurations you don't need.
You may want to take a look at this SO answer to explore mechanism how to do that.

Spring Security - XML vs Java Configuration

I'm just learning Spring Security, and a lot of Spring's documentation appears to use Java-based bean configuration (as opposed to XML.) Overall, this seems to be the way a lot of their projects are going. However, portions of their documentation tend to start with Java configuration and then switch to XML config later on. I found a blurb in one document (http://docs.spring.io/spring-security/site/docs/3.2.0.RELEASE/reference/htmlsingle/) stating the following:
Spring Security’s Java Configuration does not expose every property of every object that it configures. This simplifies the configuration for a majority of users. . . . While there are good reasons to not directly expose every property, users may still need more advanced configuration options. To address this Spring Security introduces the concept of an ObjectPostProcessor which can used to modify or replace many of the Object instances created by the Java Configuration.
Can everything that can be done in XML configuration be done with Java config? Is there a definite direction that the Spring community is taking overall in terms of configuration style?
You can choose either java based or xml based configuration.Stick to one, don't mix both.But don't forget to use the annotation based configuration.You just need to annotate spring managed components with #component,#service etc.You don't need to have that bean defenition in xml or java class.
<context:annotation-config/>
<context:component-scan base-package="com.package"/>
or
#Configuration
#ComponentScan({"com.foo.bar", "org.foo.bar"})
http://docs.spring.io/spring-security/site/docs/3.2.0.RC2/reference/htmlsingle/#jc
You can use Java or XML based. But there is a thing
Usage of xml based configuration is decreasing in newer versions of Spring.
Like #EnableAutoConfiguration tag...
With this, web applications doesnt need any XML conf even web.xml

Resources