What is Spring
First, Spring is a lightweight inversion of control (IoC) and aspect oriented (AOP) container framework
The spring framework is created because of the complexity of software development. Spring uses basic JavaBean s to do things that were previously only possible with EJB s. However, spring's use is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java applications can benefit from spring.
In other words, we learn Spring by learning its philosophy, that is, IoC and AOP. Its application range is so wide that any application can benefit from it.
Advantages of Spring
- Low intrusion / low coupling (reduce the coupling between components and realize the decoupling between software layers)
- Declarative transaction management (based on aspects and conventions)
- Easy to integrate other frameworks (such as MyBatis, Hibernate)
- Reduce the difficulty of Java development
- Spring framework includes J2EE three layers of each layer solution (one-stop)
Spring framework structure
Some concepts
- pojo is Plain Old Java Objects, simple old Java objects
- A JavaBean is a Java class that conforms to the JavaBean specification. JavaBean is a reusable component written in Java language. To write as a JavaBean, classes must be concrete and public, and have parameterless constructors. JavaBeans expose member properties of internal domains by providing common methods that conform to the consistent design pattern, and set and get methods are obtained. As we all know, property names conform to this pattern, and other Java classes can discover and operate the properties of these JavaBeans through introspection mechanism (reflection mechanism).
IoC
IOC: reverse of control
- Read as "reverse control" to better understand, it is not a technology, but a design idea, that is, the control of manually created objects in the program is handed over to the Spring framework for management.
- Positive control: to use an object, you need to be responsible for the creation of the object
- Reverse control: if you want to use an object, you only need to get the object you want to use from the Spring container. You don't care about the creation process of the object. That is to say, you have reversed the control of creating the object to the Spring framework
- Don't call me, I'll call you
A common example
Traditional approach:
For traditional patterns, we have code implementation like this
Juice juice = new Juice("orange","Polysaccharide","venti");
Factory mode:
Similarly, we have the following code implementation:
Shop shop = new Shop("orange","Polysaccharide","venti"); Juice juice = Shop.makeJuice();
In fact, for such a simple class, we do not need to introduce the factory pattern to increase the complexity, but for a more complex class (if oranges, sugar are a more complex class), we will reduce the code management cost if we use the factory.
With this example in mind, we write a simple Spring program.
First write a POJO (Legacy object)
package pojo; public class Source { private String fruit; // type private String sugar; // Sugar description private String size; // Size cup /* setter and getter */ }
Write configuration file to assemble bean
<?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 name="source" class="pojo.Source"> <property name="fruit" value="orange"/> <property name="sugar" value="Polysaccharide"/> <property name="size" value="venti"/> </bean> </beans>
Then try Spring
ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationContext.xml"} ); Source source = (Source) context.getBean("source"); System.out.println(source.getFruit()); System.out.println(source.getSugar()); System.out.println(source.getSize());
The output is as follows:
(the structure of our engineering documents is as follows)
How to create a Spring project
We use IDEA
This gives you a project that contains the Spring environment.
AOP
If IoC is the core of Spring, then aspect oriented programming is one of the most important functions of Spring. It is widely used in database transactions
AOP is Aspect Oriented Program
First of all, in the idea of aspect oriented programming, the functions are divided into core business functions and peripheral functions.
- The so-called core business, such as logging in, adding data and deleting data, is called core business
- So called peripheral functions, such as performance statistics, logs, transaction management, etc
Peripheral functions are defined as aspects in Spring's AOP idea of aspect oriented programming
In the idea of AOP, core business function and facet function are developed independently, and then the facet function and core business function are weaved together, which is called AOP
Concept in AOP:
- What classes and methods are pointcuts in
- Inform (Advice) what is actually done (when: before method / after method / before method and after method) (what: enhanced function)
- Aspect = pointcut + notification, the popular point is: when, where, and what enhancements to make!
- Weaving the process of adding facets to objects and creating proxy objects. (completed by Spring)
AOP programming
Let's do a demo in the form of a configuration file
First, we write the core functions:
package service; public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); } }
And peripheral functions
package aspect; //The dependency package here needs to be installed additionally. It is recommended to see this tutorial: https://blog.csdn.net/shuduti/article/details/53069241 import org.aspectj.lang.ProceedingJoinPoint; public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; } }
Edit configuration documents according to IoC
<?xml version="1.0" encoding="UTF-8"?> <!--Note that some dependency packages have been modified here --> <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="productService" class="service.ProductService" /> <bean id="loggerAspect" class="aspect.LoggerAspect"/> </beans>
Write test methods
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationContext.xml"} ); ProductService productService = (ProductService) context.getBean("productService"); productService.doSomeService(); }
Function
Next, let's focus on this section
We use AOP to cut in
We want to take doSomeService() as a starting point to enhance the logging function before and after its execution
Such 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: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="productService" class="service.ProductService" /> <bean id="loggerAspect" class="aspect.LoggerAspect"/> <!-- To configure AOP --> <aop:config> <!-- where: Where (package.class.Method) --> <aop:pointcut id="loggerCutpoint" expression="execution(* service.ProductService.*(..)) "/> <!-- what:What to enhance --> <aop:aspect id="logAspect" ref="loggerAspect"> <!-- when:At what time (before method)/after/Before and after --> <aop:around pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config> </beans>
Function:
We got the logging function, but we didn't modify the business code