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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:16:06+00:00 2026-05-28T04:16:06+00:00

I have just started reading Spring In Action – Third edition and am stuck

  • 0

I have just started reading “Spring In Action – Third edition” and am stuck up while experimenting with wiring concepts. I am unable to understand the lifecycle of a bean after writing this code :

    public class TestCase {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
        Test1 t1 = (Test1)context.getBean("test1");
        t1.setName1("Win");
        Test1 t2 = (Test1)context.getBean("test1");
        t2.setName1("Lin");
        Test2 t3 = (Test2)context.getBean("test2");
        Test1 t4 = t3.getName();
        System.out.println("End" +t4.getName1());

    }

}

public class Test1 {

    private String name1;

    public String getName1() {
        System.out.println("test1 - getter");
        return name1;
    }

    public void setName1(String name1) {
        System.out.println("test1 - setter");
        this.name1 = name1;
    }

    public void onStart()
    {
        System.out.println("start1");
    }

    public void onStop()
    {
        System.out.println("stop1");
    }
}


public class Test2 {

    private int age;
    private Test1 name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Test1 getName() {
        System.out.println("test2 - getter");
        return name;
    }
    public void setName(Test1 name) {
        System.out.println("test2 - setter");
        this.name = name;
    }

    public void onStart()
    {
        System.out.println("start2");
    }

    public void onStop()
    {
        System.out.println("stop2");
    }
}

Here’s my test.xml :

<bean id="test1" class="springidol.Test1" init-method="onStart" destroy-method="onStop" >
</bean>

<bean id="test2" class="springidol.Test2" init-method="onStart" destroy-method="onStop">
<property name="name" ref="test1"></property>
</bean>

The output is :

   start1
test2 - setter
start2
test1 - setter
test1 - setter
test2 - getter
test1 - getter
End - Lin

If I change the Test1 scope to prototype I get :

start1
test2 - setter
start2
start1
test1 - setter
start1
test1 - setter
test2 - getter
test1 - getter
End - null

I know I am asking for too much, but can someone get me the steps involved here – I cant understand why test2 setter is getting called after loading of tst1 is done (and even before test2 is loaded) !

Secondly, why are the “End” outputs for prototype and default scopes different ?
Thanks.

  • 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-28T04:16:07+00:00Added an answer on May 28, 2026 at 4:16 am

    Because Spring does set the references to other beans first. (Wiring of the beans)
    After that the init methods will be invoked.

    Since your test2 setter points to a reference of test1 within your application context configuration it will be invoked first.

    ———-edit————

    The prototype scope is a little bit tricky, since it behaves different depending if your prototype scoped bean is a proxy or not.

    If you get the prototyped bean from the application context directly, like you did, you will get a new instance each time. Setting a name to the first instance will not affect the name of the second instance you write out at the end.

    But if a prototype scoped bean is referenced within the application context from another singleton bean, a proxy is injected. This proxy will even switch the actually invoked instance for every method call on it. This will be more irritating since:

    Test1 t4 = t3.getName(); 
    t4.setName1("lala");
    assertEquals(null,t4.getName1()); //will be true
    

    But please verify that, since thats what I suppose it is doing. I did not verified it by code.

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

Sidebar

Related Questions

I have just started learning Objective-C, I am reading Programming in Objective-C 3rd Edition
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
We have a bunch of data on S3 (images) but just started reading about
I just have started to learn Haskell and combine reading books and tutorials with
I am not a C programmer. I have just started reading K&R's TCPL last
I have just started reading a Hibernate book and it says go to Hibenrate.org
I have just started learning ruby reading from different resources. One of them is
I've only just really started android developing and have been reading about services here
I just started reading this book Eloquent Ruby and I have reached the chapter
I have just started reading about threading in C, using pthreads. I know that

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.