Spring is an open source framework created by Rod Johnson. It was created to address the complexity of enterprise application development.

Spring uses basic Javabeans to do things that were previously only possible with EJBs. However, Spring’s use is not limited to server-side development. Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling. Purpose: To address the complexity of enterprise application development features: Use basic Javabeans instead of EJBs, and provide a wider range of enterprise application capabilities: Any Java application is a container framework for holding Javabeans (Java objects), and a mid-tier framework (universal glue) can serve as a link, such as gluing Struts and Hibernate together. In simple terms, Spring is a lightweight Inversion of Control (IoC) and AOP oriented container framework.

Inversion of Control, in plain English, means that the relationships between programs are controlled by the container, rather than directly by the program code in traditional implementations. This is where the concept of “inversion of control” comes in: control is transferred from application code to an external container. Another name for IoC is “DI=Dependency Injection”, which is the dynamic Injection of a Dependency into a component by a container. Example: Implementing Spring’s IoC

Step 1: Need to add springIDE plug-in, configure related dependencies (how to install plug-in click open link) POm. XML (1. Spring-context 2. Spring-orm 3.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zking</groupId> <artifactId>s1</artifactId> <packaging> <version>0.0.1 -snapshot </version> <name>s1 Maven Webapp</name> <url>http://maven.apache.org</ URL > <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> < version > 5.0.1. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < artifactId > spring - web < / artifactId > < version > 5.0.1. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the orm < / artifactId > < version > 5.0.1. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.0.1.RELEASE</version> </dependency> </dependencies> <build> <finalName>s1</finalName> </build> </project>Copy the code

Step 2: Plug in the Spring XML File (right-click >new >other >Spring >Spring Bean Configuration File)

Note: When creating spring XML files, you need to add beans/ AOP/TX/Context tag support (check box) 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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
    
</beans>
Copy the code

Step 3: Create a HelloWorld class

package p1;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class HelloWorld {
    private String name;
 
    public HelloWorld() {
        super();
        System.out.println("new HelloWorld()");
    }
    
    public HelloWorld(String name) {
        super();
        this.name = name;
    }
 
    public void init() {
        System.out.println("init.......");
    }
    public String getName() {
        return name;
    }
 
    public void setName(String name) { this.name = name; }}Copy the code

JavaBean = JavaBean; JavaBean = JavaBean; JavaBean = JavaBean; JavaBean = JavaBean; The full class name of the bean (object) Look up the Bean in the container (objects) name (only, starting with /, to allow multiple values, separated by a comma or space between more than one value) 4 scope: (singleton | prototype) default is singleton 4.1 singleton (singleton) : In each Spring IoC container, one bean definition corresponds to one object instance. To define a bean as an abstract bean(abstract beans cannot be instantiated), an abstract class must be defined as an abstract bean, and a non-abstract class can also be defined as an abstract bean. Specify the initialization method for the bean object (). 7 Create javabeans using the parameterized constructor: constructor-arg

Step 4: Create beans in XML (for those who don’t understand attributes, see point 3)

<bean id="helloworld" class="p1.HelloWorld" scope="prototype" name="a b c" init-method="init">
        <property name="name">
            <value>zs</value>
        </property>
</bean>
    
    <bean id="helloworld2" class="p1.HelloWorld">
        <constructor-arg index="0">
            <value>zzz</value>
        </constructor-arg>
    </bean>
Copy the code

Step 5: Write a test class

Public static void main(String[] args) {HelloWorld HelloWorld =new HelloWorld(); helloWorld.setName("Zhang");
        System.out.println("hello"+helloWorld.getName());
        //-------------------------------------------------------------
        //Spring
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        HelloWorld a = (HelloWorld)applicationContext.getBean("a");
        System.out.println("Hello."+a.getName());
        
        HelloWorld b = (HelloWorld)applicationContext.getBean("b");
        System.out.println("Hello."+b.getName());
        
        HelloWorld c = (HelloWorld)applicationContext.getBean("c");
        System.out.println("Hello."+c.getName());
        
        HelloWorld d = (HelloWorld)applicationContext.getBean("helloworld2");
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        System.out.println("Hello."+d.getName());
    }
Copy the code

4. Simple attribute configuration: 8+1+ 3:8 large basic data type +String+3 SQL java.util.date java.sql.Date java.sql

5.1 JavaBean ref Bean =”” 5.2 List or Array 5.3 Map 5.4 Properties Create a Student class that defines these Properties

private HelloWold helloworld; private String []arr; private List list; private Map map; private Properties properties; Configure in XML configuration

<? 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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="helloworlds" class="p1.HelloWold">
<property name="name"> <value> </property> </bean> <bean id="ss" class="p1.Student">
<property name="helloworld">
<ref bean="helloworlds"> <! -- ref references another object --> </property> <property name="arr">
<list>
<value>aa</value>
<value>bb</value>
<value>cc</value>
<value>dd</value>
</list>
</property>
<property name="list">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
<property name="map"> < map > < entry > < key > < value > zs < value > / < / key > < value > zhang < value > / < entry > < entry > < key > < value > ls < value > / < / key > < value > and < value > / < / entry > < entry > < key > < value > ww < value > / < / key > < value > fifty < value > / < entry > < / map > < / property > <property name="properties">
<props>
<prop key="a2">222</prop>
</props>
</property>
 
</bean>
Copy the code

6. For the project, configure two methods of writing the file path ApplicationContext

String path = “applicationContext.xml”; (Developed independently)

String path = “classpath:applicationContext-*.xml”; // SRC (modular development multiplayer development)