Simple implementation of AOP in spring

Source: Internet
Author: User

View Code directly

  1. Package edu. HUST. springproxy;
  2. // Abstract role
  3. Public interface roledao {
  4. Public void dosomething ();
  5. }

  1. Package edu. HUST. springproxy;
  2. // Real role
  3. Public class roledaoimpl implements roledao {
  4. Public void dosomething (){
  5. System. Out. println ("eat beef ..... ");
  6. }
  7. }
  1. Package edu. HUST. springproxy;
  2. Import java. Lang. Reflect. method;
  3. Import org. springframework. AOP. methodbeforeadvice;
  4. Public class before implements methodbeforeadvice {
  5. // The before method defines the implementation of an advice.
  6. /*
  7. * The method parameter is the method that is executed after advice starts. The method name can be used as a condition for determining whether the code is executed.
  8. * Object [] ARGs is an array of parameters passed to the called public method. When logs are required, argS and method parameters are useful information.
  9. * Object target is a reference to the execution method M object.
  10. *
  11. * Interface: org. springframework. AOP. methodbeforeadvice
  12. * This interface has only one method: void before (method, object [] ARGs, object target) throws throwable
  13. * Method explanation: callback before a given method is invoked.
  14. * Parameters:
  15. * (1) method-method being invoked
  16. * (2) ARGs-arguments to the Method
  17. * (3) Target-target of the Method Invocation. May be null.
  18. *
  19. **/
  20. Public void before (method, object [] ARGs, object target) throws throwable {
  21. System. Out. println ("before doing something, I think I can do something else: such as --> judge the permission ..");
  22. }
  23. }
  1. Package edu. HUST. springproxy;
  2. Import java. Lang. Reflect. method;
  3. Import org. springframework. AOP. afterreturningadvice;
  4. Public class after implements afterreturningadvice {
  5. /*
  6. * Interface: org. springframework. AOP. afterreturningadvice
  7. * This interface has only one method: void afterreturning (Object returnvalue, method, object [] ARGs, object target) throws throwable
  8. * Method explanation: callback after a given method successfully returned.
  9. * Parameters:
  10. * (1) returnvalue-the value returned by the method, if any
  11. * (2) method-method being invoked
  12. * (3) ARGs-arguments to the Method
  13. * (4) Target-target of the Method Invocation. May be null.
  14. *
  15. **/
  16. Public void afterreturning (Object returnvalue, method, object [] ARGs, object target) throws throwable {
  17. System. Out. println ("after doing something, I think I can do something else: such as --> write a log ..");
  18. }
  19. }
  1. Package edu. HUST. springproxy;
  2. Import org. springframework. Context. applicationcontext;
  3. Import org. springframework. Context. Support. classpathxmlapplicationcontext;
  4. Public class client {
  5. Public static void main (string [] ARGs ){
  6. Applicationcontext CTX = new classpathxmlapplicationcontext ("applicationcontext. xml ");
  7. Roledao = (roledao) CTX. getbean ("proxy ");
  8. Roledao. dosomething ();
  9. }
  10. }

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring // DTD bean 2.0 // en" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <Beans>
  4. <! -- Beans in spring can only declare classes, but cannot declare interfaces. If you need to introduce interfaces in some bean property attributes, you can use value to introduce them. -->
  5. <Bean id = "roledaoimpl" class = "edu. HUST. springproxy. roledaoimpl"> </bean>
  6. <Bean id = "before" class = "edu. HUST. springproxy. Before"> </bean>
  7. <Bean id = "after" class = "edu. HUST. springproxy. After"> </bean>
  8. <Bean id = "proxy" class = "org. springframework. AOP. Framework. proxyfactorybean">
  9. <! -- For beans that have introduced the proxyfactorybean class:
  10. The name attribute value of the property tag introduced by "abstract role" must be proxyinterfaces;
  11. The name attribute value of the property tag that introduces "real role" must be target;
  12. The value of the name attribute of the property tag that introduces "proxy role" must be interceptornames;
  13. For the reason, see the source code of the proxyfactorybean class -->
  14. <! -- Proxyinterfaces: defines the proxy interface to implement. It can be one or multiple (if there are multiple interfaces, use the list tag) -->
  15. <Property name = "proxyinterfaces">
  16. <Value> edu. HUST. springproxy. roledao </value>
  17. </Property>
  18. <! -- Target: defines the proxy object -->
  19. <Property name = "target" ref = "roledaoimpl"> </property>
  20. <! -- Interceptornames: defines the notifications that the proxy is redirected to. It can be one or multiple (if there are multiple notifications, use the list tag) -->
  21. <Property name = "interceptornames">
  22. <List>
  23. <Value> before </value>
  24. <Value> after </value>
  25. </List>
  26. </Property>
  27. </Bean>
  28. </Beans>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.