Spring framework --- Spring entry, spring framework --- spring
Spring entry
For better understanding, let's talk about some things you don't have:
What is Spring?
Spring is a hierarchical JavaSE/EE full-stack (one-stop) Lightweight open source framework.
Layered
Three-tier structure of the EE provided by SUN: web layer, business layer, and data access layer (persistence layer/integration layer)
Struts2 is a web layer based MVC design pattern framework.
Hibernate is An ORM framework of the persistence layer.
All-in-One
The Spring framework has three layers of solutions
Web layer: Spring MVC
Persistent layer: JDBC Template
Business Layer: Spring Bean Management
Spring core:
Control reversalThat is to say, when we call a method or class, we no longer take the initiative to create the Class Object and give control to others (spring ).
Dependency InjectionThat is to say, spring actively creates the object of the called class and injects the object into our own class so that we can use it.
Spring advantages
Convenient decoupling and simplified development
Spring is a large factory that allows you to create all objects and maintain dependency between them.
Support for AOP Programming
Spring provides Aspect-Oriented Programming, allowing you to easily intercept permissions and monitor programs.
Support for declarative transactions
You only need to configure the transaction management, without manual programming.
Convenient program testing
Spring supports Junit4 and can easily test Spring programs through annotations
Easy integration of various excellent frameworks
Spring does not exclude all kinds of excellent open-source frameworks. It provides direct support for various excellent frameworks (such as Struts, Hibernate, MyBatis, and Quartz ).
Reduces the difficulty of using Java EE APIs
Spring provides encapsulation For APIs (JDBC, JavaMail, and remote calls) that are difficult to use during Java EE development, greatly reducing the difficulty of these Apis.
All the above is nonsense. The following is the most basic:
Spring Entry Program
(1)Import package
Download Spring Development Kit
DownloadLatest jar packageAddress:Http://repo.spring.io/simple/libs-release-local/org/springframework/spring/
Spring-framework-4.3.7.RELEASE-dist.zip --- Spring Development Kit
Note:
Spring-beans-4.3.7.RELEASE.jar // these four files can be found in the spring development kit by searching
Spring-context-4.3.7.RELEASE.jar
Spring-core-4.3.7.RELEASE.jar
Spring-expression-4.3.7.RELEASE.jar
Developed log recording package: // download it under apache
Com.springsource.org. apache. commons. logging-1.1.1.jar --- packages used to integrate other logs (similar to slf4j in Hibernate)
Com.springsource.org. apache. log4j-1.2.15.jar
(2) create a Spring configuration file
Create an applicationContext. xml file under src
Introduce XML constraints: Find xsd-config.html in spring development kit. Introduce beans Constraints
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
(3) create an interface: HelloServcie
public interface HelloServcie { public void sayHello();}
(4) create an interface implementation class:
1 public class HelloServiceImpl implements HelloServcie {2 3 public void sayHello() {4 System.out.println("hello spring");5 }6 }
(5) Configure applicationContext. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: util = "http://www.springframework.org/schema/util" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <! -- Bean definitions here --> <! -- 1. quick Start Program ================================================= ========================================================== = --> <! -- Configure the path of the class to be instantiated to implement the class id Identifier --> <bean id = "helloServices" class = "com. study. spring. a_quickstart.HelloServiceImpl "> </bean> <! -- End. quick Start Program ================================================= ========================================================== = --> </bean
(6) configuration test
Public class HelloTest {public static void main (String [] args) {// the traditional method is tightly coupled with HelloServiceImpl servcie = new HelloServiceImpl (); // you can call servcie manually. sayHello (); // factory + reflection + configuration file ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("applicationContext. xml "); // load the Class Object HelloServcie helloServcie = (HelloServcie) applicationContext through the information in the factory and reflection level-1 configuration file. getBean ("helloServices"); helloServcie. sayHello () ;}/ * output twice in the background: hello spring */
This example is very simple. It mainly describes from this example that the First Advantage of spring is decoupling, which is more conducive to later maintenance.
This article is very easy to get started with. I hope you can give me more instructions. Thank you!