Adelin
Adelin

Reputation: 18961

Spring vs Guice instantiating objects

I recently started learning Spring. As I am new to Spring several question comes to mind of mine. One of them is this:

As stated here " All beans are instantiated as soon as the spring configuration is loaded by a container. org.springframework.context.ApplicationContext container follows pre-loading methodology." LINK

1 - Does this mean that all objects created with Spring ApplicationContext are Singletons ?

I created this simple test

@Component
public class HelloService {

 private ApplicationContext context;


 public HelloService() {
 }

 @Autowired
 public HelloService(ApplicationContext context) {
  this.context = context;
 }

 public String sayHello() {

  return "Hi";
 }

}

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


  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  Injector injector = Guice.createInjector(new AbstractModule() {
   @Override
   protected void configure() {
   }
  });
  HelloService helloService1 = context.getBean(HelloService.class);
  System.out.println(helloService1);
  HelloService helloService2 = context.getBean(HelloService.class);
  System.out.println(helloService2);

  HelloService helloService3 = injector.getInstance(HelloService.class);
  System.out.println(helloService3);
  HelloService helloService4 = injector.getInstance(HelloService.class);
  System.out.println(helloService4);

 }
}

The out put was

foo.bar.HelloService@191e8b08 // same instance 
foo.bar.HelloService@191e8b08 // same instance

foo.bar.HelloService@6ba67ab5 // different instance 
foo.bar.HelloService@7ec23849 // different instance

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <context:annotation-config/>
  <context:component-scan base-package="foo.bar"/>
</beans> // this is mu config bean. 

In Guice you have to explicitly say that you want this object to be instantiated a singleton .

2- Doesn't this create some problems when Spring create objects that keep some state ?

3- How to tell Spring to create a new object ?

Upvotes: 0

Views: 953

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

1 - Does this mean that all objects created with Spring ApplicationContext are Singletons ?

No. But Spring's default scope is singleton. If you want a different scope, you must explicitly declare it in the bean configuration. In Java configuration, you do that with the @Scope annotation. With XML configuration, you do it with the <bean> scope attribute. Among other ways...

2- Doesn't this create some problems when Spring create objects that keep some state ?

You can always declare different scope, so no.

3- How to tell Spring to create a new object ?

That depends on the scope. If the scope is prototype, then calling ApplicationContext#getBean(String) will give you a new instance every time. If your bean is a singleton, then you will always get the same instance.

Note that you can have multiple beans of the same type but with different scopes. For example,

<bean name="my-proto" class="com.example.Example" scope="prototype" />
<bean name="my-singleton" class="com.example.Example" /> <!-- defaults to singleton -->

and later

(Example) context.getBean("my-proto"); // new instance every time
(Example) context.getBean("my-singleton"); // same instance every time

You can therefore use the singleton in some cases, and a different scope in others. Also, you don't have to use Spring everywhere.

Upvotes: 1

Related Questions