Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

soft exception handling of BeanCreationException for spring JPA repository

I am building a service that is supposed to be elastic and resilient enough based on data sources available when SpringBootApplication launches.

E.g. when datasource for FooRepository is online, then proceed and launch FooService; when datasource for BarRepository is online, then proceed and launch BarService. If one of the data sources is offline (like network issue or terminiation), the ignore and continue with the rest of the Repositories/Services, instead of failing the launching.

Any idea of how to achieve this?

question from:https://stackoverflow.com/questions/65866662/soft-exception-handling-of-beancreationexception-for-spring-jpa-repository

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Usually this kind of things can be achieved by using @Conditional and its derivatives.

In one sentence this feature allows loading the bean (and making it available for injection into other beans) only if it obeys some condition. Usually its some kind of property or maybe presence of another bean, however in a nutshell you can define an implementation by yourself.

This is a tutorial about conditionals. More precisely you'll need a paragraph about the custom conditions

So you might define the condition that will "check the availability of the data source" and will return true from its matches method.

One caveat is that if you don't load, say DAO because the condition is not matched, then the code of the service that might use this Dao will fail because there is no bean of Dao available for injection.

So you might want either to load the no-op dao implementation when the condition is not met, or maybe not load the service itself, this really depends on the real project implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...