Spring Framework
brief introduction
The Spring framework was created due to the complexity of software development
Purpose: To solve the complexity of enterprise application development
Functionality: Replace EJB with basic JavaBean and provide more enterprise application functionality
Scope: Any Java application
Advantage:
Open Source Free Framework (Container)
Spring is a lightweight, non-invasive framework
Container Framework for Controlling Inversion (IOC) and Face Oriented (AOP)
Supports transaction processing and framework integration
Official website: https://spring.io/
GitHub: https://github.com/spring-projects/spring-framework
Maven
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.4.RELEASE</version> </dependency>
spring seven modules
IOC Theory
IoC is fully known as Inversion of Control, or "Control Inversion". One of Spring's core contents is a way to get specific objects by describing (xml or annotations) and through third parties. The control inversion in Spring is achieved by the IoC container, which is implemented by DI (Dependency Injection), or dependent injection.
- Who controls whom: In the traditional development mode, we all create objects by using a direct new object method, that is, the objects you depend on are controlled directly by you, but once you have an IOC container, it is controlled directly by the IoC container.So "who controls who", of course, is the IoC container control object.
- What to control: Control objects.
- Why Inversion: Without IoC, we all actively create dependent objects in our own objects, which is a positive transformation.However, with IoC, the dependent object is directly created by the IoC container and injected into the injected object. The dependent object changes from active acquisition to passive acceptance, so it is reversed.
- What is reversed: The acquisition of the dependent object is reversed.
IoC Create Object
Creating objects through 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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="he" class="com.youzi.pojo.Hello"> <property name="Str" value="Hello Spring!"/> </bean> </beans>
public class Hello { private String Str; public Hello(String str) { Str = str; } public Hello() { } public String getStr() { return Str; } public void setStr(String str) { Str = str; } }
... @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Object he = context.getBean("he"); System.out.println(he); }
The assignment of other objects is referenced:
<bean id="userservice" class="com.youzi.service.UserServiceImpl"> <property name="user" ref="user"/> </bean>
Direct initialization without parameters
<bean id="user" class="com.youzi.dao.UserImpl"/>
This calls parameterless constructs and setters in the corresponding classes, that is, parameters or objects that need to be initialized must have corresponding setters. If you want to call parameterized constructs, you can use <constructor-arg>to pass them by type, subscript, parameter name, reference
Using Constructors
public Users(Hello hello, String num) { this.hello = hello; this.num = num; }
<bean id="users" class="com.youzi.pojo.Users"> <!--<constructor-arg type="java.lang.String" value="1"/>--> <!--<constructor-arg index="1" value="2"/>--> <constructor-arg name="num" value="3"/> <constructor-arg ref="he"/> </bean>
spring Basic Configuration
Aliases, create another name for the object, create multiple (aliases can also be created by name in bean s)
<bean id="he" class="com.youzi.pojo.Hello" name="hello3,hello4"> <property name="Str" value="Hello Spring!"/> </bean> <alias name="he" alias="hello1"/> <alias name="he" alias="hello2"/>
The bean object is created as a singleton mode by default and can be modified by scope="prototype", so hello3 and hello4 are two different objects
Import additional xml
The above configuration is just the basic one. There are many details in each configuration to learn later