Spring + Liquibase - spring

I've got spring application. And recently added Liquibase bean. It's necessary to run it before all beans, including annotated beans like (#Component, #Service etc.). How can I do it without using depends-on.
Thanks a lot!

The options are the same as in Spring: Make sure a particular bean gets initialized first
Depends-on appears to be the standard pattern, but perhaps using the Liquibase ServletListener would work better in your case?

Related

Implementation provided for BatchConfigurer is not connsidered when using #EnableBatchProcessing(modular=true)

I am developing a sample application that Spring Batch with Spring Boot. My requirement is:
Have my own implementation of BasicBatchConfigurer so that I can configure AsyncTaskExecutor and my own dataSource as I am using SAP HANA as DB for which databaseType is not supported.
I want to use #EnableBatchProcessing(modular=true) so that I can register multiple jobs and launch them with separate Child Context
I have added all the required configurations. Without setting modular=true the Job is Launched and works as expected. It initializes the beans defined from my implementation of BasicBatchConfigurer.
However, once modular=true is set, the beans from my implementation are not initialized.
The code is hosted here: https://github.com/VKJEY/spring-framework-evaluation
I debugged further into the issue:
Looks like, When we set modular=true, BatchConfigurationSelector uses ModularBatchConfiguration
In ModularBatchConfiguration, there's a field Collection<BatchConfigurer> configurers. This has been annotated as #autowired.
I assume that this field is auto initialized if I provided a implementation
of BatchConfigurer as it has been mentioned in the comments of ModularBatchConfiguration class as well
However, While debugging I realized that the above field is still null beacuse of which, It loads DefaultBatchConfigurer and follows the default flow.
My question is why is that field configurers not being initialized in ModularBatchConfiguration? Am I missing something?
I am using Spring boot 2.1.2.
My question is why is that field configurers not being initialized in ModularBatchConfiguration? Am I missing something?
You are hitting a lifecycle issue between Spring Boot custom auto-configuration that you defined in the META-INF/spring.factories file and Spring Batch configuration.
I debugged your code and here is how to fix the issue:
remove org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.job.data.persistence.config.AsyncBatchConfigurer
from META-INF/spring.factories file. This is not needed as Spring Batch
will detect the AsyncBatchConfigurer when it is declared as a bean.
You can even remove this spring.factories file
remove #ConditionalOnMissingBean(BatchConfigurer.class) from AsyncBatchConfigurer:
Since you declared this class as a #Configuration class, it will also be defined as a bean of type BatchConfigurer and will be detected by ModularBatchConfiguration
With these two changes, the field configurers in ModularBatchConfiguration is correctly autowired with your AsyncBatchConfigurer.
As a side note, you don't need the AsyncBatchConfigurer#configurers method as Spring will do the work of injecting all BatchConfigurer beans in ModularBatchConfiguration.
Hope this helps.

Spring Boot - Load bean only if it is enabled by a property

I have a Spring Boot application with different submodules which also contains spring components.
And in the main web modules I use 70% of the beans from the submodules. It depends on the application.yml properties, if the property group (which points to a bean) is enabled or not.
First I wanted to create Aspect-s, so when a method of a bean (which is not enabled by it's property) is called, then throw an exception. This solution could work, but then I would need to create Aspect classes, method annotations, import more and more dependencies.
So I am just wondering, would be there any other easier solution to disable a bean, or do not load at all to the spring boot container?
I would imagine something like #DependsOn, but for this you need to give a name of a bean name, but you cannot use this annotation to work with yml property.
Other easy solution is to #Bean or #Import every bean I want to managed by spring container, instead of #Import everything once from submodules, but then it is a static setting, cannot be overwrite by a single property from yml.
Spring introduced the concept of conditionals quite some time ago. Spring Boot uses this to a great extend to conditionally enable features. It even created a lot of conditional rules which you can use.
One of those rules is the conditional on a property rule. To use this rule add an #ConditionalOnProperty annotation to your bean. Now it will only be included if said property is enabled or has the specific value.
#ConditionalOnProperty(name="your.property.name")

Disable spring processing of #Named

Is there a possibility to configure spring so that it doesn't initialize beans with #Named annotations?
My current work around is to make such beans #Lazy so they don't get initialized when the application context is created, but I would prefer to have nothing related to spring in such classes.
Note this two options are not good for me:
1. Suggesting to go back to a previous version of spring that didn't support JSR-299 is not an
2. Putting JSF beans in a package that is not auto-scanned by spring.
You can try to exclude #Named using a filter when configuring component scanning (in <context:component-scan> or #ComponentScan).
See also:
4.10.3 Using filters to customize scanning

create beans with annotation spring

In struts2 i almost did not use any xml configs and used much of annotations for MVC. I build a small application in that way using struts2. Now i want to develop same project using spring 3.2. Now i want to use annotation to create beans and request mapping (this i used). I want a clear example of using bean annotations and is it possible to set properties using annotations. I am getting confused because the documentation is too large, and many annotations. providing a simple list of annotations and their usage with simple example will be a great help.
Iam doing sample project on Spring 3.1.
I have used some annotations to create beans.Below are the annotations i have used.
#Component - Annotation used to create a bean given by Spring
#Resource,#Bean
JSR Annotations: #Controller,#Repository, #Service
If you are annotating your class with above annotations Spring Container will create beans for you.
Your properties will be set with help of #Autowired annotation.

Spring framework default-lazy-init all beans

Is there a spring property to lazy-init all beans that spring framework loads ?
I know about these properties
- lazy-init="true"
- default-lazy-init="true"
however there are multiple spring config xml files and some are packaged within jar so dont have liberty to change neither <bean> nor <beans> tag.
Any other way to tackle this via configuration ? or programatically ?
Short of extending the Spring bean loader, none that I know of.
You caN also use #Lazy annotation, but it is the same as you mentioned above.
According to java doc this should work ( though it looks not nice)
if (context.getBeanFactory() instanceof DefaultListableBeanFactory)
{
((DefaultListableBeanFactory) context.getBeanFactory()).setAllowEagerClassLoading(false);
}
I've implemented this on my company, had to extend some classes of spring tough. It wasn't easy, but we gained about 20s on every tomcat startup. Unfortunately, for privacy clauses, I can't show the code, but take a look at ClassPathBeanDefinitionScanner,DefaultBeanDefinitionDocumentReader,ContextNamespaceHandler and ComponentScanBeanDefinitionParser classes.
Starting with Spring Boot 2 you can use the spring.main.lazy-initialization property to configure lazy initialization across the whole application.
Setting the property value to true means that all the beans in the application will use lazy initialization.
in application.yaml
spring:
main:
lazy-initialization: true
or in application.properties
spring.main.lazy-initialization=true

Resources