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
Dependency Injection Di (implemented by configuration file or annotation , we focus on the configuration file, the XML file first)
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, two folders, respectively, are
(1) Folder Lib, will spring-framework-4.2.0.release\ These files under the Libs and the Commons-logging-1.2.jar under the commons-logging-1.2 are copied into our new lib.
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) Folder Springxml, put XML file
2.2 Build a package in the SRC directory:
Main.java.com.sommer.learn, release source code
Under the first package, new one interface Helloworld.java
Package Main.java.com.sommer.learn;public interface HelloWorld {public String sayhi ();}
New one implementation of the interface Helloworldimpl.java
Package Main.java.com.sommer.learn;public class Helloworldimpl implements helloworld{@Overridepublic String Sayhi () { Return "Hello World from Implement";}}
2.3 Create a new Helloworld.xml under the Springxml folder
<?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
Right-springxml folder--build Path--use as the 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
Package Main.java.com.sommer.learn;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Main {public static void Main (String [] args) {ApplicationContext APC = new Classpathxmlapplicationcontext ("Helloworld.xml"); HelloWorld Hello = Apc.getbean ("HelloWorld", Helloworld.class); System.out.println (Hello.sayhi ());}}
Output Hello world from Implement.
Prove that your first spring project is running successfully!
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 instead of creating the Helloworldimpl object ourselves, we leave it to the IOC container to create it. This is the implementation of dependency injection through the configuration file.
About AOP, follow up on updates ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Create your first spring project with Eclipse (the most entry-level)