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

  • Home
  • SEARCH
  • 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 4239578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:03:12+00:00 2026-05-21T03:03:12+00:00

Spring provides the FactoryBean interface to allow non-trivial initialisation of beans. The framework provides

  • 0

Spring provides the FactoryBean interface to allow non-trivial initialisation of beans. The framework provides many implementations of factory beans and — when using Spring’s XML config — factory beans are easy to use.

However, in Spring 3.0, I can’t find a satisfactory way of using factory beans with the annotation-based configuration (née JavaConfig).

Obviously, I could manually instantiate the factory bean and set any required properties myself, like so:

@Configuration
public class AppConfig {

...

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource());
        factory.setAnotherProperty(anotherProperty());

        return factory.getObject();
    }

However, this would fail if the FactoryBean implemented any Spring-specific callback interfaces, like InitializingBean, ApplicationContextAware, BeanClassLoaderAware, or @PostConstruct for example. I also need to inspect the FactoryBean, find out what callback interfaces it implements, then implement this functionality myself by calling setApplicationContext, afterPropertiesSet() etc.

This feels awkward and back-to-front to me: application-developers should not have to implement the callbacks of the IOC container.

Does anyone know of a better solution to using FactoryBeans from Spring Annotation configs?

  • 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-21T03:03:13+00:00Added an answer on May 21, 2026 at 3:03 am

    As far as I understand your problem is what you want a result of sqlSessionFactory() to be a SqlSessionFactory (for use in other methods), but you have to return SqlSessionFactoryBean from a @Bean-annotated method in order to trigger Spring callbacks.

    It can be solved with the following workaround:

    @Configuration 
    public class AppConfig { 
        @Bean(name = "sqlSessionFactory")
        public SqlSessionFactoryBean sqlSessionFactoryBean() { ... }
    
        // FactoryBean is hidden behind this method
        public SqlSessionFactory sqlSessionFactory() {
            try {
                return sqlSessionFactoryBean().getObject();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    
        @Bean
        public AnotherBean anotherBean() {
            return new AnotherBean(sqlSessionFactory());
        }
    }
    

    The point is that calls to @Bean-annotated methods are intercepted by an aspect which performs initialization of the beans being returned (FactoryBean in your case), so that call to sqlSessionFactoryBean() in sqlSessionFactory() returns a fully initialized FactoryBean.

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

Sidebar

Related Questions

Is there any web framework in C++ which is like Spring for Java, i
My spring web application is using ajax with spring, and it based in general
Ive got a question to Spring Security 3.0.5. When using Spring Security to secure
I am using Spring MVC and want to develop a website that uses REST
I am implementing spring security framework in my web app. I have all the
This is a follow-on to my question Spring Web MVC - validate individual request
Spring 3 includes the ContentnegotiatingViewResolver which can be used to decide on the views
Im looking for some tool that provides pub/sub model, but instead string channels allows
Spring ROO have built-in email support So you can setup email sending properties with
I have an Oracle database that runs a PL/SQl job once every week to

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.