Create your first spring project with Eclipse (the most entry-level)

Source: Internet
Author: User
Tags apc

http://blog.csdn.net/shymi1991/article/details/48085955

The introduction of spring on the internet has been numerous, but most of the lengthy, beginners difficult to understand the memory. Here is a short refinement, not a detailed explanation, the main content is to take you to create a spring project, to feel what this thing is like.

What does the 1.Spring framework do for you?

is a development model for Java-based enterprise applications that allows developers to focus only on the application-level business logic without caring about the deployment of the environment.

2.Spring Design Principles

Control reversal IOC: the right to create and manage objects to third parties (containers, also known as IOC containers)

Dependency Injection di: Through configuration files or annotations to achieve the creation of objects, property assignment, we first focus only on the configuration file, which is the XML file. di is the way to achieve control inversion

Facet-oriented AOP

OK, let's get started!

1. Preparing the Software

Eclipse java EE IDE, download website HTTP://WWW.ECLIPSE.ORG/DOWNLOADS/PACKAGES/ECLIPSE-IDE-JAVA-EE-DEVELOPERS/MARSR

JDK 1.6 above, here's my latest version 1.8

Spring Framework http://sourceforge.net/projects/springframework/, (official website is a dependent download via Maven, If you have not learned maven, you should download it from the SourceForge here. Mine is spring-framework-4.2.0.release-dist.zip. Unzip the zip package and the. jar file inside the Libs is what we're going to use.

commons-logginghttp://commons.apache.org/proper/commons-logging/download_logging.cgi Click Commons-logging-1.2-bin.zip Download Unzip

2. New Project

2.1 File--new--java Project

Enter Project Name:firstspring

In the project directory next folder Lib

Copy the files under the Spring-framework-4.2.0.release\libs and the Commons-logging-1.2.jar under the commons-logging-1.2 into our new lib.

Add build path, right click on Project--properties--java build path, under Libraries tab--add JARs ...

Add build path, right click on Project--properties--java build path, under Libraries tab--add JARs ...

Add the jar package inside the Firstspring\lib

Ok--apply--ok

2.2 Built in SRC directory:

A package Main.java.com.sommer.learn, release source code

Folder Springxml, putting XML files

Under package new one interface Helloworld.java

[Java]View Plain Copy
    1. Package Main.java.com.sommer.learn;
    2. Public interface HelloWorld {
    3. Public String Sayhi ();
    4. }

New one implementation of the interface Helloworldimpl.java

[Java]View Plain Copy
    1. package main.java.com.sommer.learn;   
    2.   
    3. public  class helloworldimpl implements helloworld{   
    4.   
    5.      @Override   
    6.     public  String sayhi ()  {  
    7.   
    8.          return 
    9.            
    10.     }  
    11.   
    12. }   

2.3 Create a new Helloworld.xml under the Springxml folder

[HTML]View Plain Copy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
  6. <Bean id= "helloWorld" class="Main.java.com.sommer.learn.HelloWorldImpl"> </Bean>
  7. </Beans>

Tip: If your XML file is not under the SRC path, you should right---build Path--use as source folder, otherwise the XML file will not be found under the following class path path

2.4 OK, now let's build a common class to see what the effect of the configuration was.

Main.java.com.sommer.learn under New class, named Main.java

[Java]View Plain Copy
  1. Package Main.java.com.sommer.learn;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. Public class Main {
  5. public static void Main (string[] args) {
  6. ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");
  7. HelloWorld Hello = Apc.getbean ("HelloWorld", HelloWorld.   Class);
  8. System.out.println (Hello.sayhi ());
  9. }
  10. }

Output Hello world from Implement.

Prove that your first spring project is running successfully!

Project structure

3. Summary

Here we do not manually create an instance of Helloworldimpl (object), which is placed in the IOC container by the ApplicationContext to help us create it. ApplicationContext is an IOC container interface that creates objects called beans, <bean id= "class=" ></bean> this line of configuration information in an XML file. The Getbean method is to take this object from the IOC container (based on the identity ID and Class name Class), and then we can invoke the method of the class.

Our main class needs to use the Helloworldimpl class, which means that the main class relies on the Helloworldimpl class, but main does not have to create Helloworldimpl objects, but instead gives us an IOC container to create them. This is the implementation of dependency injection through the configuration file.

About AOP, follow up on updates ~

Create your first spring project with Eclipse (the most entry-level)

Related Article

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.