**
Basic Introduction to spring
**
1. What is Spring?
Spring is a lightweight framework for IoC and AOP containers.A framework for providing basic services for Java applications to simplify the development of enterprise applications, which allows developers to focus solely on business needs.There are three common configurations: XML-based, annotation-based, and Java-based.
2. Advantages of Spring?
(1) spring is a low-invasive design with very low code pollution;
(2) spring's DI mechanism handles the dependencies between objects to the framework, reducing the coupling of components;
(3) Spring provides AOP technology to support centralized management of common tasks such as security, transactions, logs, permissions, and so on, providing better reuse.
(4) spring provides integration support for mainstream application frameworks.
AOP:
AOP, commonly referred to as Face-Oriented, supplements Object-Oriented by extracting and encapsulating common behaviors and logic that have business-independent but impact on multiple objects into a reusable module called Aspect, which reduces duplicate code and reduces code duplication in the systemIt improves the coupling between modules and the maintainability of the system.Can be used for privilege authentication, logging, transaction processing.
The key to AOP implementation is the proxy mode, which is mainly divided into static proxy and dynamic proxy (JDK dynamic proxy, cglib dynamic proxy).
Important terms for AOP:
**Section:** Refers to the extracted public modules (e.g. log files, rights management, etc.)
2) Join point: A method in Spring AOP where a connection point always represents the execution of a proxy object method.
(3) Advice: An action performed on a specific connection point of a facet.There are various types of notifications, including "around", "before" and "after".Many AOP frameworks, including Spring, use interceptors as notification models and maintain a chain of interceptors centered on connection points.
(4) Pointcut: The entry point is the definition of which Join point s we want to intercept.Specify the interception methods, such as add*, search*, through the entry point expression.
(5) Introduction: The introduction is the introduction of new interface enhancements on the basis of one interface.
(6) Target Object: An object notified by one or more facets.It is also called an adviced object.Since Spring AOP is implemented through a runtime proxy, this object is always a proxied object.
(7) Weaving: The process of applying enhancements to a target object to create a new proxy object.Spring is finished at run time.(Different combinations of cut-in methods)
--------
Copyright Notice: This is an original article by CSDN blogger "a745233700". It is reproduced in accordance with the CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement.
Original link: https://blog.csdn.net/a745233700/article/details/80959716
pointcut:excecution="modifier.return value.package name.class name.method name (parameter list)"
Insert a picture here
JDK dynamic proxy:
public class JDKDynamicProxy2 { private Object target; public JDKDynamicProxy2(Object target) { this.target = target; } public <T> T getProxy() { return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("proxy:"+proxy.getClass()); System.out.println("method:"+method); log(); Object result = method.invoke(target, args); return result; } }); } public void log() { System.out.println("log information...."); } }
Main function:
public class Test03 { public static void main(String[] args) { JDKDynamicProxy proxy=new JDKDynamicProxy(new oracleLogin()); login log=proxy.getProxy(); log.dologin(); } }
CGLIB dynamic proxy:
public class CglibProxy { private Object target; public CglibProxy(Object target) { this.target = target; } public <T>T createProxy() { return (T) Enhancer.create(target.getClass(), new MethodInterceptor() { @Override public Object intercept(Object arg0, Method method, Object[] arg2, MethodProxy arg3) throws Throwable { // TODO Auto-generated method stub System.out.println("proxy:"+arg0.getClass()); System.out.println("method:"+method); log(); Object result = method.invoke(target, arg2); return result; } }); } public void log() { System.out.println("log information...."); }
Main function:
CglibProxy proxy=new CglibProxy(new oracleLogin()); login log=proxy.createProxy(); log.dologin();
Notification:
(1) Before advice: A notification executed before a join point, but this notification cannot prevent execution before the join point unless it throws an exception.
(2) After returning advice: A notification that is executed after the normal completion of a join point: for example, a method returns normally without throwing any exceptions.
(3) After throwing advice: A notification that is executed when a method throws an exception and exits.(It and post-return notifications always execute only one)
(4) After (finally) advice: A notification that is executed when a connection point exits (usually in the final code block, it must be executed).
(5) Around Advice: A complete dynamic proxy process that has full control over when the facet method is executed and in a certain order. (1-4 The process may be executed in an incorrect order)
Notifications can be configured in two ways: xml files and annotations:
1. How to configure xml:
2. Ways of commenting
(1) Configure in the spring configuration file
<aop:aspectj-autoproxy/>
(2.)
/* * Define a method for declaring an expression for a starting point, which, in general, does not require any additional code to be added * Use @Pointcut to declare an entry point expression * Other subsequent notifications use the entry point method name instead * * */ // @Pointcut("execution(* com.yc.aop.annotation.*.*(..))") // public void declareJoinPointExpression() { // // } // // // //Pre-notification of the pre-notification declaration method notifies the target method before notifying it // // @Before("execution(* com.yc.aop.annotation.PersonBizImpl.add(..))") // public void beforeMenthod(JoinPoint joinPoint) { // String methodName=joinPoint.getSignature().getName(); // //Get parameters // List<Object>list=Arrays.asList(joinPoint.getArgs()); // System.out.println ("logging before method:"+methodName+ "parameters"+list); // } // // //Post Notification Declares that a method's post notification is invoked after the method executes (whether an exception occurs or not) // // @After("declareJoinPointExpression()") // public void afterMenthod(JoinPoint joinPoint) { // String methodName=joinPoint.getSignature().getName(); // List<Object>list=Arrays.asList(joinPoint.getArgs()); // System.out.println ("logging agter method:"+methodName+ "parameters"+list); // } // //Return Notification // @AfterReturning(value="declareJoinPointExpression()",returning="result") // public void afterReturnMethod(JoinPoint joinPoint,Object result) { // String methodName=joinPoint.getSignature().getName(); // System.out.println("logging afterReturnMethod:"+methodName+"Result:"+result); // } // /* // * Code executed when the target method has an exception // * Exception objects can be accessed: and execution code with a specific exception can be specified // * // * */ // @AfterThrowing(value="declareJoinPointExpression()",throwing="ex") // public void afterThrowingMenthod(JoinPoint joinPoint,Exception ex) { // String methodName=joinPoint.getSignature().getName(); // System.out.println("logging afterThrowingMenthod:"+methodName+"exception information:"+ex.getMessage())); // } //Wrap notifications need to carry ProceedingJoinPoint parameters /*A ProceedingJoinPoint parameter similar to the dynamic proxy entire process determines whether the target method is executed or not * */ @Around("execution(* com.yc.aop.annotation.*.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable { Object result=null; Object methodName=pjd.getSignature().getName(); try { //Pre-notification of the pre-notification declaration method notifies the target method before notifying it System.out.println("logging before method: "+methodName+"parameter"+Arrays.asList(pjd.getArgs())); //Post-notification declares a method's post-notification is called after the method executes (whether an exception occurs or not) result=pjd.proceed(); System.out.println("logging after method: "+methodName+"parameter"+result); }catch (Exception e) { // TODO: handle exception } return result; } }