Write the first program using the Java Spring Framework Hellow World_java

Source: Internet
Author: User
Tags aop getmessage unique id java spring framework

What is the Spring framework?
Spring is the most popular application development framework for Enterprise Java. Millions of developers around the world use the Spring framework to create high-performance, easily testable, reusable code.

The spring framework is an open source Java platform that was originally written by Rod Johnson and first released under Apache2.0 license in June 2003.

Spring is lightweight when it comes to size and transparency. The basic version of the spring framework is approximately 2MB.

The core features of the spring framework can be used in any Java application, but there is also a Web application built on the extended Java EE platform. The goal of the spring framework to make Java-EE development easier to use, and to promote good programming habits by making a pojo based programming model.

Benefits of using the Spring framework:
Here are a list of the great benefits of using the Spring framework:

Spring enables developers to use Pojo to develop enterprise-class applications. The benefit of using only Pojo is that EJB container products, such as application servers, are not required, but only a powerful servlet container, such as Tomcat or some commercial product selection, must be used.

Spring is organized in a modular way. Although the number of packages and classes is huge, you only have to worry about what you need, and the rest is ignored.

Spring does not reinvent the wheel, but it really leverages existing technologies like some ORM frameworks, log frames, Jee,quartz and JDK timers, and other view technologies.

Writing the application to test spring is simple because the environment-related code is moved into the framework. In addition, by using JavaBean-style pojo, it becomes easier to use dependency injection for test data.

The Spring web framework is a well-designed web MVC framework that provides a good alternative to web frameworks, such as struts or other overly-designed or less popular web frameworks.

Spring provides a convenient API translation technique for specific exceptions (via Jdbc,hibernate, or jdo thrown, for example) into a consistent, unchecked exception.

Lightweight IOC containers are often lightweight, especially relative to EJB containers, such as. This facilitates the development and deployment of applications on computers with limited memory and CPU resources.

Spring provides a consistent transaction management interface that can be scaled down to (using a single database, for example) local transactions and extended to global transactions (using JTA, for example).

Dependency Injection (DI):
Spring's most determined technology is dependency Injection control inversion (DI). Control reversal (IoC) is a general concept that can be manifested in many different ways and dependency injection is only a concrete example of control reversal.

When writing a complex Java application, application classes should be as independent of other Java classes as possible to increase the reuse of these classes and to test them independently of other classes, and thus the likelihood of unit testing. Dependency injection helps to glue these classes together while maintaining their independence.

What is dependency injection? Let's take a look at these two words separately. The dependency section here translates into an association between two classes. For example, Class A is dependent on class B, now, let's take a look at the second part, inject. All this means that Class B will be injected by the IOC into category A.

Dependency injection can pass parameters to the constructor or use the post construction setter method to occur. Dependency injection is the core of the spring framework, so it is explained in a separate chapter, a good example.

Aspect-oriented programming (AOP):
A key component of spring is the facet-oriented programming (AOP) framework. The functionality of an application that spans multiple points is known as crosscutting concerns and these crosscutting concerns are distinguished from the business logic concepts of the application. There are a variety of common good examples including logging, declarative transactions, security, and caching.

Modular OOP key units are classes, while the modular units in AOP are slices. DI helps you detach from each other's application objects, AOP can help you eliminate crosscutting concerns from the objects they affect.

The Spring Framework AOP module provides an aspect-oriented programming implementation that allows you to define method interceptors and pointcuts to clean decoupling and implement functionality that should separate the code. I'll discuss the concept of spring AOP in a separate chapter.


Hello World
Let's use the spring framework from actual programming. To begin writing the first example of using the spring framework, you must make sure that you have set up the spring environment. Need a little knowledge of the Eclipse IDE.

So let's start by writing a simple spring application that will print "Hello world! Or additional information that is based on the spring bean configuration file to complete the configuration.

