Spring boot requires to create a bean instance. Due to which the pods are started as I run the application
#Bean
Ignite ignite(IgniteConfiguration igniteConfiguration) {
Ignition.start(igniteConfiguration)
}
Is there any way that I can configure and start it in spring boot service on receiving a request?
You can add #Lazy annotation for bean declaration and also combine #Lazy with #Autowired annotation in your service.
More information here:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Lazy.html
This is not recommended, because Ignite startup may be a lengthy process, and you probably don't want to wait for it upon getting a request (only to learn that startup failed, for example) and certainly not start it per request.
Also, consider using IgniteSpringBean instead of raw Ignite.
Related
My Spring Boot application uses a database server. During the tests, I would like to run an embedded version of the database. The server starts with a random port each time (it's from testcontainers.org).
First thing I tried was to use JUnit4's #ClassRule to start/stop the server, but Spring Boot is smart and re-uses contexts across test classes. So for a single test class everything works fine, but when I run tests in a package (or all tests), they fail due to this lifecycle difference.
Is it possible to somehow hook into tests execution and get a callback when Spring Boot Test infrastructure starts and stops a new context?
The most probable answer I will get is 'just add a server bean to the context when your tests run'. Ok, but here I face another problem:
How do I make sure that the server bean is initialized before other beans that talk to the server? #DependsOn does not seem to fit here as I do not want to have in production a bean annotated with #DependsOn("testServer")
Spring Boot is 2.1.6.
We are externalizing configuration of our microservices (spring boot based) using spring cloud.
As per my understanding on Spring Cloud, to enable the beans loading refreshed/updated values from Config server we need to do 2 things in Spring Cloud Client:
add #RefreshScope on the beans reading values from property files
using #Value
add spring actuator to provide /refresh endpoint to
refresh the context.
Scenario:
We have 100s of classes reading values from property file using #Value.
I have to mark all these beans refresh enabled using #RefreshScope annotation.
How can I avoid putting #RefreshScope annotation on all these classes.
Is there any shortcut or spring cloud feature to get around this situation.
You may want to look into Spring Boot feature called #ConfigurationProperties. It is designed to better organize several external configuration options.
According this Github issue, it should work for spring-cloud without #RefreshScope usage.
EDIT (reaction on comment): Maybe you are missing point of #ConfigurationProperties. With this annotation, you wouldn't use it in other configuration classes. You would have dedicated class (or few classes) only for reading and providing properties. Other configuration classes would inject this configuration holder bean.
You could encapsulate your #Values into one (or several) ConfigurationService bean which is #RefreshScoped and autowire this service into your classes instead. That way you only have a small amount of request scoped beans and your services can stay singletons.
I'd like to have a way how to expose all endpoints that exposed by my Spring application. Is there a simple way to check, for each #profile which are exposed?
Example:
GET /api/resource
GET /api/resource/list
POST /api/resource
PUT /api/resource
In the past, I have used a web application made in Laravel, and they had a simple cli method for checking the exposed methods.
I assume based on how the questions is worded that you are not using Spring Boot, if you were, the actuator mappings endpoint does this for you, but your answer lies in how the mappings endpoint is build in actuator. There is a RequestMappingHandlerMapping object you leverage.
In this scenario you can use two approaches:
Spring Boot Actuator feature. Your endpoints of application will be available at http://host/actuator/mappings
Swagger library can also be used to list all endpoints of a REST API
The best solution is to use Spring boot actuator and hit the endpoint /actuator/mappings to get all the endpoints.
But if you can't use actuator or can't add it as dependency you can retrieve all the endpoints programmatically the mapping handlers, Spring get shipped with three implementations of this interface (HandlerMapping):
RequestMappingHandlerMapping: which is responsible for endpoints that annotated with #RequestMapping and its variants #GetMapping, #PostMapping .. etc
BeanNameUrlHandlerMapping: as the name suggest it will resolve the endpoint(URL) directly to a bean in the application context. for example if you hit the endpoint /resource it will look for a bean with the name /resource.
RouterFunctionMapping: it will scan the application context for RouterFunction beans and dispatch the request to that function.
Anyways, to answer your question you can autowire the bean RequestMappingHandlerMapping and print out all the handler methods. Something similar to this:
#Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping;
#PostConstruct
public void printEnpoints() {
requestMappingHandlerMapping.getHandlerMethods().forEach((k,v) -> System.out.println(k + " : "+ v));
}
I'm redirecting from http to https using the TomcatEmbeddedServletContainerFactory instructions found here:
http://drissamri.be/blog/java/enable-https-in-spring-boot/
However this breaks the testing of the rest controller, even though it uses an Application to run the test that does not contain or reference the TomcatEmbeddedServletContainerFactory configuration performing the redirection.
If I remove the redirection configuration from the Application that contains the #SpringBootApplication annotation that runs the application, the test passes.
Any idea how to keep the production configuration for the application in place without breaking the Rest controller configuration?
TIA,
- Ole
If you don't want to use the TomcatEmbeddedServletContainerFactory in your Application.java in your test, you could always add work with Spring profiles to make sure that bean is only loaded when you start your application with a certain profile (production for example).
Then the #Bean TomcatEmbeddedServletContainerFactory would have a #Profile("production") annotation, and your test would not create that bean unless you are using the production profile explicitly.
I will update this answer when you give more information regarding your problem.
I am able to view the Bean Graph for any beans wired through XML, but when I have beans that are #Autowired through annotations, it doesn't show any arrows between them.
According to STS #171 and STS #1066 it was implemented a long time ago.
Note: Last tested with STS 3.4.0.RELEASE
If I deploy to tcServer and do Live Bean Graph, then it works, but not using regular view.
The improvement feature was requested and even closed; but new tickets still say that feature is requested - and not closed yet even (STS-2396) in 2.8. I haven't succeeded getting there. No joy :(
Issue says - "When STS is resolving autowired beans for either validation purposes or for the bean graph, it seems to only to be able to find beans that have their type explicitly defined."
If you are using Spring boot you can use spring acutator to view all the beans..
Spring acutator enables /beans endpoint which shows all beans in JSON format.
Once you start the Spring boot app all you need to do is http://localhost:8080/beans