Home  >  Article  >  Java  >  Spring Framework Learning (6) AOP

Spring Framework Learning (6) AOP

黄舟
黄舟Original
2016-12-29 13:21:021230browse

AOP (Aspect-Oriented Programming) aspect-oriented programming is completely different from OOP. Using AOP, the programming system is divided into aspects or concerns, rather than objects in OOP.

Introduction of AOP

In the use of OOP object-oriented, code duplication is inevitable, and using object-oriented programming, this duplication cannot Avoid, for example, when judging user permissions, execute the corresponding method according to the corresponding permissions; when setting the encoding format in the servlet, the same code appears many times, and it has nothing to do with the business. It is easy to forget to write, and the result will appear when running. Garbled code. This duplication of code not only makes coding cumbersome, but also makes it difficult to maintain. AOP organizes these codes, puts the code that solves a certain aspect problem separately in a certain module, and then weaves it into the program.

Terms in AOP

Spring Framework Learning (6) AOP

Aspect: cross-cutting functions,
abstract classes, or interfaces, the important thing about AOP programming is Cross-cutting features are identified.
(Aspects, similar to the character encoding function)
Advice: The specific implementation of the cross-cutting function needs to be analyzed based on the actual situation. If it is before before the target object is operated and after the operation, it is after
advice.
(enhanced, similar to character encoding filter)
Pointcut: entry point, describing the limitations of cross-cutting function application, not all processes are required, those places that can be used are entry points
(similar to Filter matching rules /*)
Joinpoint: Connection point, or the time when a component joins the process, such as setting properties, calling methods, etc. Spring only supports connection points for method calls, while some other frameworks support attributes Connection points such as: AspectJ,
(filter rules similar to filters REQUEST, FORWARD)
Weave: Stitching, the process of applying components to business processes, is called stitching or weaving.
(Similar to the process of configuring the filter to the web.xml file)
Proxy, proxy, in terms of implementation, Spring's AOP actually uses the dynamic proxy of JDK (using the interface to complete the proxy operation), also CGLIB can be used (proxy operations are completed using inheritance).
Target, target, the actual object of the business operation


Example: Setting the character encoding format is regarded as an Aspect, and the interceptor is an Advice enhancement.



characterFilter
com.bjpowernode.egov.filter.CharacterEncodingFilter


characterFilter
/servlet/*

Filter class

public class CharacterEncodingFilter implements Filter {
 
   @Override
   public void destroy() {}
 
   @Override
   public void doFilter(ServletRequest request, ServletResponse response,
      FilterChainchain) throws IOException, ServletException {
           request.setCharacterEncoding("GB18030");
           chain.doFilter(request,response);
   }
 
   @Override
   publicvoid init(FilterConfig filterConfig) throws ServletException {}
}

This way there is no need to set the encoding in each servlet. .

Usage of AOP in the spring framework

1, copy the jar package

Spring Framework Learning (6) AOP

2, in the spring configuration file Add namespace and constraint files

Enable aop function: just add the tag and you can pull it.

Spring Framework Learning (6) AOP

3, write the proxied class and extended class

Spring Framework Learning (6) AOP

4, declared by the configuration file









	
		
		
		
		
		
	

5, if the test

is successful, when executing the target method targetmethod() in the target class, the beforemethod() method in the extension class will be executed first, then the target method, and finally aftermethod() method.
That is, we only need to manually call the targetmethod method. The two method frameworks in the extension class will read the configuration file during execution, obtain the corresponding information, and automatically add the extended method to us. .
The test will definitely be successful. If you don’t believe it, you can try it yourself.

Advantages of using AOP in the Spring framework

Aop is integrated with spring's IOC container, enhanced, and the entry points are all javabeans, which can be configured in the same file
Like other parts of spring, it can be Any transplant between different application servers
spring implements Aop's interception interface, so that users do not have to bind to a specific interceptor interface

aop's aspect-oriented programming ideas break the object-oriented way of thinking, we What you need to learn is not only the use of aop, but also the aspect-oriented thinking.

The above is the content of spring framework learning (6) AOP. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn