简体   繁体   English

万一我们通过aop作用域代理将原型bean注入到singleton bean中,getter的工作原理(Singleton Bean)是什么?

[英]How getter work (Singleton Bean) in case, we inject prototype bean into singleton bean via aop scoped proxy?

I have class Employee which is singleton as defined in spring.xml 我有Employee类,如spring.xml中定义的单例

public class Employee{
private Vehicle vehicle;
public Vehicle getVehicle() {
    return vehicle;
}
public void setVehicle(Vehicle vehicle) {
    this.vehicle = vehicle;
}
}

I have class Vehicle which is prototype as defined in spring.xml 我有Vehicle类,它是spring.xml中定义的原型

public class Vehicle {
private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

Below is spring.xml 下面是spring.xml

<bean id="employee" class="com.example.factory.Employee">
<property name="vehicle" ref="vehicle"></property>
</bean>
<bean id="vehicle" class="com.example.factory.Vehicle" scope="prototype">
<property name="name" value="car"></property>
<aop:scoped-proxy />
</bean>

Now I know spring will create proxy for vehicle. 现在我知道spring将为车辆创建代理。 Every time I call getVehicle() on employee object I get new object of Vehicle. 每次我在员工对象上调用getVehicle()时,都会得到Vehicle的新对象。 But in getVehicle() method I am not creating new object of Vehicle and as per my understanding spring is not creating proxy for Employee object. 但是在getVehicle()方法中,我没有创建Vehicle的新对象,并且据我所知,Spring没有为Employee对象创建代理。 So someone please make me understand in detail what is happening internally and how getVehicle() is working? 所以有人请让我详细了解内部正在发生什么以及getVehicle()如何工作?

You don't get a new instance of Vehicle every time you call getVehicle(). 每次调用getVehicle()时,不会获得Vehicle的新实例。 You get a new instance of Vehicle every time Spring must provide one. 每当Spring必须提供一个新的Vehicle实例时,您都会得到一个。 That could happen in two ways: 这可能以两种方式发生:

  1. You ask Spring for a Vehicle bean 你问春天的车豆
  2. Spring autowires a Vehicle to the Employee bean. Spring将Vehicle自动连线到Employee bean。 This happens only once, since Employee is a singleton. 由于Employee是单身人士,因此仅发生一次。 So if this is the only way that Vehicle is used, it might as well be a singleton, too. 因此,如果这是使用Vehicle的唯一方法,那么它也可能是单例。

See this page for a more detailed explanation. 请参阅此页面以获得更详细的说明。

Below is my findings on this 以下是我对此的发现

I have changed the bean definition for vehicle by removing the initialization as below. 我通过删除以下初始化更改了车辆的bean定义。

<bean id="employee" class="com.emp.Employee">
<property name="vehicle" ref="vehicle"></property>
</bean>

<bean id="vehicle" class="com.emp.Vehicle" scope="prototype">
<aop:scoped-proxy />
</bean>

And I have created a test class for this scenario 我为此场景创建了一个测试类

See below the code 见下面的代码

public class TestScope {

    @Autowired
    Employee employee = null;

    @Test
    public void testScope()
    {

        employee.getVehicle().setName("bike");
        System.out.println("vehicle name:"+employee.getVehicle().getName());


    }
    }

With the above code running , the output is below 运行以上代码后,输出如下

vehicle name:null

But if I change the scope of the Vehicle to default (singleton) class I am getting the following result 但是,如果我将Vehicle的范围更改为默认(单例)类,则会得到以下结果

vehicle name:bike

So to conclude, for each employee.getVehicle() there is a new instance of the Vehicle created since it is explicitly stated in the bean definition to do so , and the proxy will refer this object. 综上所述,对于每个employee.getVehicle(),都有一个创建的Vehicle的新实例,因为在bean定义中已明确声明要这样做,并且代理将引用该对象。 But if we remove the scope definition , it will be singleton and will create a single object and that will remain the same through out the life cycle of the bean. 但是,如果我们删除范围定义,它将是单例的,并且将创建一个对象,并且在Bean的整个生命周期中都将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM