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
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.
<!-- 字符编码过滤器--> <filter> <filter-name>characterFilter</filter-name> <filter-class>com.bjpowernode.egov.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping>
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
2, in the spring configuration file Add namespace and constraint files
Enable aop function: just add the tag and you can pull it.
3, write the proxied class and extended class
4, declared by the configuration file
<!--声明目标类--> <bean id="targetclass" class="com.spring.aop.TargetClass"></bean> <!--声明扩展类--> <bean id="extendsclass" class="com.spring.aop.extendsClass"></bean> <!--织入扩展类,并声明在哪个方法上执行扩展类--> <aop:config> <aop:aspect id="extendAspect" ref=""> <aop:pointcut expression="execution(public ** (..))" id="extendsPoincat"> <!--在目标方法执行前执行的方法--> <aop:before method="beforemethod" pointcut-ref="extendsPoincat" /> <!--在目标方法执行后执行的方法--> <aop:after method="aftermethod" pointcut-ref="extendsPoincat" /> </aop:aspect> </aop:config>
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 (www.php.cn)!