The core of Spring
Spring's core concepts are inversion of control (Ioc), aspect oriented programming (AOP), and dependency injection (DI)! These three core technologies are the basis of learning spring, and most of the others are based on their integration. Let's introduce them separately
Spring's official website address: https://spring.io
1. Inversion of control (Ioc): concept (inversion of control)
2. The role of IOC
Reducing the coupling between computer programs (removing the dependency in our code) can only be reduced, not eliminated completely!
spring ioc can't add, delete, modify or query our database, or encapsulate our presentation layer data. It can't accept requests. What it can do is simply to solve the coupling between programs.
3. IOC interpretation in spring
When we used to write Java programs, when we created a class, we used to go to the class itself to create an object. However, this is not the case in spring. For example, our target class: UserService interface and implementation class. Before obtaining the UserService instance, we can directly new an object. Now, we will go to spring to create an instance of the object. We will go through this process It's called inversion of control (IOC). The permission to create an instance is given to spring. What if we need an object instance in the future? The answer is simple and easy to know. Just take it from spring and use it!
- IOC example
1. Create a new spring project
2. Create a new profile
3. Create a new com.main package and a new class under src
package com.main; import com.test.IntroduceDemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class testMain { public static void main(String[] args) { //Create Spring context (load bean.xml) ApplicationContext acx= new ClassPathXmlApplicationContext("beans.xml"); //Get the instance of the class in testMain, and get the IntroduceDemo id=acx.getBean("IntroduceDemo",IntroduceDemo.class); //Call the method to display the name and age id.show(); } }
4. Create a package com.test under src, and create a new class
package com.test; public class IntroduceDemo { //Full name private String name; //Age private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * Self introduction. */ public void show(){ System.out.println("Hello, my name is"+this.name+"This year"+this.age+"Year old!"); } }
5. In beans.xml of src directory, the code is as follows:
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="IntroduceDemo" class="com.test.IntroduceDemo"> <property name="name" value="An easy nest in the yard"/> <property name="age" value="23"/> </bean> </beans>
Project structure:
Result: object created by spring succeeded
Dependency injection
Dependency injection is a way for spring containers to build dependency relationships for their objects. We give spring a list of objects that need to be created. For example, to create a object and objects that depend on it, first look them up in the list. If there are any dependent objects, assume that a object depends on many objects, and do not know whether other objects exist or where other objects are And how they create them, and the spring container will actively create and inject A-dependent objects into a, such as A-dependent C, which will automatically inject C into A-dependent objects!
DI problem introduction
1. First of all, we need to understand what dependency is: one object needs to use another object, so we say they have dependency.
class B { private A a; //Class B depends on Class A }
For example, there are two classes: A and B, both of which have show methods
public class A{ public void show(){} }
public class B{ public void show(){} }
Suppose the show() method of A needs to call the show() method of B to complete some functions
// An highlighted block public class A{ private B b; public class A(B b){ this.b=b; } public void show(){ this.b.show(); } }
In practice, what if show in A needs to call A lot of show like methods? When creating an object, the amount of code is large and the modification is tedious. At this time, find it in the object list provided by spring. If A needs any object, find it and return the object directly. That will be very comfortable and conducive to expansion!
DI case demonstration
Target class
Create BookService interface and implementation class
// An highlighted block var foo = 'bar';
Create BookDao interface and implementation class
Configure the dao and service xml files
test
Create three new packages under src: com.service, com.dao and com.main
com.service: an interface and an implementation class
package com.service; public interface BookService { public abstract void addBook(); }
package com.service; import com.dao.BookDao; public class BookServiceImpl implements BookService{ private BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } @Override public void addBook(){ this.bookDao.addBook(); } }
com.dao: an interface and an implementation class
package com.dao; public interface BookDao { public void addBook(); }
package com.dao; public class BookDaoImpl implements BookDao{ @Override public void addBook(){ System.out.println("di addBook()"); } }
main function test
package com.mainjava; import com.service.BookService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { String xmlPath = "springContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); BookService bookService = (BookService) applicationContext.getBean("bookServiceId"); bookService.addBook(); } }
springContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bookServiceId" class="com.service.BookServiceImpl"> <property name="bookDao" ref="bookDaoId"></property> </bean> <!-- Establish dao Example --> <bean id="bookDaoId" class="com.dao.BookDaoImpl"></bean> </beans>
Aspect oriented programming (AOP)
Let's first understand the function of AOP in Spring and then analyze AOP carefully
In spring, AOP is easy to program. Many functions that are not easy to implement with traditional OOP can be easily handled with AOP.
Spring provides face-to-face programming, which can easily implement functions such as permission interception, running monitoring, etc. because of the length, the next chapter will continue to write AOP!