1. Introduction to Spring Framework
(1 Introduction
Spring is a framework for structural J2EE systems based on IOC and AOP
(2) What is IOC
IOC inversion of control is the basis of Spring. Inversion Of Control
simply means that the creation of objects is called by the previous programmer's own new construction method, and it becomes the creation of objects by Spring.
(3) What is DI
DI Dependency Injection Dependency Inject. Simply put, the properties of the obtained object have been injected with relevant values and can be used directly.
(4) What is AOP
AOP is Aspect Oriented Program, aspect-oriented programming.
First of all, in the idea of aspect-oriented programming, functions are divided into core business functions and peripheral functions.
The so-called core business, such as login, adding data, and deleting data are all
called core business. The so-called peripheral functions, such as performance statistics, logs, transaction management, etc.
Peripheral functions are defined as aspects in Spring's aspect-oriented programming AOP idea.
In the idea of aspect-oriented programming AOP, the core business function and the aspect function are developed independently,
and then the aspect function and the core business function are "weaved" together, which is called AOP.
2. Spring framework case implementation steps
(1) Create a new java project type project
(2) Download the dependency package and import it
链接:https://pan.baidu.com/s/1EQJ9-9AjgQ39TP1XRauVcw?pwd=t7le
提取码:t7le
Import the jar package into the project, the way to import the package: right click on project->properties->java build path->libaries->add external jars
(3)pojo
package com.how2java.pojo;
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
(4)applicationContext.xml
Create a new applicationContext.xml file in the src directory.
applicationContext.xml is the core configuration file of Spring. The Category object can be obtained through the keyword c. When the object is obtained, the string "category 1" is injected into the name attribute
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
</beans>
(5) test
The test code demonstrates that the Category object is obtained through spring, and the name property of the object is injected.
As shown in the figure, you can print out the name attribute of the Category object obtained through Spring
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
3. Principle
Compare by getting objects
The traditional way:
Actively create an object through the new keyword
IOC way
The life cycle of the object is managed by Spring, and an object is directly obtained from Spring. IOC is the abbreviation of Inversion Of Control (Inversion Of Control), just like the control right was originally in his own hands and handed over to Spring.
To make an analogy: the
traditional way: it is equivalent to going to the vegetable market to buy a new chicken, but it is a raw chicken. You have to pluck it yourself, remove the internal organs, add pepper, soy sauce, roast it, and go through various processes before it can be eaten.
Using IOC: It is equivalent to going to a restaurant (Spring) and ordering a chicken. When it is handed over to you, it has all kinds of flavors, and you can just eat it.
4. Practice
Use IOC to get a Product object.
(1)Product
package com.how2java.pojo;
public class Product {
private int id;
private String name;
private double price;
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", category=" + category + "]";
}
}
(2)applicationContext.xml
new content
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="手机" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="id" value="17" />
<property name="name" value="iphone14" />
<property name="price" value="7800" />
<property name="category" ref="c" />
</bean>
</beans>
(3) Test TestSpring02
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Product;
/**
* IOC DI
* @author taowenmrng
*
*/
public class TestSpring02 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Product p = (Product) context.getBean("p");
System.out.println(p);
}
}