This is the 17th day of my participation in Gwen Challenge

Overview of Spring

Spring is an open source framework at the design level, which solves the problem of loose coupling between the business logic layer and other layers. Therefore, it puts the thought of interface oriented programming throughout the whole system application. Spring is a lightweight Java development framework created in 2003 by Rod Johnson. Simply put, Spring is a layered JavaSE/EE full-stack lightweight open source framework.

Second, Spring features

1, convenient decoupling, simplify development.

2. AOP programming support.

Support for declarative transactions.

4, convenient test procedures

5. Convenient integration of various excellent frameworks

Third, spring download

IO /libs-releas…

You can select a version to download. After selecting a version, you can enter the directory structure. Dist is the final release, containing the lib and source code required for development. Docs are development documents. Schemas are constraint files.

Four, Spring setup introductory case

1. Create a dynamic Web project in Eclipse.

2. Import the spring base lib package into the lib folder.

3. Write an entity User class

package com.jichi.entity; public class User { private String name; private Integer age ; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}Copy the code

4. Create applicationContext.xml file

Under the SRC file, create a new applicationContext.xml file and introduce the constraints.

<? The 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" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:task="http://www.springframework.org/schema/task" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:websocket="http://www.springframework.org/schema/websocket" 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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> </beans>Copy the code

5. Configure the user entity in beans

<bean name="user" class="com.jichi.entity.User"></bean>
Copy the code

6. Write test code

public class TestDemo { @Test public void test1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) ac.getBean("user"); System.out.println(user); }}Copy the code

7. The result shows that the User object has been successfully handed over to the Spring container for management

com.jichi.entity.User@db5e9c
Copy the code

Spring BeanFactory differs from ApplicationContext

1, the BeanFactory

Spring’s primitive interface, which is relatively simple, creates objects only when they are retrieved from the container.

2, ApplicationContext

Each time the container is started, it initializes all objects in the container and provides more functionality.

The implementation class ClassPathXmlApplicationContext is the spring configuration file loading classpath, FileSystemXmlApplicationContext is loaded under local disk spring’s configuration file.

3. Implementation diagram

4, the conclusion

In Web development, use applicationContext.

You can use the BeanFactory in resource-poor environments.

Spring bean element attributes

1. Class: the full class name of the container-managed object.

2. Name: Indicates the name of the object managed by the container. Obtain the object from the container based on this name.

3. Id: unique identifier of the container-managed object. The ID cannot be repeated and cannot contain special characters.

4, the scope:

(1) Singleton (default): singleton object, which will only exist in one Spring container.

Prototype: new object is created in the Spring container only when the object is fetched from the container, each time a new object is created. Note that when used with Struts2, struts2 action objects must be configured to the Prototype multiinstance mode.

(3) Request: In the Web project, create an object and store it in the Request field.

(4) Session: In the Web project, create an object and store it in the session domain.

5, init-method and destory-method

Init-method is the method executed when the object is created, and destory-method is the method executed when the object is destroyed. The object must be a singleton, and the destruction of the object is performed only when the container is closed.

<bean name="user" class="com.jichi.entity.User" init-method="init" destroy-method="destory"></bean>
Copy the code

public class User { private String name; private Integer age ; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public void init(){system.out.println (" initial "); } public void deStory (){system.out.println (" destroy "); }}Copy the code

How Spring creates objects

1. Empty parameter construction method

<bean name="user" class="com.jichi.entity.User" ></bean>
Copy the code

Static factory instantiation

Create a factory class

public class UserFactory { public static User createUser(){ return new User(); }}Copy the code

(2) Configuration

<bean name="user" class="com.jichi.factory.UserFactory" factory-method="createUser"></bean>
Copy the code

3. Instance factory instantiation

Create a factory class

public class UserFactory { public static User createUser(){ return new User(); }}Copy the code

(2) Configuration

<bean name="user" factory-bean="userFactory" factory-method="createUser"></bean>
Copy the code