English 中文(简体)
Spring newbie: Bean 装载和电缆流
原标题:Spring newbie : Bean loading and wiring flow

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");
    }
}

我在此检验:

<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>

产出如下:

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

如果我把测试1的范围改为原型一:

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

我知道,我要求的是太多的,但有人可以让我了解这里所涉及的步骤——我不明白,为什么在装货头1之后(甚至在装装货2之前)就要求测试2组。

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

最佳回答

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

由于您的测试2 定型号在应用背景组合中提到了测试1,因此首先将予以援引。

页: 1

原型范围是一个小小小小trick,因为如果你的原型星群是代用的,其行为会有所不同。

如果你像你一样直接从申请环境中获得原型灯,你将每次得到一次新的检查。 给初审指定名字不会影响最后书写的二审的名字。

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

但是,请核实,因为我假定这样做。 我没有通过法典加以核实。

问题回答

暂无回答




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签