简体   繁体   中英

Why should I use Spring core framework over java application

I am new to Spring framework .I have made a simple "HelloSpring" application using Spring framework .

POJO CLASS :-
public class HelloSpring {

private String message;

public void setMessage(String message) {
    this.message = message;
}

public String getMessage() {
    return message;
}
}

Beans.xml :

   <?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloSpring"    class="com.dash.abinash.SpringHelloApp.HelloSpring">
       <property name="message" value="Hello World!" />
     </bean>

Client.java :-

import org.springframework.context.ApplicationContext;
import     org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientApp {
    public static void main(String[] args) {
         @SuppressWarnings("resource")
         ApplicationContext context = new     ClassPathXmlApplicationContext("Beans.xml");
         HelloSpring obj = (HelloSpring) context.getBean("helloSpring");
         System.out.println(obj.getMessage());
      }
        }

output :-

  Sep 27, 2017 12:02:58 PM     org.springframework.context.support.ClassPathXmlApplicationContext    prepareRefresh
  INFO: Refreshing    org.springframework.context.support.ClassPathXmlApplicationContext@4534b60d:     startup date [Wed Sep 27 12:02:58 IST 2017]; root of context hierarchy
 Sep 27, 2017 12:02:58 PM     org.springframework.beans.factory.xml.XmlBeanDefinitionReader    loadBeanDefinitions
 INFO: Loading XML bean definitions from class path resource [Beans.xml]
 Hello World!

But same output , I will get without Spring framework also by using abstraction and encapsulation concept (getter and setter) method .

HelloSpring ref=new HelloSpring();
    ref.setMessage("Hello World!");
    System.out.println("Output without Spring framework "+ref.getMessage());

Then , why should we use Spring framework . May be it is a silly question but try to demonstrate me in a detailed manner if possible .

Loose coupling is one of the major advantage, Explore more you will love it.

In you example:

This is a recipe of creating object:

 <bean id="helloSpring"    class="com.dash.abinash.SpringHelloApp.HelloSpring">
       <property name="message" value="Hello World!" />
     </bean>

And Spring will take efforts of cooking and creating beans for you.

Spring is smart it can reuse this object at number of places.

Spring can create them with various scopes

and many more advantages , I would refrain myself from writing a blog here

It's like this: When you do:

HelloSpring ref=new HelloSpring();
ref.setMessage("Hello World!");
System.out.println("Output without Spring framework "+ref.getMessage());

you are creating every-time an instance of the class.

But in an enterprise application( web application) you need to keep in mind that this kind of approach is bad for server. So here comes the Spring part for example, you might use @service and HelloSpring is declared once (like a service) and every time you want you can use HelloSpring without having to make another place in server memory for it.
Like:

@Service
public class HelloSpring{
...
...
}

As @Yuval mentions it on comment, there are many tools that Spring offers. You just need to go through docs and find what is good for your applications benefit.

Best

In your example its 1 class for printing some message. so you have created object using new operator. but in enterprise applications the business logic will be implemented across multiple java class. in this case if you create new java instances everytime the memory will run out and servers go down. to tacle this problem we refer to Dependency Injection design pattern. Spring framework is built on this pattern and to tackle the above mentioned real time problems. if you are intrested please go through some tutorials there are even more usfull features in spring to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM