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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:53:36+00:00 2026-05-25T14:53:36+00:00

I have a question regarding component scanning and the use of an additional bean-definition

  • 0

I have a question regarding component scanning and the use of an additional bean-definition in the XML configuration for the same service class in Spring Version 3.0.5.RELEASE.

I created a class “MyService” annotated with

@Service( "myService" )

and there is a bean definition for the same class that looks like this:

<bean id="myService" class="....MyService" />

I put a

System.out.println( "MyService has been instantiated" );

message into the constructor of the MyService class. That message will be displayed at the console only once, when my application starts. Spring seems smart enough to detect, that there can be only one instance of that class with the id “myService”.

But even when I change the annotation to

@Service( "myService2" )

and leave the xml bean definition id to “myService”, the class will only be instantiated once.

My two questions are:

  1. Although that is the behavior that I currently want, is it possible to mix up XML bean definition and component scanning for one class, to have it instantiated twice with different Bean IDs?

  2. Does anyone know, what the mechanism is “behind the scenes” in Spring? How does Spring decide when to instantiate a class only once? (Maybe it is just not allowed to mix component scanning and XML bean definition for the same class?)

Thanks a lot for your help!

  • 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-25T14:53:36+00:00Added an answer on May 25, 2026 at 2:53 pm

    Your question 2. can be answered easily. Spring instantiates beans depending on their scope. The default scope is “singleton”, no matter if you use xml or annotation-based configuration. If you declare a bean as scope=prototype, it will get instantiated every time the container is asked for the bean.

    As annotation:

    @Scope(StandardScopes.PROTOTYPE)
    

    In xml:

    <bean .... scope="prototype">
    

    If you need exactly two instances of a bean, the best would be to declare two of them with dfiferent names in XML I would think.

    Concerning your question 1, I don’t know if it is possible to declare two instances of a class, one using annotation and one using XML. Are you sure that classpath scanning really works in your example? Leave out the XML bean declaration and try again, do you have an instance of the bean then? If not, you should first fix the classpath scanning as it seems not to work.

    Anyway, the most important question: Why would you want to do that? Mixing both types of configuration will create somewhat unmaintainable code. Don’t mix the configuraton types, go for only one, or tell us why the heck you would need that.

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

Sidebar

Related Questions

I have a question regarding primefaces datatable component. I want to bind a DataTable
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding dependency injection. say i want to create a class
Following on from my recent question regarding parsing XML files in Java I have
I have question regarding disabling browser caching. I have already found few solutions, and
I have a question regarding static function in php. let's assume that I have
I have a question regarding the onkeypress event on JavaScript. Is it possible to
I have a question regarding the integration of business objects developed using Linq To
I have a question regarding a window style hexadecimal. According to http://support.microsoft.com/kb/111011/en-us , 0x16CF0000

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.