Step 1th-Create a Java project:
The first step is to use the Eclipse IDE to create a simple Java project. Option File-> New-> Project finally selects Java Project from the Wizard List Wizard. The project is now named Hellospring Using the wizard window, as follows:

After the project is successfully created, you will see the following in Project Explorer:

Step 2nd-Add the required libraries:
The second step is to add the spring framework and the common Log API library in our project. To do this, right-click your project name hellospring and then follow the context menu with the following options: Build path-> Configure the Java Building Path window shown below:

Now use the Add External JARs option Libraries card to provide an external jar button to add the core jar files from the spring framework and the Common Log installation directory:

    • antlr-runtime-3.0.1
    • Org.springframework.aop-3.1.0.m2
    • Org.springframework.asm-3.1.0.m2
    • Org.springframework.aspects-3.1.0.m2
    • Org.springframework.beans-3.1.0.m2
    • Org.springframework.context.support-3.1.0.m2
    • Org.springframework.context-3.1.0.m2
    • Org.springframework.core-3.1.0.m2
    • Org.springframework.expression-3.1.0.m2
    • commons-logging-1.1.1

Step 3rd-Create a source file:
Now let's hellospring the project based on the source file actually created. First, we need to create a name for the package Com.yiibai. To do this, right click SRC in the Package Resource Management Section and follow the options: New-> Package.

Next we create Helloworld.java and Mainapp.java files under Com.yiibai package.

Here are the contents of the Helloworld.java file:

Package Com.yiibai;

public class HelloWorld {
  private String message;

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

  public void GetMessage () {
   System.out.println (' Your message: ' + message);
  }
}

The following is the contents of the second file Mainapp.java:

Package Com.yiibai;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mainapp {public
  static void Main (string[] args) {
   ApplicationContext context = 
       new ClassPath Xmlapplicationcontext ("Beans.xml");

   HelloWorld obj = (HelloWorld) context.getbean ("HelloWorld");

   Obj.getmessage ();
  }


The following two key points need to be noted in the main program:

The first step is to create where we use the Framework API Classpathxmlapplicationcontext () application context. This API loads the beans configuration file and, ultimately, based on the provided API, it needs to create and initialize all of the objects. The beans class mentioned in the configuration file.

The second step is to use the Getbean () method of the created context to get the desired bean. This method returns with the Bean's ID and can eventually create a generic object for the actual object. Once you have an object, you can use this object to invoke any class method.

Step 4th-Create the Bean configuration file:
You need to create a bean configuration file, which is an XML file and is grouped together as classes such as cement. This file needs to be created in the SRC directory as shown in the following illustration:

Normally the developer retains the name of this file as Beans.xml file, but must be independent and choose any name you like. You must make sure that this file is available in CLASSPATH and use the same name in the application context created by the main application, such as Figure Mainapp.java file.

Used in Beans.xml to assign unique IDs, creating different value objects with different beans and controls, without affecting any Spring source files. For example, using the following file, you can pass any value for the "message" variable so that you can print different values of the messages without affecting the Helloworld.java and Mainapp.java files. Let's take a look at how it works:

<?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= "HelloWorld" class= "Com.yiibai.HelloWorld" > <property name= "message
    " value= "Hello world!" />
  </bean>

</beans>

When the spring application is loaded into memory, the framework uses the above configuration file to create all the defined beans and assigns a unique ID to them in the <bean> tag definition. You can use the <property> tag to pass values that use different variables when creating an object.

Step 5th-Run the program:
Create the source code and bean configuration file to complete, ready to compile and run the program. To do this, keep the Mainapp.java file tab active and use the Run option provided in the Eclipse IDE, or use CTRL + F11 to compile and run the application Mainapp. If everything goes well then the application will print the following information in the Eclipse IDE console:

Your Message:hello world!
Congratulations, you have created the first spring application and executed it successfully. You can change the value of the "message" property and keep the source files of both unchanged from the flexibility of the Spring application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.