Spring Application Failure - java

I have a Spring Boot app with many beans. In one bean, if there is an exception in the bean's constructor, the application fails to start. In a different bean, an exception in the constructor does not cause the application to fail to start. Failing to start is the desired behavior. What is the difference between the two beans? How do I get the behavior to be the same?

Related

Spring: How to load my bean as the first bean when server startsup

I have a java application based upon spring and I have a bean that runs some connection test logic during server startup. If the connection test fails, I stop the server.
I need this bean as the first one to be loaded before any other beans performs some operations using the connection string.
Can someone help me figure out how I can load this connection test bean as the first bean please.
Ways tried:
Bean lifecycle hooks such as InitializingBean, init-method, #PostConstruct won't work as they will get invoked for my specific bean but can't control what beans gets loaded before my desired bean.
Update:
depends-on or #DependsOn worked.
Thanks

Spring Boot: how to run a server at Spring Context startup and stop it during context shutdown during execution of tests?

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.

Togglz application context listener warning

I've implemented a basic togglz setup using Spring Boot starter. I'm getting the following at startup:
WARN o.t.s.l.TogglzApplicationContextBinderApplicationListener - ApplicationContext already bound to current context class loader, releasing it first
This may also be causing multiple startups, as I sometimes see the Spring banner go by twice.
To summarize my implementation:
Using property-based toggles in application.yml
Wiring FeatureManager into components
Have a configuration which returns a SpringSecurityUserProvider bean
Not using a TogglzConfig (though I tested with it, and see no difference)
I've looked at the Togglz class, and see the log warning, but I don't understand what situation in Spring Boot would cause this error.
Thank you!

Prevent Spring from Failing if One Bean Fails

We have an application that creates beans on startup using Spring's AnnotationConfigApplicationContext. What we're trying to do is not have Spring destroy all the beans created and thus cause the app fail on startup if creation of one of the beans fails. Is there any native Spring config or way to accomplish this?
I tried overriding the AnnotationConfigApplicationContext with my own Custom AnnotationConfigApplicationContext and catching exceptions within there. What was happening though is when an exception was thrown, all remaining beans were not created.
We're using Spring 4.3.0.
easy way to deal with this is by lazy initialization of bean using attribute lazy-init="true" in your bean declaration

Spring Autowired Exception when a remote ejb is down

Hi,
I have a simple question that I am not able to solve.
I'm using Spring 3 to lookup Remote ejbs and I added lazy-init=true into bean definition because my application must starting
also if some ejb is down.
Unfortunately my controller when autowiring of an ejb that is down, I get "Injection of autowired dependencies failed".
Someone can help me! Is #Autowired not lazy? It's possible, using spring, start my web application also if some remote ejb is not started?

Categories