File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Controllers singleton or not?

 
Fawad Ali
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I know that spring beans are singleton by default. I want to know whether Spring controllers are singleton or a new instance is created for each user?
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still a singleton, and proud of it. ;)

Mark
 
kunalkumar somani
Greenhorn
Posts: 7
Eclipse IDE Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fawad Ali,

By default it always singleton.

There are many ways to check your bean is singleton or not. For your confirmation if you want to check just create a Standalone application and load your configuration file from BeanFactory or ApplicationContext thn get your bean more thn one time using getBean() method.

And finally print object and check the address of that objects.It should be same. For example you can see below code in main method....


Resource resource = new FileSystemResource("com/spring/example/spring.cfg.xml");
BeanFactory factory = new XmlBeanFactory(resource);

Singleton singleton = (Singleton) factory.getBean("singleton");

Singleton singleton1 = (Singleton) factory.getBean("singleton");

System.out.println("Address 1=>"+singleton);
System.out.println("Address 2=>"+singleton1);

xml file has...

<bean id="singleton" class="com.example.Singleton">
</bean>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic