1. What is Spring

Spring is a lightweight Java development framework that emerged in 2003 and was created to address the complexities of enterprise application development. At the heart of Spring are inversion of Control (IoC) and Aspect Oriented programming (AOP). Spring is a lightweight open source framework that can be used in Java SE/EE.

2. The effect of the Spring

(1) Decoupling, and handing over the relationship between object creation and management objects to Spring

(2) Provide AOP support

(3) Support for declarative transactions

(4) Convenient integration of various frameworks :Spring is like a plugboard, and other frameworks are plugs, which can be easily combined together. Whichever frame you want to use, put this plug into the plugboard.

Inversion of control (INVERSION of Control) is a concept. It is an idea. It refers to the assembly and management of objects by the container, which is controlled directly by the program code. An inversion of control is a transfer of control over an object from the program code itself to an external container. Through the container to achieve object creation, attribute assignment, dependency management.

The Spring framework uses dependency injection (DI) to implement IoC. The Spring container is a giant factory that creates and manages all of the Java objects, called beans. The Spring container manages the dependencies between the beans in the container. Spring uses a “dependency injection” approach to manage the Bean dependencies. Use IoC to decouple and sum objects

4. Spring implementation steps

Defines the interface to the service and its implementation class

Define the spring configuration file, and the bean tag is the object that creates the class

       <bean id="customerService" class="com.yuan.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"/>
       </bean>
Copy the code
Public void testAdd (){String config="applicationContext.xml"; / / configuration file ApplicationContext ac = new ClassPathXmlApplicationContext (config); CustomerService = (CustomerService) ac.getBean(" CustomerService "); Customer Customer =new Customer(); customer.setCust_id(110); customer.setCust_name(""); service.addCustomer(customer); }Copy the code

5.DI implementation of XML implementation

(1)XML:set injection, construct injection

Set injection for simple types: Java basic data types and strings are both simple types

The < bean id = "" class =" "> < property name =" attribute name "value =" value "/ > < / bean >Copy the code

Set injection of reference types

The < bean id = "" class =" "> < property name =" attribute name "ref =" reference objects "/ > < / bean >Copy the code

6. Automatic injection of reference types

ByName: automatic injection byName

Note that the id attribute of the called bean tag must be the same as the attribute name of the caller’s javaBean class

<bean id="" class="" autowire="byName"> <property name="" value=""/> <property name="" value=""/> </bean> <bean Class =""> <property name="" value=""/> </bean>Copy the code

ByType: automatic injection based on the type

One of three things must happen: the class specified by the class attribute of the called bean must be of the same type as the reference variable of the caller’s javaBean class or bean implementation or subclass of that class

<bean id="" class="" autowire="byType"> <property name="" value=""/> <property name="" value=""/> </bean> <bean Id =" class=""> <property name="" value=""/> </bean>Copy the code

7. Comments on the implementation of DI

Using annotations for DI eliminates the need to declare bean instances in the Spring configuration file. Using annotations in Spring requires some changes to the existing Spring runtime environment. The component scanner needs to be configured in the Spring configuration file to scan for annotations in the specified base package.

<context:component-scan base-package=""/>Copy the code

The container launch scans for comments in the package and its subpackages, as well as subpackages below them.

Define the annotation @Component for the Bean

You need to use the @Component annotation on the class

@Component(" Component name ") public class CustomerCopy the code

@repository annotates the DAO implementation class @Service annotates the Service implementation class @Controller annotates the Controller implementation class

These three annotations, along with @Component, can create objects, but these three annotations also have other meanings. @Service creates business layer objects, which can be added to transaction functions, and @Controller creates objects that can be used as handlers to receive user requests.

Simple type attributes are injected at @Value

You need to use the @value annotation on the attribute. The Value attribute of the annotation is used to specify the Value to inject.

@Value("XXX")
private String name;
Copy the code

ByType is automatically injected into @autowired

Add @autowired to the reference type and then use the attribute injection of a simple type for the reference type

You need @AutoWired to inject the same classes as the entity classes

ByName automatically injects @autowired and @Qualifier

The @autowired @ the Qualifier propertiesCopy the code

You need @Qualifier(name) to be the same as @Component(name)

Spring provides support for @Resource annotations in the JDK. The @Resource annotation can match beans either by name or by type. The default is by name.

8. AOP programming

Aspect Orient Programming (AOP), Aspect oriented Programming. Aspect – oriented programming considers the process from a dynamic perspective. The underlying AOP is realized by using dynamic proxy pattern. Two kinds of proxy are used: JDK dynamic proxy and CGLIB dynamic proxy.

(1) Aspect generally refers to cross business logic. Transaction processing and log processing in the above example can be understood as sections. A common aspect is Advice. It is actually an enhancement to the main business logic.

A JoinPoint refers to a specific method that can be woven into a section. Typically, methods in a business interface are join points.

A Pointcut is a collection of one or more declared join points. Specify a set of methods through a pointcut. Methods marked final cannot be used as join points or pointcuts. Because the final one can’t be modified, can’t be enhanced.

(4) Target refers to the image to be strengthened. That is, the image of the class containing the master business logic. In the previous example, if the object of StudentServiceImpl is enhanced, the class is called the target class, and the object of the class is called the target object. Of course, without enhancement, there would be no goal.

(5) Advice A notification indicates the execution time of a section. Advice is also called enhancement. MyInvocationHandler in the example above can be interpreted as a notification. On the other hand, notifications define the point at which the enhancement code enters the target code, before or after the target method executes, and so on. Different notification types have different timing. Pointcuts define where to cut in, and notifications define when to cut in.

There are five types of advice commonly used in AspectJ: (1) pre-advice (2) post-advice (3) surround advice (4) exception advice (5) final advice

AspectJ pointcut expressions

AspectJ defines specialized expressions for specifying pointcuts

Access method return value method declaration (parameter) exception typeCopy the code

For example execution(public * (..)) ) specifies that the pointcut is: any public method. execution( set*(..) ) specifies that the pointcut is: any method that starts with “set”.

AspectJ’s annotation-based AOP implementation

(1) Define the business interface and implementation class (2) define the aspect class (3) declare the target object the aspect class object (4) register AspectJ’s automatic proxy

// <aop:aspectj-autoproxy/>Copy the code

This part of spring transactions is not understood yet and will not be summarized