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 9079999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:58:16+00:00 2026-06-16T19:58:16+00:00

I have some Spring managed classes (through xml configuration), one of which is a

  • 0

I have some Spring managed classes (through xml configuration), one of which is a SessionFactory, which is injected into another Spring managed class. Whenever this class needs a new Session it calls createSession on the SessionFactory.

However unless I’m mistaken this means that the Sessions themselves are not Spring managed, which is problematic as they have some @Transactional methods, which require the bean to be managed by Spring.

I’ve been reading up on FactoryBeans, but I’m not sure how the best way to do this is, especially as my createSession method takes a parameter, and the FactoryBean.getObject() does not.

I can use getObject and then set the parameter manually higher up, but I’d like to force the set in the factory if possible.

Can anyone help? Thanks in advance. A simplified example is below.

public class SessionFactory {

    public final Session createSession(String username){
        Session session = new SessionImpl(username);
        return session;
    }

}


public class SessionImpl implements Session{

    private String username;

    @Override
    @Transactional
    public void doSomething(){
        // Does something
    }

    public void setUsername(String username){
        this.username = username;
    }

    public String getUsername(){
        return username;
    }

}


public class Service {

    private SessionFactory sessionFactory; // Set by Spring through xml config

    public void doSomethingServicy(){

    }

    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }


}
  • 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-06-16T19:58:18+00:00Added an answer on June 16, 2026 at 7:58 pm

    I solved it by creating the factory bean in Spring, and declaraing a prototype-scoped Session object with a factory bean and factory method as seen below:

    <bean id="sessionFactory" class="com.SessionFactory" >
            <property name="dependencyA" ref="dependencyA" />
            <property name="dependencyB" ref="dependencyB" />
    </bean>
    
    <bean id=session" class="com.SessionImpl" factory-bean="sessionFactory" factory-method="createSession" scope="prototype" />
    

    Then retrieving a new instance in the code when required:

    Session session = (Session) applicationContext.getBean(SpringConstants.SESSION_BEAN_NAME, username);
    

    Here username is part of the Object... method parameter which makes up the arguments that get passed into the factories createSession method

    I appreciate the structure of the program could be better, but given the restrictions on the code it solves the problem nicely.

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

Sidebar

Related Questions

I have some problems sending mails through SMTP using Spring's MailSender interface and the
I have three apps in a Spring 2.5 managed project that share some code
I'm trying to have an abstract base class for some builder classes so I
I have some @javax.xml.bind.annotation.Xml... annotated classes here intended for a RESt web service. Jersey
I have a Spring application that I believe has some bottlenecks, so I'd like
I have a spring action that I am rendering some json from the controller,
I have Spring web application. I would like to put some common piece of
While implementing some security aspects with Spring Security, I have noticed that both Authentication
Currently we have an application who use spring who support mysql. Some people prefer
HI, I have some string from XML file, and I I want to replace

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.