Detailed explanation of Spring and how to use Spring?

Introduction to Spring: 1.
Terminology
1. Intrusive design The
introduction of a framework has an impact on the structure of existing classes, that is, it is necessary to implement or inherit some specific classes. Such as: Struts framework
2. Non-intrusive design The
introduction of the framework has no impact on the existing class structure. Such as: Hibernate, Spring
3. Inversion of Control (IoC)
Inversion of Control (Inversion on Control, IoC): The creation of objects is handed over to the external container to complete.
4. Dependency injection (DI)
Dependency injection: dealing with dependencies between objects
Class tight coupling problem. For example, class A depends on class B. The conventional practice is to create an object of B directly in A, and then call the method of object B; inversion of control is to hand over the instantiation of creating object B to a third-party implementation; and then create Class B operates interface I, and creates an object of interface I in A, and finally a third party injects an instance of B into the object of interface I in A. In this way, the control of instantiating the object of class B is given to the third party by A, and the problem of tight coupling between class A and class B is also solved. 6.AOP Aspect Oriented Programming (Aspect Oriented Programming) is the product of the development of software programming ideas to a certain stage, and is a useful supplement to object-oriented programming. AOP is generally suitable for occasions with cross-cutting logic, such as access control, transaction management, performance monitoring, etc. Aspect-oriented programming simply means adding new functions to the code segment without changing the source program and enhancing the code segment. 7. Crosscutting Logic






In a business system, there are always some things that are scattered, penetrated into the system and have to be dealt with. These common operations interspersed in a given business are the so-called cross-cutting logic, also known as aspect.
8. Enhancement processing The operations or functions performed before and after the execution of
the target method are enhancement processing.
9. Pointcut The target method into which enhancement processing
can be inserted is the so-called pointcut.

2. Introduction to Spring The
Spring framework is a lightweight framework that can solve object creation and dependencies between objects. Spring is a comprehensive, one-stop solution for enterprise application development. Spring runs through the presentation layer, business layer, and persistence layer. But Spring still integrates seamlessly with other frameworks.

3. Introduction of Spring Jar package
1.org.springframework.aop - Spring's aspect-oriented programming, providing the implementation of AOP (aspect-oriented programming)
2.org.springframework.asm - spring3.0 began to provide its own independent asm jar package
3.org.springframework.aspects - the integration of AspectJ framework provided by Spring 4.org.springframework.beans -
used by all applications, including accessing configuration files, creating and managing beans, etc.
5.org.springframework.context.support - Spring context extension support for MVC
6.org.springframework.context - Provides extended services on basic IOC functions, in addition to providing support for many enterprise-level services, There are mail services, task scheduling, JNDI positioning, EJB integration, remote access, caching and support for a variety of view layer frameworks.
7.org.springframework.core - Spring's core toolkit, other packages depend on this package
8.org.springframework.expression - Spring expression language
9.org.springframework.instrument.tomcat - Spring's connection pool for tomcat Integration
10.org.springframework.instrument - Spring's proxy interface to the server 11.org.springframework.jdbc -
Simple encapsulation of JDBC 12.org.springframework.jms - Simple encapsulation
to simplify the use of jms api
13.org.springframework.orm - Integrate third-party orm implementations, such as hibernate, ibatis, jdo, jpa, etc.
14.org.springframework.oxm - Spring's support for object/xml mapping, allowing JAVA and XML to switch back and forth
15.org.springframework.test - Simple encapsulation of test frameworks such as JUNIT
16.org.springframework.transaction - Provides consistent declarative and programmatic transaction management for JDBC, HIBERNATE, JDO, JPA
17.org.springframework. web.portlet - Spring MVC enhancement
18.org.springframework.web.servlet - support for J2EE6.0 servlet3.0
19.org.springframework.web.struts - Integrate support for struts framework, more convenient and easier to integrate Struts framework.
20.org.springframework.web - contains the core classes required when using the Spring framework when developing web applications.
21.Org.springframework.web.mvc - contains the core classes required for SpringMVC application development.
Note : core, beans, expression, and context are the core jar packages of Spring. The commons-logging-*.jar package must be imported when using Spring.
Fourth, applicatonContext.xml - detailed bean configuration
1. Spring related namespace introduction
<?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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http: //www.springframework.org/schema/aop"  
    xmlns:context="http://www. 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

Five, processing enhancement
1. Processing enhancement type
Pre-enhancement MethodBeforeAdvice Post
-enhancement AfterReturningAdvice
Surround enhancement MethodInterceptor : MethodBeforeAdvice The post-enhanced interface is: AfterReturningAdvice The surround-enhanced interface is: MethodInterceptor Throws exception enhanced interface is: ThrowsAdvice Enhancement through annotation Enhanced through Scheme configuration 3.Enhanced through interface implementation <aop:config > <aop:pointcut expression="execution(public void *User())" id="addoptpointcut"/> <aop:advisor advice-ref="logbefore" pointcut-ref="addoptpointcut" /> </aop:config >         <aop:config>
















<aop:pointcut expression="execution(* spring_basic.UserService.*(..))" id="userServiceOptPointcut"/>
<!--
<aop:advisor advice-ref="exceptionAdvice" pointcut-ref="userServiceOptPointcut" />
-->
<aop:advisor advice-ref="logger" pointcut-ref="userServiceOptPointcut" />
</aop:config>
4.通过注解实现增强
前置增强     
@Before("execution(* service.*.*(..))")
后置增强
@AfterReturning(pointcut="execution(* service.*.*(..))", returning="result")
异常抛出增强 
@AfterThrowing(pointcut="execution(* service.*.*(..))", throwing="ex")
环绕增强     
@Around("execution(* service.*.*(..))")
Note: You also need to add the following configuration to the applicationContext.xml file
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
5. Enhancement through Scheme configuration
First : add a common class, and write methods in it as required.
public class Logger {
public void before(JoinPoint jp){
}
public void after (JoinPoint jp, Object result){
}
public void aterThrowing(JoinPoint jp, Exception ex){
}
public Object aroundExecute(ProceedingJoinPoint pjp) throws Throwable{
}
  }
Second: Configure aop-aspect related configuration in applicationContext.xml
<aop: config>
<aop:pointcut expression="execution(* service.*.*(..))" id="optpointcut"/>
<aop:aspect ref="mylogger">
<aop:before method="before" pointcut -ref="optpc" />
<aop:after-returning method="after" returning="result" pointcut-ref="optpc" />
<aop:after-throwing method="aterThrowing" throwing="ex" pointcut-ref="optpc" />
<aop:around method="aroundExecute" pointcut-ref="optpointcut"/>
</aop:aspect>
</aop:config>
六、拆分Spring配置文件
方式一:在web.xml文件中加载多个spring配置文件
<!-- spring监听器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml,classpath:applicationContext-dao.xml,
    classpath:applicationContext-service.xml,classpath:applicationContext-action.xml,
    </param-value>
  </context-param>
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
Method 2: Load multiple spring configuration files by wildcard
<!-- spring listener-->
  <context-param >
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml,
    classpath:applicationContext-*.xml
    </param-value>
  </context-param>
  <listener>
<listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
Method 3: Use import to load other spring configuration files
<import resource="applicationContext-dao.xml" />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326332152&siteId=291194637