{"id":449,"date":"2021-09-13T09:58:29","date_gmt":"2021-09-13T07:58:29","guid":{"rendered":"http:\/\/blog.xoupix.fr\/?p=449"},"modified":"2021-09-14T07:30:45","modified_gmt":"2021-09-14T05:30:45","slug":"spring-bean-in-a-nutshell","status":"publish","type":"post","link":"https:\/\/blog.xoupix.fr\/index.php\/2021\/09\/13\/spring-bean-in-a-nutshell\/","title":{"rendered":"Spring Bean in a nutshell"},"content":{"rendered":"\n<p>Let&#8217;s get started with Spring Bean !<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>Bean is a key concept of the Spring Framework. Understanding this specific part of Spring Framework is crucial to use it efficiently.<\/p>\n\n\n\n<p>In this post, we will go throught this Spring concept.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Spring Bean ?<\/h2>\n\n\n\n<p>From the official Spring documentation :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.<\/p><\/blockquote>\n\n\n\n<p>In Spring, bean is fully part of the IoC container, or &#8220;Inversion of Control&#8221; :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then&nbsp;<em>injects<\/em>&nbsp;those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name&nbsp;<em>Inversion of Control<\/em>&nbsp;(IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the&nbsp;<em>Service Locator<\/em>&nbsp;pattern.<\/p><\/blockquote>\n\n\n\n<p>The &#8220;Inversion of Control&#8221; process allows you to create instances, without creating them using the <em>new<\/em> Java instruction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java sample<\/h2>\n\n\n\n<p>In this post, we will use the following simple class :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Person {\n    \n    private String firstName;\n\n    private String lastName;\n\n    private Address address;\n\n    public Person() {\n\n    }\n\n    public Person(String firstName, String lastName, Address address) {\n        setFirstName(firstName);\n        setLastName(lastName);\n        setAddress(address);\n    }\n\n    public String getFirstName() {\n        return this.firstName;\n    }\n\n    public void setFirstName(String firstName) {\n        this.firstName = firstName;\n    }\n\n    public String getLastName() {\n        return this.lastName;\n    }\n\n    public void setFirstName(String lastName) {\n        this.lastName = lastName;\n    }\n\n    public Address getAddress() {\n        return this.address;\n    }\n\n    public void setAddress(Address address) {\n        this.address= address;\n    }\n}\n\npublic class Address {\n\n    private String street;\n\n    private int number;\n\n    public Address() {\n\n    }\n\n    public Address(String street, int number) {\n        setStreet(street);\n        setNumber(number);\n    }\n\n    public String getStreet() {\n        return this.street;\n    }\n\n    public void setStreet(String street) {\n        this.street= street;\n    }\n\n    public int getNumber() {\n        return this.number;\n    }\n\n    public void setNumber(int number) {\n        this.number= number;\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Java way<\/h2>\n\n\n\n<p>In this example, we just want the following user :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>John Doe<br>123 Main St<br>Anytown, USA<\/p><\/blockquote>\n\n\n\n<p>Let&#8217;s do this :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Address address = new Address(\"Main St\", 123);\nPerson person = new Person(\"John\", \"Doe\", address);<\/pre>\n\n\n\n<p>As we can see in this approach, we are firstly creating the <em>address <\/em>instance, and then pass it to the <em>person <\/em>instance. Is there any alternative to do it ?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Spring bean and the xml approach<\/h2>\n\n\n\n<p>We will use the following xml :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;beans xmlns=\"http:\/\/springframework.org\/schema\/beans\"\n\txmlns:batch=\"http:\/\/springframework.org\/schema\/batch\" xmlns:task=\"springframework.org\/schema\/task\"\n\txmlns:util=\"http:\/\/springframework.org\/schema\/util\" xmlns:xsi=\"w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/springframework.org\/schema\/beans\n\t\thttp:\/\/springframework.org\/schema\/beans\/spring-beans-3.2.xsd\">\n\n    &lt;bean id=\"address\" class=\"my.package.Address\" \/>\n        &lt;property name=\"street\" value=\"Main St\" \/>\n        &lt;property name=\"number\" value=\"123\" \/>\n    &lt;\/bean>\n\n    &lt;bean id=\"person\" class=\"my.package.Person\" \/>\n        &lt;property name=\"firstName\" value=\"John\" \/>\n        &lt;property name=\"lastName\" value=\"Doe\" \/>\n        &lt;property name=\"address\" ref=\"address\" \/>\n    &lt;\/bean>\n&lt;\/beans><\/pre>\n\n\n\n<p>The Java part :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ApplicationContext ctx = new ClassPathXmlApplicationContext(\"beans.xml\");\nPerson person = ctx.getBean(\"person\", Person.class);<\/pre>\n\n\n\n<p>This xml file will produce, using the &#8220;Inversion of Control&#8221;, 2 beans. All beans are automatically created using the IoC, and can be retrieved using a <em>getBean <\/em>call.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Using Spring Bean, all instance declaration is moved from code to specific configuration file (or annotation). All objects are created by the Spring Framework, and can be retrieved independently.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s get started with Spring Bean !<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":8,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":458,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/posts\/449\/revisions\/458"}],"wp:attachment":[{"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xoupix.fr\/index.php\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}