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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:47:47+00:00 2026-06-11T06:47:47+00:00

I always have a question from the 1st day when I used spring. If

  • 0

I always have a question from the 1st day when I used spring. If a class has a constructor that needs two parameters, but these 2 parameters are not fixed, they are generated according to input request, every time they are different, but I need spring container to manage the class’s instance, how to achieve this in spring?
For example

    Class A{
     A(int x,int y){//omit}
}

but x, and y are not fixed,we need to calculate x and y by our program, then we can create instance for A, in ordinary java code,like below

    int x=calculate(request);
    int y=calculate(request);
    A a=new A(x,y);

But how to make spring manages the class A’s instance creation?

Additional information: Why I need Class A is managed by spring, because A depends on some other classes which are managed by spring.

  • 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-11T06:47:50+00:00Added an answer on June 11, 2026 at 6:47 am
    • The most straightforward way to do it is to use ApplicationContext.getBean(String name, Object... args) – it can create a prototype-scoped bean passing the given arguments to its constructor. Obviosly it’s not a good idea to use ApplicationContext directly in any bean that uses A.

    • A more elegant approach is to hide the creation of A behind a factory. That factory can use the previous approach internally, or it can obtain an instance of a bean in a regular way (Provider<A>, etc) and then call a non-public initialization method to pass that parameters (instead of passing parameters through using constructor).

    • Yet another apporach is to use @Configurable and load-time weaving that allows Spring to initialize objects created with new. Though it requires some extra configuration of runtime environment.

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

Sidebar

Related Questions

I always have used Spring Framework classes as-is. However I am thinking of customizing
I have an odd question that I have always thought about, but could never
I have found that Scala always has a natural explanation to anything. Always something
just a general question, do you ALWAYS have to handle error? i was just
I have a quick question about the accelerometer in Android devices. Is it always
I have a simple question, but I'm searching for longer time, but I always
I have two questions. 1) Should you always use a using statement on a
I have strings that use simple_format so they always have p tags surrounding them.
I have an ASP.NET 3.5 site that needs to export data to a pre-formatted
i have existing code that generates a spreadsheet from excel using OpenXML. It works

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.