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
- Package Main.java.com.sommer.learn;
- Public interface HelloWorld {
- Public String Sayhi ();
- }
New one implementation of the interface Helloworldimpl.java
[Java]View Plain Copy
- package main.java.com.sommer.learn;
-
- public class helloworldimpl implements helloworld{
-
- @Override
- public String sayhi () {
-   
- return
-
- }
-   
- }
2.3 Create a new Helloworld.xml under the Springxml folder
[HTML]View Plain Copy
- <? 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="Main.java.com.sommer.learn.HelloWorldImpl"> </Bean>
- </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
- 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 ("Springxml/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!
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)