1

I have class Employee which is singleton as defined in 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

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

Below is 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. Every time I call getVehicle() on employee object I get new object of 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. So someone please make me understand in detail what is happening internally and how getVehicle() is working?

lavish
  • 96
  • 5
  • actually the `getVehicle()` always returns the same object which is actually a proxy for an actual `Vehicle` instance. For each method call you do on the `Vehicle` you will get a new instance because that is what you told it to do with the scoped-proxy and `scope=prototype`. Basically a scoped proxy doesn't make sense for `scope=prototype` only for `request` and `session` scoped (and some others that aren't provided by default). – M. Deinum Feb 02 '16 at 10:47
  • if `getVehicle()` always returns the same object, then why on executing this statement `System.out.println(getVehicle())` results in different hashcode everytime? – lavish Feb 02 '16 at 17:56
  • Because it returns a proxy instead of the actual object, the proxy is always the same. The `hashCode` method is passed on to the actual underlying object, (basically using `hashCode` to check if it is the same object isn't a very good thing to do especially not when proxies are in play). – M. Deinum Feb 03 '16 at 06:33

2 Answers2

1

You don't get a new instance of Vehicle every time you call getVehicle(). You get a new instance of Vehicle every time Spring must provide one. That could happen in two ways:

  1. You ask Spring for a Vehicle bean
  2. Spring autowires a Vehicle to the Employee bean. This happens only once, since Employee is a singleton. So if this is the only way that Vehicle is used, it might as well be a singleton, too.

See this page for a more detailed explanation.

Ken Clubok
  • 1,238
  • 6
  • 11
  • Well yes and no.. It is a scoped proxy, with scope prototype, which in this case means a scoped proxy is injected and each method call on the `Vehicle` will lead to a new instance. The key here is the fact that it is a scoped-proxy and not a regular prototype bean. – M. Deinum Feb 02 '16 at 10:45
0

Below is my findings on this

I have changed the bean definition for vehicle by removing the initialization as below.

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

vineeth sivan
  • 510
  • 3
  • 18