<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by hemasai jammana on Medium]]></title>
        <description><![CDATA[Stories by hemasai jammana on Medium]]></description>
        <link>https://medium.com/@hemasaijammana?source=rss-c17e12798752------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fa4ml1EuXqemHEax.jpg</url>
            <title>Stories by hemasai jammana on Medium</title>
            <link>https://medium.com/@hemasaijammana?source=rss-c17e12798752------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 18 May 2026 06:31:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hemasaijammana/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Spring MVC]]></title>
            <link>https://medium.com/@hemasaijammana/spring-mvc-33c2ba484e3c?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/33c2ba484e3c</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Thu, 06 Jul 2023 15:25:59 GMT</pubDate>
            <atom:updated>2023-07-06T15:25:59.667Z</atom:updated>
            <content:encoded><![CDATA[<p>complete spring MVC with basic CRUD operations project.</p><p>In this article, we will see what is spring MVC how we will develop our project using spring MVC and how it helps to build our applications fastly without focusing more on the configuration side.</p><p><strong>MVC architecture:</strong><br>In this type of architecture, we have a clear-cut separation of logic. All the code is in the form of <strong>M(Model) V(View) C(Controller).</strong></p><p><strong>Advantages of MVC architecture:</strong><br>1. As we have multiple layers there is clear-cut separation.<br>2. Modification in one layer will not impact the other.<br>3. Maintenance and enhancement are easy.<br>4. Industry standard approach.</p><p>We have a few frameworks that provide abstraction on top of MVC architecture. A few of the frameworks are:<br>1. Struts<br>2. JSF<br>3. Web Work<br>4. Spring MVC<br>5. ADF</p><p><strong>Advantages of using a framework with MVC architecture:</strong><br>1. Ready-made servlet controller, no need to write controller logic manually.<br>2. Ready-made servlet controller can trap all the incoming requests to the application.<br>3. Framework itself takes care of navigation and view Data management.<br>4. Avoided boilerplate code. (like configuring servlet for every)</p><p>We will see Spring MVC/ Spring Boot MVC in this part where the Ready-made servlet Controller/ Front Controller is <strong><em>DispatcherServlet.</em></strong></p><p>DispatcherServlet is already coded and made ready by the Spring team.<br>To use the dispatcher servlet we need to use the below annotations:<br>1. @Controller (spring Bean + Controller class)<br>2. @RequestMapping (controller class having request mapping and type)</p><pre>import org.springframework.stereotype.Controller;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web.bind.annotation.RequestMethod;<br><br>@Controller<br>public class DemoController {<br>    @RequestMapping(value = &quot;/print&quot;, method = RequestMethod.GET)<br>    public String printRequest() {<br>        return &quot;Hello&quot;;<br>    }<br>}</pre><p>The above is an example of a controller handler with Request mapping of <strong><em>“/print” </em></strong>and with method type as <strong><em>“GET”.</em></strong></p><p>Different values as method arguments are as below:<br>1. Servlet Request<br>2. Servlet Response<br>3. Path Variable<br>4. Request Param<br>5. Request Header<br>6. Request Attribute<br>7. Model Attribute<br>8. Session Attribute<br>9. Error, Binding result.</p><p>Different values as method responses are as below:<br>1. String<br>2. View<br>3. Model<br>4. Model and View<br>5. void<br>6. Model Attribute</p><blockquote>Note: In SpringBoot-MVC Apps the following components comes automatically so we<br>need not develop them<br>a. DispatcherServlet<br>b. IOC-Container<br>c. HandlerMapping<br>d. ViewResolver</blockquote><blockquote>To get all these we need to go for a starter file called “spring-<br>boot-starter-web”.</blockquote><p>The general flow of the spring MVC application is as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LSAHeZtuOm_m_bmXoySZyA.png" /><figcaption>Spring MVC flow</figcaption></figure><p>The main part of Spring MVC is the Dispatcher servlet which will trap all the incoming requests process and send the requests to a particular controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZPDWYXHvrWXuglDjXzFMYA.png" /><figcaption>Dispatcher Servlet</figcaption></figure><p>Few of the annotations are commonly used while developing Spring MVC applications.</p><ol><li><strong>@Controller:<br></strong>This annotation defines the class as a component and will treat it as a controller entry point for all incoming requests.<br>This annotation is always applied at the class level.<br><strong>ex:<br>@Controller<br>public class DemoController {<br>}</strong></li><li><strong>@RequestMapping:<br></strong>This annotation is used to define the end point for which the method annotated needs to be executed and the request type.<br>This annotation is used at the method level.<br><strong>ex:<br>@RequestMapping(value = “/print”, method = RequestMethod.<em>GET</em>)</strong><br>To simplify more we also have <br>@GetMapping<br>@PostMapping<br>@PatchMapping<br>@DeleteMapping<br>for the above annotations, no need to specify the RequestMethod, the annotation itself resembles the method type.</li><li><strong>@RequestHeader:<br></strong>This annotation is used to get any parameter from the header in the incoming request. <br>This is used as a parameter for the method.</li><li><strong>@RequestBody:<br></strong>This annotation is used to get the request body from API convert to a given model or POJO specified and store the value.<br>This is used as a parameter for the method.</li><li><strong>@PathVaribale:<br></strong>This annotation is used to get any value from the path specified in the URL.<br>This is also used as a parameter for the method.</li><li><strong>@QueryParam:<br></strong>This annotation is used to get any query parameters provided in the URL. <br>This is also used as a parameter.</li></ol><pre>@Controller<br>public class DemoController {<br>    @RequestMapping(value = &quot;/student/{studentId}&quot;, method = RequestMethod.GET)<br>    public Student printStudent(<br>            @RequestHeader String authorization,<br>            @RequestBody Student student,<br>            @PathVariable String studentId<br>    ) {<br>        return student;<br>    }<br>}</pre><p>Attaching the student project which contains the CRUD operations<br>1. creating a student.<br>2. updating a student based on Id.<br>3. get students based on Id.<br>4. delete student based on Id.<br>5. get all the students.</p><p><a href="https://github.com/HemasaiJ/SpringBoot/tree/master">Project Link</a>, will be updated.</p><p>Please do follow for more articles and learn with me.<br>you can connect with me here. <a href="https://www.linkedin.com/in/hemasai-jammana-55aa7117a/">linkedin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33c2ba484e3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monolithic vs MicroService Architecture]]></title>
            <link>https://medium.com/@hemasaijammana/monolithic-vs-microservice-architecture-f79cbf46449f?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/f79cbf46449f</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Sun, 18 Jun 2023 13:47:18 GMT</pubDate>
            <atom:updated>2024-02-13T03:55:00.224Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will learn about monolith and microservice architectures and see the key differences between them.</p><p><strong>Monolithic Architecture:<br></strong>In this type of architecture, complete code is maintained at only one server. All the different types of business logic are handled in one server. This can scale horizantally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*dZ9MgbEC0JajkJwHfjWnwg.png" /><figcaption>Monolithic architecture</figcaption></figure><p><strong>Advantages:<br>- </strong>we can scale horizontally.<strong><br>- </strong>This is fast as there are no network calls involved.<br>- simple to scale horizontally.</p><p><strong>Disadvantages:<br>- </strong>Small change also need to deploy all the code to reflect the change.<br>- A lot of testing is required as all the code is impacted even if small changes.<br>- If service is down everything is down.<br>- Handling code will be difficult as it is in one repository.<br>- limitation of size and complexity.<br>- Too hard to understand complete application.<br>- If any new technology adoption to new technology is difficult.</p><p><strong>Microservice Architecture:<br></strong>In this type of architecture, code is broken down into smaller services where each service will handle the logic individually and are inter connected. We need to use some service discovery mechanism to route the calls to individual services. service discoveries can be (eureka,API gateway etc.). The API Gateway is responsible for tasks such as load balancing, caching, access control, API metering, and monitoring.</p><blockquote>Database in this type of architecture can be single also where all the serives talk to single DB.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0-QTNDrWRAjx9F0Vsamq3Q.png" /><figcaption>microservice architecture</figcaption></figure><p><strong>Advantages:<br></strong>- Handling code will be easy.<br>- Faster deployment.<br>- No need to know about complete application, knowing about one service also we can work on it.<br>- Adapting to new technologies can be very easy.<br>- Different services can be in different programming languages.<br>- Cost effective sometimes. (We can scale only required service when there are any sales in Amazon they will scale the login services and payment services rest need not be scaled.)<br>- Small changes can be done easily.<br>- No need to test the entire application, only changes in particular services can be tested.</p><p><strong>Disadvantages:</strong><br>- Even if one service is down, the rest services will be running.<br>- Network calls are involved so there will be little latency involved while making any inter-service communication.</p><p>In summary choosing what type of architecture to use depends on many factors. Size of your team, how complex is your project, and many more.<br>It is important to understand monolithic architecture because internally in microservice architecture we will be using it. The Microservices architecture pattern is the better choice for complex, evolving applications.</p><p>Please do follow for more articles and learn with me.<br>you can connect with me here. <a href="https://www.linkedin.com/in/hemasai-jammana-55aa7117a/">linkedin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f79cbf46449f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maven]]></title>
            <link>https://medium.com/@hemasaijammana/maven-d882b91cbc60?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/d882b91cbc60</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Sat, 15 Apr 2023 14:44:50 GMT</pubDate>
            <atom:updated>2023-04-15T14:44:50.674Z</atom:updated>
            <content:encoded><![CDATA[<p>Complete information about Maven.</p><h4><strong>Introduction:</strong></h4><p>Project development involves a lot of steps that need to be done manually like downloading dependencies, adding paths to external dependencies organizing the files in proper directories, and a lot more.<br>Humans will make errors while doing these manual tasks.</p><p>What if we have some tool that will do all of these manual steps for us based on the commands? That tool is known as <strong>MAVEN</strong>.<br>Maven is a project management tool that manages all the manual steps for project development.</p><p>Maven uses <strong>POM (Project Object Model)</strong> to know about the complete details of the project like managing dependencies, adding plugins, and building the software.</p><h4><strong>Installing Maven:</strong></h4><ol><li>Download the zip file from Maven&#39;s official site <a href="https://maven.apache.org/download.cgi">https://maven.apache.org/download.cgi</a> and extract.</li><li>Create a maven path environment variable.</li><li>Create a JAVA_HOME path environment variable this is Java JDK.</li><li>Verify whether maven is installed with maven commands <br><em>mvn -version</em></li></ol><pre>Apache Maven 3.6.3<br>Maven home: /usr/share/maven<br>Java version: 11.0.18, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64<br>Default locale: en_IN, platform encoding: UTF-8<br>OS name: &quot;linux&quot;, version: &quot;5.15.0-69-generic&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;</pre><p>you need to get like this which contains details about Maven.</p><h4><strong>Creating Maven Project:</strong></h4><p>creating a maven project can be done 2 ways<br>1. using CLI (Command Line interface).<br>2. using any IDE(IntelliJ, Eclipse, Netbeans etc)</p><ol><li><strong>Using CLI:</strong><br>Go to the terminal and execute the below commands</li></ol><pre>mvn archetype:generate</pre><p>This will show all the different archetype projects available in a central repository we can select one and proceed. 2038 for the standalone project.<br>select and proceed.<br>select version number <br><strong>GroupId</strong> — a unique base name of the company or group that created the project.<br><strong>ArtificatId — </strong>a unique name of the project.<br><strong>Version — </strong>version of the project.<br><strong>Package</strong> — package name for the project.<br>Provide all the above information and confirm. A project with a given artifact name will be created in the working directory.<br> (or) we can use the below command to create.</p><pre>mvn archetype:generate -DgroupId=Hemasai -DartifactId=MavenTest -<br>DarchetypeArtifactId=maven-archetype-quickstart<br>-Dpackage=com.hemasai -Dversion=1.0 -DinteractiveMode=false</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*_ABNFZItRyauT9plMXDRxg.png" /><figcaption>Project Structure</figcaption></figure><pre>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;<br>  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;<br>    &lt;groupId&gt;Hemasai&lt;/groupId&gt;<br>    &lt;artifactId&gt;MavenTest&lt;/artifactId&gt;<br>    &lt;packaging&gt;jar&lt;/packaging&gt;<br>    &lt;version&gt;1.0&lt;/version&gt;<br>  &lt;name&gt;MavenTest&lt;/name&gt;<br>  &lt;url&gt;http://maven.apache.org&lt;/url&gt;<br>  &lt;dependencies&gt;<br>    &lt;dependency&gt;<br>      &lt;groupId&gt;junit&lt;/groupId&gt;<br>      &lt;artifactId&gt;junit&lt;/artifactId&gt;<br>      &lt;version&gt;3.8.1&lt;/version&gt;<br>      &lt;scope&gt;test&lt;/scope&gt;<br>    &lt;/dependency&gt;<br>  &lt;/dependencies&gt;<br>&lt;/project&gt;</pre><p>2. <strong>using any IDE:<br></strong>Go to any IDE and create a new project as a maven project. <br>Here also we need to give all the details GAV [GroupId, ArtificatId, Version].<br>Once the project gets created we can see the same pom.xml file as we have seen in the above part.</p><h4><strong>Maven Build LifeCycle and Phases:</strong></h4><p>Maven has 3 life cycles. Each life cycle contains number of phases<br>1. clean (3 phases)<br>2. default (23 phases)<br>3. site (4 phases).<br>We will be seeing the most important maven life cycle phases.</p><ul><li><em>validate</em> — checks the correctness of the project</li><li><em>compile</em> — compiles the provided source code into binary artifacts</li><li><em>test</em> — run the unit tests</li><li><em>package</em> — packages compiled code into a (jar, war, ear) file</li><li><em>integration-test</em> — executes additional tests, which require the packaging</li><li><em>verify</em> — checks if the package is valid</li><li><em>install</em> — installs the package file into the local Maven repository</li><li><em>deploy</em> — deploys the package file to a remote server or repository.</li></ul><h4><strong>Advantages:</strong></h4><ol><li>Can download jars automatically.</li><li>Dependency management.</li><li>Provides standard project directory structure.</li><li>Support Maven inheritance to share jar files and plugins among multiple projects.</li><li>Allows to develop multi-module projects</li><li>Build automation</li><li>can generate project documentation and run tests.</li><li>can clean and install the projects on local servers and remote servers.</li></ol><p><strong>References:<br></strong><a href="https://maven.apache.org/guides/getting-started/">https://maven.apache.org/guides/getting-started/</a><br><a href="https://maven.apache.org/guides/introduction/introduction-to-the-pom.html">https://maven.apache.org/guides/introduction/introduction-to-the-pom.html</a><br><a href="https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html">https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html</a></p><p>Please do follow for more articles and learn with me.<br>Feel free to connect with me on <a href="https://www.linkedin.com/in/hemasai-jammana-55aa7117a/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d882b91cbc60" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading Properties in Spring Boot]]></title>
            <link>https://medium.com/@hemasaijammana/reading-properties-in-spring-boot-ec2a65e371b?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec2a65e371b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[property]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Wed, 12 Apr 2023 14:31:45 GMT</pubDate>
            <atom:updated>2023-04-12T14:31:45.106Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@hemasaijammana/spring-vs-springboot-fe6f441adeaf">In the earlier articles,</a> we have seen about the difference between spring and Spring Boot. Now we will see how can we read the properties in Spring Boot Application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>We can read the properties in 2 ways in SpringBoot:<br>1. @Value<br>2. @ConfigurationProperties.</p><h4><strong>@Value:</strong></h4><p>This will perform the field-level injection. We can use it to inject each value into spring bean properties. This will inject only one value at a time.</p><blockquote><strong>By default, in Spring Boot all the properties are picked from the application.properties file in src/main/java/resources.</strong></blockquote><blockquote><strong>As application.properties is default file we no need to provide any path to read properties from any file</strong></blockquote><pre>@Component<br>public class Employee {<br><br>    @Value(&quot;${com.hemasai.name}&quot;)  // getting name from application.properties<br>    private String name;<br><br>    @Value(&quot;${com.hemasai.email}&quot;) // getting email from application.properties<br>    private String email;<br><br>  ......<br>}</pre><pre>application.properties<br><br>com.hemasai.name = &quot;hemasai&quot;<br>com.hemasai.email =&quot;testxml@gmail.com&quot;</pre><p>In the above application.properties file, we have given name and email and in the employee class, we inject these values using <strong>@Value.</strong></p><p>But now, if we want to read files from another file, we need to import that file and use the same @Value annotation. The file should also be present in the resources folder.</p><pre>input.properties<br><br>name = &quot;Hemasai&quot;<br>email = &quot;Testemail@gmail.com&quot;</pre><pre>@Component<br>@PropertySource(value = &quot;input.properties&quot;)  //reading from the mentioned file<br>public class Employee {<br>    @Value(&quot;${name}&quot;)<br>    private String name;<br>    @Value(&quot;${email}&quot;)<br>    private String email;<br><br>  ....<br>}</pre><p><strong>@PropertySource</strong> will import the given file into the respective bean, then we will be using the same @Value to get the property.</p><h4><strong>@ConfigurationProperties:</strong></h4><p>When we don’t need to use @Value annotation, then we can use this annotation on the class level and get the properties.<br>This will make use of setter methods to set the values.</p><pre>application.properties<br><br>com.hemasai.name = &quot;hemasai&quot;<br>com.hemasai.email =&quot;test@gmail.com&quot;</pre><pre>@Component<br>@ConfigurationProperties(value = &quot;com.hemasai&quot;)<br>public class Employee {<br><br>    private String name;  // getting com.hemasai.name from propeties file<br>    private String email; // getting com.hemasai.email from properties file<br>    ..<br>}</pre><p>As we see in the above code we are using the @ConfigurationProperties on the Employee class, this annotation will have some prefix default empty and add the variable name to the prefix and search with that key in the application.properties file.</p><p>In the above case com.hemasai is a prefix and the name is added to the prefix and com.hemasai.name is searched in the application.properties.</p><blockquote><strong>The properties can be read from the YAML file also.<br>Need to create on YAML file with required values and place in resources folder.</strong></blockquote><h4>Differences between Value and ConfigurationProperties:</h4><p><strong>Value:<br></strong>1. This can be used in both the spring framework and Spring Boot.<br>2. Single value injection.<br>3. Common prefixes are not required for all keys.<br>4. Keys in the properties file and property name need not match.<br>5. If some key is not found this will throw an error.<br>6. we can’t read properties using a YAML file.</p><p><strong>ConfigurationProperties:<br></strong>1. This can be used only in Spring Boot.<br>2. Bulk values are injected at once.<br>3. Common prefix is required for all the properties.<br>4. Key in the properties file and property name should match.<br>5. If some key is not found this will not inject anything and not throw an error.<br>6. we can read properties using the YAML file also.</p><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec2a65e371b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring vs SpringBoot]]></title>
            <link>https://medium.com/@hemasaijammana/spring-vs-springboot-fe6f441adeaf?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe6f441adeaf</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[difference]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Tue, 11 Apr 2023 03:31:48 GMT</pubDate>
            <atom:updated>2023-04-11T03:31:48.170Z</atom:updated>
            <content:encoded><![CDATA[<p>In the earlier articles, we have seen the spring core contents like Dependency Injection, Configuring the Spring container, Spring Bean, and many more. Now it&#39;s time to know the difference between Spring and Spring Boot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*fDiOuJ937RsefGN1me0XVg.png" /><figcaption>Spring vs SpringBoot</figcaption></figure><h4>Spring:</h4><ol><li>Spring is a framework for JEE technologies/ Application framework.</li><li>Dependency injection and Dependency lookup is the main feature in spring.</li><li>Spring supports XML-driven configuration as an input to the spring IOC container.</li><li>We(users) need to create an IOC container explicitly.</li><li>Allows developing spring apps using XML-based, Annotation based, and pure Java based.</li><li>Doesn&#39;t provide an embedded server to use in Web apps.</li><li>Doesn’t provide an embedded inMemory Database.</li><li>Lightweight because of no configuration.</li><li>No support for microservice architecture-based application development.</li><li>Spring requires developers to write more code for setting up the application, configuring dependencies, and managing infrastructure.</li></ol><h4>Spring Boot:</h4><ol><li>Spring Boot provides an abstraction for the spring framework and simplifies spring application development.</li><li>AutoConfiguration (providing common things automatically) is the main feature in spring boot.</li><li>Doesn’t support XML-based configuration as an input to the spring IOC container.</li><li>we(users) need not create an IOC container explicitly, it gets created automatically using <strong><em>SpringApplication.run().</em></strong></li><li>Supports only one style of configuration which is AutoConfiguration where inputs are supplied through the <strong><em>application.properties.</em></strong></li><li>provides an embedded server tomcat, and Jetty to use web applications.</li><li>provides an embedded inMemory database (H2).</li><li>Heavyweight because of AutoConfiguration.</li><li>support microservice architecture-based application development.</li><li>Spring Boot, provides a lot of pre-configured settings that developers can use to get started quickly and reduce boilerplate code.</li></ol><blockquote><strong>In simple words <br>spring framework + embededservers — Configuration = SpringBoot</strong></blockquote><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe6f441adeaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot Auto Driven Configuration]]></title>
            <link>https://medium.com/@hemasaijammana/spring-boot-auto-driven-configuration-c87653b44fe9?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/c87653b44fe9</guid>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Sun, 09 Apr 2023 11:45:10 GMT</pubDate>
            <atom:updated>2023-04-09T11:47:05.086Z</atom:updated>
            <content:encoded><![CDATA[<p>In the earlier articles, we have seen the spring configuration and how we can build the spring application. Now we will see the spring Boot Auto driven configuration style of building an application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>To perform dependency injection, we must provide <em>configuration metadata</em> to the spring IOC container.<br>There are 4 different ways of configuring metadata<br>1. <a href="https://medium.com/@hemasaijammana/spring-ioc-container-configuration-9143453b4f81">XML-based approach.</a><br>2. <a href="https://medium.com/@hemasaijammana/annotations-in-spring-29b9e58605a2">Annotation-driven configuration.</a><br>3. <a href="https://medium.com/@hemasaijammana/pure-java-based-configuration-in-spring-9eba9a788a24">Using Java code configuration.</a><br>4. Spring boot AutoDriven configuration.</p><p>We have already seen the 3 ways of configuring metadata to spring application in the earlier posts. Now we will see the Spring Boot auto-driven Configuration.<br>Spring Boot provides a feature called “auto-configuration” that helps developers avoid the need for boilerplate code when setting up a Spring application. Auto-configuration is a mechanism that automatically configures certain Spring components based on the dependencies that are present on the classpath.</p><p><strong>Why SpringBoot?:<br></strong>1. Build the applications rapidly.<br>2. No XML configuration.<br>3. Provide a range of non-functional features that are common to large classes of projects</p><h4>Developing Your First Spring Boot Application:</h4><p>spring provides UI to generate the spring boot application <a href="https://start.spring.io/">spring.start.io</a>.<br>we can generate the spring application by providing the required information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QO-S09pxG6r9bxkFYZ-NQQ.png" /><figcaption>spring initializer</figcaption></figure><p>Once we go to the above site we will get the options mentioned in the above image.</p><p><strong>Project:<br></strong>This indicates the build system of the project.</p><p><strong>Language:<br></strong>This indicates in which language we need the spring application.</p><p><strong>Spring Boot:<br></strong>This indicates the sprint boot version to use, always go for the release version don&#39;t go for <em>SNAPSHOT/ M2</em></p><p><strong>Project Metadata:<br></strong>This section needs the project information we need to create. Basic project information like what is the group, artifact, project name and description, and packaging mode.</p><p><strong>Dependencies:<br></strong>If we want any dependencies we can add them here for standalone projects we will not have any dependencies.<br>For web applications, we can have dependencies like Spring MVC, and Spring Data JPA that we can select from here.</p><p>Once we provide all the information Click on Generate and one project will be downloaded and we can import that project using any of the IDE which has build system support.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*Ni8TCtYg72G3OSFAXeBzPQ.png" /><figcaption>Spring Boot Project structure</figcaption></figure><p>We will have the project structure as shown in the above image. We will have the pom.xml file which contains information about the project and what dependencies this project needs etc.,</p><p>We will be having the main method in the spring application. This is a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s Spring Application class by calling run.</p><p>The main code will be in the TestApplication.java file</p><pre>@SpringBootApplication<br>public class TestAppApplication {<br><br> public static void main(String[] args) {<br>  SpringApplication.run(TestAppApplication.class, args);<br> }<br><br>}</pre><p><strong>@SpringBootApplication:<br></strong>This annotation is the main part of the Spring boot application this refers 3 annotations<br>1. <strong>@EnableAutoConfiguration:</strong> It enables auto-configuration.<br>2. <strong>@ComponentScan:</strong> This annotation scans for the stereotype annotations in the package and subpackage.<br>3. <strong>@SpringBootConfiguration:</strong> This is to make the current class a configuration class.</p><pre>SpringApplication.run(TestAppApplication.class, args);</pre><p>The above line will create the container for the spring application and make the container ready to get the beans.</p><blockquote><strong>SpringApplication.run() will internally use the AnnotationConfigApplicationContext to create an IOC container using the java class as @Configuration. In fact the same class will act like a configuration class (TestAppApplication.java).</strong></blockquote><pre>@SpringBootApplication<br>public class TestAppApplication {<br><br> public static void main(String[] args) {<br><br>  ConfigurableApplicationContext context<br>    = SpringApplication.run(TestAppApplication.class, args);<br><br>  context.getBean(Courier.class);<br>  context.close();<br> }<br><br>}</pre><p>We can get the beans using the same process as we are doing in other types of configuring the IOC container.</p><p><strong>Advantages of Spring Boot:<br></strong>1. No Xml file is involved in configuring the container.<br>2. Developing the application is too fast.<br>3. Readability and maintainability are easy.<br>4. Auto Configuration.<br>5. No need to create an IOC container explicitly.</p><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c87653b44fe9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pure Java-based Configuration in Spring.]]></title>
            <link>https://medium.com/@hemasaijammana/pure-java-based-configuration-in-spring-9eba9a788a24?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/9eba9a788a24</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Sat, 08 Apr 2023 05:47:49 GMT</pubDate>
            <atom:updated>2023-04-08T05:49:48.404Z</atom:updated>
            <content:encoded><![CDATA[<p>We have seen different ways of configuring the spring container in <a href="https://medium.com/@hemasaijammana/spring-ioc-container-configuration-9143453b4f81"><em>the earlier articles</em></a>. Now we will see the spring application using pure Java code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>To perform dependency injection, we must provide <em>configuration metadata</em> to the spring IOC container.<br>There are 4 different ways of configuring metadata<br>1. XML-based approach.<br>2. Annotation-driven configuration.<br>3. Using Java code configuration.<br>4. Spring boot AutoDriven configuration.</p><p>Out of these, we have already seen XML-based approach and Annotation-driven configuration, now we will see the Java code configuration.<br>In the XML-based and Annotation-driven configurations, we have used two containers Bean Factory and Application Context to get the beans.<br>Now to work on Java-based configuration Bean factory won’t work we should only work on Application Context.</p><p><strong>Application Context Container:<br>1. </strong>This is an extension of the bean factory.<br>2. Implementation classes of ApplicationContext(I) are below:<br> a. FileSystemXmlApplicationContext(standalone)<br> b. ClassPathXmlApplicationContext(standalone)<br> c. XmlWebApplicationContext(SpringMVC apps)<br> d. AnnotationConfigApplicationContext(Standaloneapp’s)<br> e. AnnotationConfigWebApplicationContext(SpringMVC apps)</p><p>We will use AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext based on the requirement when we are working using Java-based configuration.</p><p>In a pure Java-based configuration Spring application, the application context is configured entirely using Java code, rather than XML configuration files. This approach provides a more flexible and type-safe way to configure the application context and can make it easier to maintain and refactor the application.</p><p><strong>Steps / Rules to build Java-based projects easily:</strong><br>1. Annotate all the user-defined classes using stereotype annotations(@C<strong><em>omponent, @Service, @Repository</em></strong>) so the spring container can manage beans. (or)<br>we can also use the <strong>@Bean</strong> annotation in the configuration class to generate beans of the required class.</p><p>2. Add one configuration class and annotate with <strong><em>@Configuration </em></strong>and we can use <strong><em>@ComponentScan </em></strong>to scan all the beans in the provided base package.</p><blockquote>Note :- Configuration internally creates a component so the container can access this class and this class will become the configuration class for the application.</blockquote><pre>@Configuration<br>@ComponentScan(basePackages = &quot;src&quot;)<br>public class AppConfig {<br><br>.....//If any pre defined class need to provide beans information here.<br>}</pre><p>3. Configure the pre-defined classes using <strong><em>@Bean </em></strong>in the configuration class which is mentioned above.<br>4. Inject all the dependencies using the <strong><em>@Autowired</em></strong> annotation which we have seen in the earlier posts.<br>5. use AnnotationConfigApplicationContext class to create an IOC container having <strong><em>@Configuration</em></strong> class as the input class name</p><pre>public class TestApp {<br>    public static void main(String[] args) {<br><br>        ApplicationContext applicationContext = <br>                new AnnotationConfigApplicationContext(AppConfig.class);<br><br>        applicationContext.getBean(...); // to get the bean of particular class <br>                              //provide the class name as the parameter.<br><br>        ((AbstractApplicationContext)applicationContext).close();<br>    }<br>}</pre><p>Done application is ready and we can work with whatever beans we want and perform the business logic.</p><p><strong>Advantages of Java-based configuration:<br></strong>1. XML-based configuration can be avoided.<br>2. Improves readability.<br>3. Debugging becomes easy.<br>4. <em>Foundation for spring boot.</em></p><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9eba9a788a24" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Annotations in Spring]]></title>
            <link>https://medium.com/@hemasaijammana/annotations-in-spring-29b9e58605a2?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/29b9e58605a2</guid>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[annotations]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 04:15:00 GMT</pubDate>
            <atom:updated>2023-04-06T04:15:00.962Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@hemasaijammana/autowiring-scope-of-bean-ad0c0725a637">In the earlier articles</a>, we have seen the basics of the spring framework like spring container and what is dependency injection, what is Spring bean, and the different attributes of a spring bean.<br>Now it is time to move to the next way of the configuration of metadata to the spring container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>As we already know there are 4 ways to configure the metadata (bean information) to the spring container.<br>1. XML file based <br>2. Annotations based<br>3. Pure Java code base<br>4. Spring Boot Auto Driven</p><p>Now we will see the Annotation based configuration and a few of the annotations. We may be using the annotations but how can the spring container come to know and deal with the annotations for that we need to inform the spring container through XML file adding <strong><em>&lt;context:annotation-config/&gt;</em></strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;<br>    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br>    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br>        http://www.springframework.org/schema/beans/spring-beans.xsd<br>        http://www.springframework.org/schema/context<br>        http://www.springframework.org/schema/context/spring-context.xsd&quot;&gt;<br><br>    &lt;context:annotation-config/&gt;<br><br>&lt;/beans&gt;</pre><p><strong><em>&lt;context:annotation-config/&gt; </em></strong>This will check for the required beans which are defined.</p><p><strong>@Required:<br></strong>When we are working on constructor injection if any parameter is missing we will get an exception but for setter injection, we will not get any exception.<br>To provide this functionality we can use the @Required annotation on setters.</p><p><strong>@Autowired:<br></strong>As we have already seen what is Auto wiring earlier, we know we can do it in three ways bytype, byName, and constructor.<br>This Autowired also does the same byType, byName, and constructor mode of auto wiring.<br>By Default auto wiring on byType.</p><p><strong>AutoWire using ByName:<br></strong>If more than one bean of the same type is found then we will get an exception. There are 3 classes that are implementing courier class.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;<br>       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br>       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br>        https://www.springframework.org/schema/beans/spring-beans.xsd<br>        http://www.springframework.org/schema/context<br>        https://www.springframework.org/schema/context/spring-context.xsd&quot;&gt;<br><br> &lt;context:annotation-config/&gt;<br><br>&lt;!-- Dependent bean --&gt;<br>&lt;bean id=&quot;amazon&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>&lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot;/&gt;<br>&lt;bean id=&quot;bluedart&quot; class=&quot;src.courier.BlueDart&quot;/&gt;<br><br>&lt;!-- target bean --&gt;<br>&lt;bean id=&quot;flip&quot; class=&quot;src.services.Flipkart&quot;/&gt;</pre><pre>public class Flipkart {<br><br>    @Autowired<br>    private Courier courier;<br><br>..................,,,<br>}</pre><p>In the above code as we have 3 beans of courier type so we will get an exception. We can rectify the above using primary in XML file or we can use @Qualifer on top of the courier instance.</p><pre>    &lt;bean id=&quot;amazon&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>    &lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot;/&gt;<br>    &lt;bean id=&quot;bluedart&quot; class=&quot;src.courier.BlueDart&quot; primary=&quot;true&quot;/&gt;</pre><pre>public class Flipkart {<br><br>    @Autowired<br>    @Qualifier(&quot;bluedart&quot;)<br>    private Courier courier;<br><br>  ..............,,,<br>}</pre><p>If we use both the @Qualifier and primary = “true”, Qualifier if given high preference.</p><p><strong>AutoWire using ByName:</strong></p><pre>&lt;!-- Dependent bean --&gt;<br>&lt;bean id=&quot;amazon&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>&lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot;/&gt;<br>&lt;bean id=&quot;courier&quot; class=&quot;src.courier.BlueDart&quot;/&gt;<br><br>&lt;!-- target bean --&gt;<br>&lt;bean id=&quot;flip&quot; class=&quot;src.services.Flipkart&quot;/&gt;</pre><pre>public class Flipkart {<br><br>    @Autowired<br>    private Courier courier;<br><br>..................,,,<br>}</pre><p>As we are having only one bean with the courier as name auto wiring will happen using By Name.</p><p><strong>AutoWire using Constructor:<br></strong>We will be using <em>Autowired</em> annotation at the constructor level and if multiple beans we will use the <em>Qualifier</em></p><pre>public class Flipkart {<br>    private Courier courier;<br>    <br>        @Autowired<br>    public Flipkart(@Qualifier(&quot;bluedart&quot;) Courier courier) {<br>        this.courier = courier;<br>    }<br>}</pre><p><strong>Setter Injection using Autowired:</strong></p><pre>public class Flipkart {<br><br>    private Courier courier;<br>    <br>    @Autowired<br>    public void setCourier(@Qualifier(&quot;bluedart&quot;) Courier courier) {<br>        this.courier = courier;<br>    }<br>}</pre><p><strong>@PostConstruct:<br></strong>We can write our own custom init methods and annotate with PostConstruct then the spring container will execute that first and then it process to the business logic.</p><p><strong>@PreDestroy:<br></strong>We can also write the same destroy method to clear all the data before the container leaves the particular class and annotate with the PreDestroy.</p><h3>Stereotype Annotations:</h3><p>In Spring Framework, stereotype annotations are used to indicate the roles of specific classes in the application. Stereotype annotations are a type of metadata that provide hints to Spring about how to handle certain classes, and they can be used to simplify the configuration and management of Spring components.<br>Spring can automatically detect stereotyped classes and register bean definitions with the ApplicationContext.<br>To auto-detect these classes and register the beans we need to provide the below XML file in which we provide a base package to scan for these stereotyped classes. <strong><em>&lt;context:component-scan base-package=”org.example”/&gt;</em></strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;<br>    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>    xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br>    xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br>        http://www.springframework.org/schema/beans/spring-beans.xsd<br>        http://www.springframework.org/schema/context<br>        http://www.springframework.org/schema/context/spring-context.xsd&quot;&gt;<br><br>    &lt;context:component-scan base-package=&quot;org.example&quot;/&gt;<br><br>&lt;/beans&gt;</pre><p>We have the following annotations in spring:<br>1. Component<br>2. Controller<br>3. Service<br>4. Repository</p><ol><li><strong>Component:<br></strong>This annotation is used to mark a class as a Spring component, which means it will be scanned and registered with the Spring container as a bean. This annotation can be used with any class, but it is typically used with domain objects, utility classes, and other non-specific classes.</li><li><strong>Controller:<br>Component</strong> + <br>This annotation is used to mark a class as a Spring MVC controller, which means it will be scanned and registered with the Spring container as a bean. This annotation is typically used with classes that handle HTTP requests and responses.</li><li><strong>Service:<br>Component +<br></strong>This annotation is used to mark a class as a Spring service, which means it will be scanned and registered with the Spring container as a bean. This annotation is typically used with classes that perform business logic and data access operations.</li><li><strong>Repository:<br>Component + <br></strong>This annotation is used to mark a class as a Spring repository, which means it will be scanned and registered with the Spring container as a bean. This annotation is typically used with classes that perform data access operations, such as querying a database.</li></ol><p>Advantages:<br>- code readability and maintainability will be good.<br>- save a lot of time adding all the configuration.<br>- we can mention the clear role of a particular class.</p><p>We will be seeing more annotations in the coming posts.</p><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29b9e58605a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Autowiring & Scope of Bean]]></title>
            <link>https://medium.com/@hemasaijammana/autowiring-scope-of-bean-ad0c0725a637?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad0c0725a637</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring-framework]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Tue, 04 Apr 2023 04:54:51 GMT</pubDate>
            <atom:updated>2023-04-04T04:54:51.068Z</atom:updated>
            <content:encoded><![CDATA[<p>We have seen the Spring bean and its metadata <a href="https://medium.com/@hemasaijammana/what-is-spring-bean-764091396618">in the earlier article</a>. In this article, we will see more about two of them Auto wiring and scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>Injecting dependent beans into the target beans is called Dependency Injection/ Auto wiring.<br>Spring can auto resolve the dependencies using the configuration provided to the spring container.<br>Spring will reduce the need for constructor arguments when we use auto wiring in the configuration using XML based.<br>Auto Wiring can be done in 2 ways:<br><strong>1. Explicit Auto wiring / Manual Injection</strong><br>a. &lt;property name = “ “ ref = “ “ /&gt;<br>b. &lt;constructor-arg name = “ “ ref = “ “ /&gt;<br>We have already seen this type of injection setter and constructor injection in the Dependency Injection part.</p><p><strong>2. Auto wiring / Auto Injection</strong><br>In this type auto wiring can be done in 3 ways:<br>1. byName<br>2. byType<br>3. constructor</p><p>Limitations:<br>1. This is only possible in Object type/ reference type beans<br>2. There is a possibility of ambiguity.<br>3. Readability of the spring bean configuration file will need improvement.</p><p><strong>1.byName:</strong><br>This type of auto wiring follows setter injection.<br>The container finds a dependent spring bean class/ object based on the id that is matching with the target class.<br>There is no possibility of an ambiguity problem because the bean ids in the IOC container are unique.</p><pre>    &lt;bean id=&quot;courier&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>&lt;!--    courier is the type of the Object what we used in the Flipkart object--&gt;<br>    &lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot;/&gt;<br>    &lt;bean id=&quot;bluedart&quot; class=&quot;src.courier.BlueDart&quot;/&gt;<br><br>    &lt;bean id=&quot;flip&quot; class=&quot;src.services.Flipkart&quot; autowire=&quot;byName&quot;/&gt;</pre><p>In the above XML file, we have 3 beans that are implementing courier type interface. so if it is by Name container will look for the courier type of object with the same name(courier) while injecting that in the Flipkart service.</p><p><strong>2. byType<br></strong>This type of auto wiring also follows setter injection.<br>There will be a possibility of ambiguity problem because in the container there can be different beans of the same type.<br>We can resolve the ambiguity problem using <em>“primary = true”.</em></p><pre>    &lt;bean id=&quot;amazon&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>    &lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot; primary=&quot;true&quot;/&gt;<br>    &lt;bean id=&quot;bluedart&quot; class=&quot;src.courier.BlueDart&quot;/&gt;<br><br>    &lt;bean id=&quot;flip&quot; class=&quot;src.services.Flipkart&quot; autowire=&quot;byType&quot;/&gt;</pre><p>In the above configuration file if we have auto wire using byType which looks for the type of courier object we have 3 classes that are implementing the courier interface and we have 3 beans where we get an ambiguity problem. we get rid of this using “primary = true” for one of the beans.</p><p><strong>3. constructor<br></strong>This type of auto wiring follows constructor injection.<br>There will be no possibility of ambiguity as we have only one bean with a name.<br>In this type, the constructor param name should match the dependent class bean id for the auto wiring to happen.</p><pre>    &lt;bean id=&quot;amazon&quot; class=&quot;src.courier.Amazon&quot;/&gt;<br>    &lt;bean id=&quot;dtdc&quot; class=&quot;src.courier.Dtdc&quot;/&gt;<br>    &lt;bean id=&quot;courier&quot; class=&quot;src.courier.BlueDart&quot;/&gt;<br>&lt;!--    courier is the constructor name what we used in the Flipakrt object creation--&gt;<br><br>    &lt;bean id=&quot;flip&quot; class=&quot;src.services.Flipkart&quot; autowire=&quot;constructor&quot;/&gt;</pre><p>In the above configuration file, we have 3 beans of courier type and auto wire is using the constructor. we make one bean of courier type so that when we are using the constructor injection it picks the courier bean.</p><p><strong>Scope attribute in Spring bean:<br></strong>We can define the bean scope when the container creates the bean. This is powerful and flexible because we can choose the scope of objects we create instead of having the scope of the object at the java class level.<br>Spring framework supports 5 types of scopes<br>1. singleton(default)<br>2. prototype<br>3. request<br>4. session<br>5. global session</p><p>out of which 3 are available only if we use web-aware Application Context.<br>We will be seeing the other two types here:</p><p><strong>1. Singleton:<br></strong>This is the default scope for any bean in the spring.<br>IOC container will never make the spring class a singleton java class, but it creates only one object stores this object in the internal cache and returns the object whenever the bean is used.</p><pre>    &lt;!--    Configuring dependent bean--&gt;<br>    &lt;bean id=&quot;student&quot; class=&quot;src.model.Student&quot; scope=&quot;singleton&quot;/&gt;<br><br>    &lt;!--    configuration target bean--&gt;<br>    &lt;bean id=&quot;university&quot; class=&quot;src.University&quot;&gt;<br>        &lt;constructor-arg ref=&quot;student&quot;/&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*NKOh5nAOMQJLuJiHjBNh4A.png" /><figcaption>Singleton scope</figcaption></figure><p><strong>2. Prototype:<br></strong>IOC container will create a new object for the spring bean class for every getBean().<br>The container does not keep the scope of spring bean in the cache.</p><pre>    &lt;!--    Configuring dependent bean--&gt;<br>    &lt;bean id=&quot;student&quot; class=&quot;src.model.Student&quot; scope=&quot;prototype&quot;/&gt;<br><br>    &lt;!--    configuration target bean--&gt;<br>    &lt;bean id=&quot;university&quot; class=&quot;src.University&quot;&gt;<br>        &lt;constructor-arg ref=&quot;student&quot;/&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*YkbjLExGdPnh7fakdBzfIg.png" /><figcaption>Prototype scope</figcaption></figure><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad0c0725a637" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Spring Bean?]]></title>
            <link>https://medium.com/@hemasaijammana/what-is-spring-bean-764091396618?source=rss-c17e12798752------2</link>
            <guid isPermaLink="false">https://medium.com/p/764091396618</guid>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[hemasai jammana]]></dc:creator>
            <pubDate>Sat, 01 Apr 2023 10:46:37 GMT</pubDate>
            <atom:updated>2023-04-01T10:46:37.901Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@hemasaijammana/how-does-spring-container-perform-dependency-injection-98bc05401246">In the earlier article</a>, we have seen what spring dependency is and how spring container is performing spring dependency injection. Now in this article, we will see what is <strong>Spring Bean </strong>in Spring<strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*iJeyxS6_z6F8delqdTMI1A.png" /><figcaption>Spring</figcaption></figure><p>As we all know Spring framework/Spring Boot is the most popular web development framework. In spring all the objects are treated as Beans.</p><p>Bean is an object that is instantiated, assembled, and managed by the spring IOC container. A simple java object which is created, configured, and managed by the spring container is called a bean.<br>Spring can manage one or more beans. Beans are configured using metadata provided to the spring container.<br>There are 4 different ways of configuring metadata<br>1. XML-based approach.<br>2. Annotation-driven configuration.<br>3. Using java code configuration.<br>4. Spring boot AutoDriven configuration.<br>We have already seen this configuration in this post. <a href="https://medium.com/@hemasaijammana/spring-ioc-container-configuration-9143453b4f81">configuration article</a></p><p>I am taking an example of XML-based configuration as below:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;<br>       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans<br>        https://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt;<br><br>    &lt;!--    Configuring dependent bean--&gt;<br>    &lt;bean id=&quot;student&quot; class=&quot;src.model.Student&quot;/&gt;<br>    &lt;bean id=&quot;course&quot; class=&quot;src.model.Course&quot;/&gt;<br><br>    &lt;!--    configuration target bean--&gt;<br>    &lt;bean id=&quot;university&quot; class=&quot;src.University&quot;&gt;<br>        &lt;constructor-arg ref=&quot;student&quot;/&gt;<br>        &lt;constructor-arg ref=&quot;course&quot;/&gt;<br>    &lt;/bean&gt;<br><br>&lt;/beans&gt;</pre><p>Spring Bean will have the following metadata so that the configuration, instantiation, and management of beans are identified by spring container:<br>1. id/name<br>2. class<br>3. constructor-arguments<br>4. properties<br>5. scope<br>6. auto wiring<br>7. lazy-initialization mode<br>8. initialization method<br>9. destruction method</p><p><strong>1. Name:</strong><br>In Spring bean can be identified by some name so that it performs dependency injection or creation of objects. If you have not provided any id or name spring automatically creates one unique name to identify the bean.<br>If you want to provide a name you can give it with an id or name attribute in the bean tag. you can give multiple names also with alias attribute.</p><pre>&lt;bean id=&quot;student&quot; alias=&quot;studentObject&quot; /&gt;<br><br>or<br><br>&lt;bean name=&quot;student&quot; alias=&quot;studentObject&quot; /&gt;</pre><p><strong>2. Class:<br></strong>Spring while creating a bean it looks for the type of bean it should create for a particular bean Id, then it checks its configuration file for the type or class of object that it is to be created.</p><pre>&lt;!--    Configuring dependent bean--&gt;<br>    &lt;bean id=&quot;student&quot; class=&quot;src.model.Student&quot;/&gt;<br>    &lt;bean id=&quot;course&quot; class=&quot;src.model.Course&quot;/&gt;</pre><p>In the above XML file we are mentioning bean named with the student should be of <strong>src.mode.Student </strong>class.</p><p><strong>3. Constructor arguments</strong><br>We have seen this in <a href="https://medium.com/@hemasaijammana/dependency-injection-f2b68d72b58b">Dependency Injection</a> where we will use constructor injection to inject the dependencies into the target object.<br>In this we provide a tag <strong><em>&lt;constructor-arg ref=” “/&gt;</em></strong></p><pre>&lt;!--    configuration target bean--&gt;<br>    &lt;bean id=&quot;university&quot; class=&quot;src.University&quot;&gt;<br>        &lt;constructor-arg ref=&quot;student&quot;/&gt;<br>        &lt;constructor-arg ref=&quot;course&quot;/&gt;<br>    &lt;/bean&gt;</pre><p>In the above code, the University bean is dependent on the student bean and course bean where they are injected using constructor injection using the <em>&lt;constructor-arg ref=” “/&gt;</em> tag</p><p><strong>4. Properties:</strong><br>We have seen the same in the Dependency injection part where the dependent objects are injected into the target object using <strong><em>&lt;property /&gt;</em></strong><em> tag.</em></p><pre>    &lt;!--    configuration target bean--&gt;<br>    &lt;bean id=&quot;university&quot; class=&quot;src.University&quot;&gt;<br>        &lt;property name=&quot;student&quot; ref=&quot;studentBean&quot;/&gt;<br>        &lt;property name=&quot;course&quot; ref=&quot;courseBean&quot;/&gt;<br>    &lt;/bean&gt;</pre><p>In the above code, the university object is dependent on the student and course and we are using setter injection to inject the dependent beans.</p><p><strong>5. Scope:<br></strong>Spring will be able to inject a particular type of object to particular target objects based on the scope of objects created for a particular type. We can configure the bean scope using the configuration metadata instead of the default java class level scope.<br>Spring supports 5 different types of scopes:<br>1. Singleton (Default)<br>2. Prototype<br>3. Request<br>4. Session<br>5. Global Session</p><p><strong>6. Auto Wiring:<br></strong>Spring can auto resolve the dependencies using the configuration provided to the spring container. <br>Spring will reduce the need of constructor arguments when we use auto wiring in the configuration using XML based.<br>The auto wiring functionality has 4 modes:<br>1. No (Default)<br>2. byName<br>3. byType<br>4. constructor</p><blockquote>Note :- We will see Autowiring and Scopes in detail in next articles.</blockquote><p><strong>7. lazy-initialization mode<br></strong>There is a requirement where we don’t want all the beans to be instantiated but when we require that specific bean spring container should provide that. we can achieve this using <strong><em>lazy-init=”true”</em> </strong>attribute.<br>Lazy-initialized beans are beans in a Spring context that are not instantiated immediately when the context is created but are instead instantiated when they are first requested.</p><pre>&lt;!--    Configuring dependent bean--&gt;<br>    &lt;bean id=&quot;student&quot; class=&quot;src.model.Student&quot; lazy-init=&quot;true&quot;/&gt;<br>    &lt;bean id=&quot;course&quot; class=&quot;src.model.Course&quot;/&gt;</pre><p>In the above code, the student object will not be created at the startup but when we try to create an object which is dependent on this, Spring will create a bean for this student object.</p><p><strong>8. initialization method <br>9. destruction method<br></strong>we will see this in the upcoming articles when we see the lifecycle of a bean.</p><p>Please do follow for more articles and learn with me.<br>Special Thanks to <a href="https://www.linkedin.com/in/nitin-m-110169136/">Nitin M</a> who made these all concepts very easy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=764091396618" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>