Is it possible to make Spring boot use my Beans.xml file?
How can I supply context to it?
Is there any other way to put it?
Sincerely,
Peter.
Absolutely! Just add a #Configuration with a #ImportResource. For more info, see http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ImportResource.html
Related
Is it possible to have a Spring application with both Spring XML that defines some of the beans and also a #Configuration class that defines other beans?
Yes, it's possible.
Spring Boot and Spring 5.x will drive you away from XML and towards annotations. It's a good idea to follow the trend and write using the proper idiom.
None of my Spring Boot apps use XML. That was part of Rod Johnson's original vision for Interface 21/Spring, but annotations have won the day.
It is certainly possible, but it is not recommended. Bean configuration should be consistent to be either all in #Annotations or in XML files.
Is there any specific reason that you want to put some bean configuration in XML rather than in #Configuration class?
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?
In my unit tests I want to configure Spring in code (API, Annotations) so they will not depend on bean configuration files.
Can this be done?
For example:
Class Dependency {}
Class A
{
#AutoWired
Dependency d;
}
When testing A, I want to be able to create an instance of it with the Dependency member resolved, without having to use configuration files.
Thank you!
In short, yes, you can start a spring application context with any of the implementations of org.springframework.context.support.AbstractApplicationContext. Namely, if you don't want to load the definitions from an XML file, you can use the org.springframework.context.support.StaticApplicationContext or org.springframework.context.support.GenericApplicationContext to start the context.
With the context instantiated, you can start creating beans with the BeanFactory, either the oen default to the selected context or a custom one, that suits your needs.
In practice, it's lot more work than that. It's easier if you just use plain XML configuration, but it can be done.
You can use the Java configuration instead of the XML configuration.
You can use the AnnotationConfigApplicationContext to create application context programatically without bean configuration files.
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
I have two packages holding spring controllers,I am not able to give actual names and packages.
com.test.controller------ABCController
com.test.xyz.controller------xyzController
I defined <context:component-scan base-package="com.test"/> in spring xml file,Spring able to find out controllers from com.test.controller,XYZController never called from spring container.it is saying no handler found.If i move xyzcontroller under package com.test.controller,then it's start working.
Please give any hints,why XYZController not working.
Thanks,
Raj
Use <context:component-scan base-package="com.test.**"/> in your spring config, so it will scan for every annotated class in all subpackages of com.test.