Use Eclipse to create the first Spring Project (the most basic)
There are countless articles on Spring on the Internet, but most of them are lengthy and hard for beginners to understand and remember. Here we will give a brief extraction without a detailed explanation. The main content is to show you how to create a Spring Project and try it out.
1. What is Spring Framework used?
It is a development model provided for java-based enterprise applications, allowing developers to focus only on the business logic at the application layer, rather than environment deployment.
2. Spring Design Principles
Control reversal IoC: The right to create and manage objects is handed over to a third party (container, also known as IoC container)
Dependency injection DI: Create an object and assign values to attributes through configuration files or annotations. First, we only focus on the configuration file, that is, the xml file.DI is the way to implement control Inversion
Aspect-Oriented AOP
Okay. Let's get started!
1. Prepare Software
Eclipse java ee ide, download website http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr
JDK 1.6 or above. Here, my latest version is 1.8.
Spring Framework http://sourceforge.net/projects/springframework/, (the official website is downloaded through maven dependencies, if you have never learned maven, or download it from sourceforge here ). My name is spring-framework-4.2.0.release-dist.zip. Decompress the zip package and use the. jar file in libs.
Commons-logginghttp: // commons.apache.org/proper/commons-logging/download_logging.cgi click commons-logging-1.2-bin.zip to download and decompress
2. Create a project
2.1 File -- new -- java project
Enter project name: firstSpring
In the project directory, the next folder lib
Copy these files in the spring-framework-4.2.0.RELEASElibs and the commons-logging-1.2.jar in commons-logging-1.2 to the new lib.
Add build path, right-click the project -- properties -- java build path, and click -- add JARs under the libraries tab...
Add the jar package in firstSpringlib
OK -- apply -- OK
2.2 create in the src directory:
A package main.java.com. sommer. learn is provided with the source code.
The springXML folder contains xml files.
New Interface HelloWorld. java under the package
package main.java.com.sommer.learn;public interface HelloWorld {public String sayHi();}
Add another implementation of this 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 file in the springXML folder.
Tip: If your xml file is not in the src path, right-click -- build path -- use as source folder. Otherwise, the xml file cannot be found in the following class path.
2.4. Now, let's create a common class to see what the configuration has done.
Main.java.com. sommer. learn is a 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(springXML/HelloWorld.xml);HelloWorld hello = apc.getBean(helloWorld,HelloWorld.class);System.out.println(hello.sayHi());}}
Output Hello World from Implement.
It proves that your first spring Project has run successfully!
Project Structure
3. Summary
Here, we have not manually created the HelloWorldImpl instance (object), which is created by Spring through ApplicationContext and placed in the IoC container. ApplicationContext is an IoC container interface, and all the objects it creates are called beans, that is, This line of configuration information. The getBean method is to obtain this object from the IoC container (based on the id and class name), and then we can call the method of this class.
Our Main class needs to use the HelloWorldImpl class. That is to say, the Main class depends on the HelloWorldImpl class, but the Main does not need to create the HelloWorldImpl object, but instead is automatically created by the IoC container,This means dependency injection is implemented through the configuration file.