Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:15:51+00:00 2026-05-31T11:15:51+00:00

I have declared a bean which is instantiated via a factory method. The factory

  • 0

I have declared a bean which is instantiated via a factory method. The factory method invokes some remote services. So, the method has been declared to throw Exception.

The bean is correctly being instantiated when everything works fine. However, when the factory method throws exception, then everything starts going wrong. because my declared bean is referenced from another bean.

I want to set a default value, if the factory method throws Exception.

Part of my config file is as follows :

<bean id="Helper" class="com.test.Helper">
   <constructor-arg ref="myBean" />
</bean>
<bean id="myBean" class="com.test.Factory" factory-method="getBean" />

the getBean() method is as follows:

Factory {
      public static Bean getBean() throws Exception{
             //Invokes some Remote Services and does some processing
             ....
             ....
            //returns bean object
      }

  }

Please help me how I can solve this. I am not allowed to modify the factory method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:15:52+00:00Added an answer on May 31, 2026 at 11:15 am

    If bean or factory method throws an exception during creation, the whole application context startup fails (I guess this is what you mean by “everything starts going wrong“). There is nothing you can do about it on the Spring side.

    If you want to return some default value, simply catch the exception in your custom factory, log it and return that default. If you cannot modify the existing factory, consider @Configuration approach:

    @Configuration
    public SafeCfg {
    
        @org.springframework.context.annotation.Bean
        public Bean bean() {
            try {
                return Factory.getBean();
            } catch(Exception e) {
                return //some default Bean
            }
        }
    
    }
    

    It works since Spring 3.0. Just place it somewhere so that the application context can pick it up.

    But I doubt this is what you want. Even when the external system becomes available, you’ll be still using the default, fallback value. Do you expect it to work like this? More advanced approach is to use lazy proxy (Spring has support for that: Is there a spring lazy proxy factory in Spring?) and initialize it only when needed and refresh when broken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stateless session bean which needs access to a factory class. Is
I have an java EE application which has one message-driven bean and it runs
I have a ServiceListFactoryBean which creates a list of service implementations: <bean id=services class=org.springframework.beans...ServiceListFactoryBean
I have declared one variable in xslt, assigned some value to it but while
When we say we have declared our form bean in session scope, few questions
I have a bean declared to be scope=request. is there a chance to obtain
In a <rich:popupPanel /> I have a <rich:fileUpload /> which has a fileUploadListener defined
I have a bean that extends this txProxyTemplate and inside it, this method orderUpdateOverseer
I have a managed bean declared in session scope. There is a drop down
I have been looking into some existing code of the company I am with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.