user5245767
user5245767

Reputation: 9

When is an object created in Spring?

Are objects created at the time when the following line is executed?

ApplicationContext con = new ClassPathXmlApplicationContext("factory.xml");

or is the object obj created at the moment the following code is executed?

Sample obj = (Sample)con.getBean("id");

Upvotes: 0

Views: 91

Answers (2)

Rob
Rob

Reputation: 237

It depends on how the bean is defined. See this for scoping details:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes

If the bean is a proxy (e.g. scoped, aop) then instantiation will depend on other factors.

Upvotes: 1

morgano
morgano

Reputation: 17422

It depends on the scope of the bean, by default a bean is a singleton, and is created at the moment the first line is executed. If your bean specification has the scope "prototype", then a new bean will be created every time you execute the second line

Upvotes: 1

Related Questions