{"id":336,"date":"2020-05-27T11:00:00","date_gmt":"2020-05-27T18:00:00","guid":{"rendered":"https:\/\/www.tizianasellitto.it\/blog\/?p=336"},"modified":"2020-05-28T18:31:01","modified_gmt":"2020-05-29T01:31:01","slug":"spring-bean-singleton-scope","status":"publish","type":"post","link":"https:\/\/www.tizianasellitto.it\/blog\/?p=336","title":{"rendered":"Spring Bean Singleton Scope"},"content":{"rendered":"\n<p class=\"has-text-align-justify\">In this post I&#8217;m going to implement a small demo to show the behavior of the Singleton Scope applied to a Bean in a Spring Boot Application. Before doing that let&#8217;s think about what <em>beans<\/em> in the Spring world are.<\/p>\n\n\n\n<p>The <em>beans<\/em> are the backbone of an application. They are objects instantiated, assembled and managed by a Spring IoC container of which&nbsp;the <em>ApplicationContext<\/em> interface is responsible for. <\/p>\n\n\n\n<p class=\"has-text-align-justify\">The scope of a bean defines the life cycle and visibility of that bean in the contexts in which it is used. The <a href=\"https:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/core.html#beans-factory-scopes\">latest version<\/a> of Spring framework defines 6 types of scopes: <\/p>\n\n\n\n<ul><li>singleton<\/li><li>prototype<\/li><li>request<\/li><li>session<\/li><li>application<\/li><li>websocket<\/li><\/ul>\n\n\n\n<p>For now I&#8217;m going to focus on the first one <em><strong>Singleton Scope<\/strong><\/em>. Using singleton scope (the default scope) the container will create a single instance of that bean and all request for that bean will return the same object. That means that all modification will be reflected in all references to that bean.<\/p>\n\n\n\n<p>Let&#8217;s create from an <em>Address<\/em>, a <em>Customer<\/em> and an <em>Employee<\/em> entity. <\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"320\" height=\"138\" src=\"https:\/\/www.tizianasellitto.it\/blog\/wp-content\/uploads\/2020\/05\/1858ac3e.png\" alt=\"Class Diagram\" class=\"wp-image-338\" srcset=\"https:\/\/www.tizianasellitto.it\/blog\/wp-content\/uploads\/2020\/05\/1858ac3e.png 320w, https:\/\/www.tizianasellitto.it\/blog\/wp-content\/uploads\/2020\/05\/1858ac3e-300x129.png 300w\" sizes=\"(max-width: 320px) 100vw, 320px\" \/><\/figure><\/div>\n\n\n\n<p>Let&#8217;s also define the @Configuration annotated class, where to specify the initialization of the Customer and Employee bean to use the Singleton Bean Address.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration\npublic class Config {\n\t@Bean\n\t@Scope(\"singleton\")\n\tpublic Address personSingleton() {\n\t\treturn new Address();\n\t}\n\n\t@Bean\n\tpublic Customer customer(Address address) {\n\t\tCustomer customer = new Customer();\n\t\tcustomer.setAddress(address);\n\t\treturn customer;\n\t}\n\n\t@Bean\n\tpublic Employee employee(Address address) {\n\t\tEmployee employee = new Employee();\n\t\temployee.setAddress(address);\n\t\treturn employee;\n\t}\n}<\/code><\/pre>\n\n\n\n<p>If we execute this  Junit Test we can see that the hashCode for these 2 objects  is the same and that if we change the value of one of the Address fields both reference to the Address beans will have the updated value as well as all objects that use that reference of the bean.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@SpringBootTest\nclass ScopeDemoApplicationTests {\n\tprivate static final String CITY = \"San Francisco\";\n\n\t@Autowired\n\tprivate ApplicationContext context;\n\n\t@Test\n\tpublic void givenSingletonScope_whenSetName_thenEqualNames() {\n\n\t\tAddress personSingletonA = (Address) context.getBean(\"personSingleton\");\n\t\tAddress personSingletonB = (Address) context.getBean(\"personSingleton\");\n\n\t\tCustomer customer = (Customer) context.getBean(\"customer\");\n\t\tEmployee employee = (Employee) context.getBean(\"employee\");\n\n\t\tpersonSingletonA.setCity(CITY);\n\t\tAssert.assertEquals(CITY, personSingletonB.getCity());\n\n\t\tAssert.assertEquals(personSingletonA.hashCode(),\n                                    personSingletonB.hashCode());\n\n\t\tAssert.assertEquals(customer.getAddress().hashCode(),\n                                    employee.getAddress().hashCode());\n\t}\n}<\/code><\/pre>\n\n\n\n<p>The code for this example is available in <a href=\"https:\/\/github.com\/tizianasellitto\/spring-demo\/tree\/master\/spring-scope\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I&#8217;m going to implement a small demo to show the behavior of the Singleton Scope applied to a Bean in a Spring Boot Application. Before doing that let&#8217;s think about what beans in the Spring world are. The beans are the backbone of an application. They are objects instantiated, assembled and managed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[32,35,34],"_links":{"self":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336"}],"version-history":[{"count":10,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}