简体   繁体   English

为什么我应该在Java应用程序上使用Spring核心框架

[英]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 . 我是Spring框架的新手。我使用Spring框架制作了一个简单的“ HelloSpring”应用程序。

POJO CLASS :-
public class HelloSpring {

private String message;

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

public String getMessage() {
    return message;
}
}

Beans.xml : 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 :- 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 . 但是同样的输出,我也将通过使用抽象和封装概念(getter和setter)方法来获得Spring框架。

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

Then , why should we use Spring framework . 然后,为什么要使用Spring框架。 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会努力为您烹饪和制作豆类。

Spring is smart it can reuse this object at number of places. Spring很聪明,它可以在许多地方重用此对象。

Spring can create them with various scopes Spring可以在各种范围内创建它们

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. 但是在企业应用程序(Web应用程序)中,您需要记住,这种方法对服务器不利。 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. 因此,例如,这里有Spring部分,您可以使用@service,并且一次声明HelloSpring(就像服务一样),并且每次您希望使用HelloSpring时都不必在服务器内存中放置其他位置。
Like: 喜欢:

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

As @Yuval mentions it on comment, there are many tools that Spring offers. 正如@Yuval在评论中提到的那样,Spring提供了许多工具。 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. 在您的示例中,它的1类用于打印某些消息。 so you have created object using new operator. 因此您已使用new运算符创建了对象。 but in enterprise applications the business logic will be implemented across multiple java class. 但是在企业应用程序中,业务逻辑将在多个java类中实现。 in this case if you create new java instances everytime the memory will run out and servers go down. 在这种情况下,如果您每次都创建新的Java实例,则内存将用完并且服务器将关闭。 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. Spring框架是基于这种模式构建的,旨在解决上述实时问题。 if you are intrested please go through some tutorials there are even more usfull features in spring to use. 如果您感兴趣,请阅读一些教程,在春季还可以使用更多有用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我要在java中使用null(String [])null? - Why should I use (String[])null over null in java? 为什么我应该使用 Deque 而不是 Stack? - Why should I use Deque over Stack? 为什么我要避免代码中的对象实例化并使用Spring框架? - Why should I want to avoid object instantiation in code and use the Spring framework? 在测试中使用 Java + Spring + Cucumber 框架比核心 Java + Cucumber 框架有什么优势? - What are the advantages of using a Java + Spring + Cucumber framework over a core Java + Cucumber framework in testing? 我为什么要使用Spring Android? - Why should I use Spring Android? 为什么我应该使用 Deque over Stack 和 LinkedList over Queue? - Why should I use Deque over Stack and LinkedList over Queue? 创建Java电子邮件批处理程序时,我应该使用Spring Framework jar文件还是Spring Batch jar文件? - Should I use the Spring Framework jar files or Spring Batch jar files while creating a java email batch program? 我应该使用哪种设计模式? 使用Spring框架 - What design pattern should I use ? using spring framework 我应该使用框架吗? - Should I use a framework? 为什么要在Java中使用异常处理? - Why should I use Exception handlings in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM