Static agent of SPRINGAOP

Source: Internet
Author: User
Tags ming

first, springaop:

   

⒈aop: Aspect oriented programming-oriented slicing programming, the realization of the core business and non-core business between the separation, so that core class only do core business, proxy class only do Non-core business.

objective:

      ⑴, separating core and Non-core business    

      ⑵, to turn multiple non-core business processing (points) into a tangent (the previous Non-core business code needs to be written multiple times to write only Once. )

⒊ Requirements: It is reasonable to use AOP only when there are multiple businesses in the core class that need to be processed, and it is not necessary to use AOP if only one business needs to be Processed.

second, Static Agent:

   1, need to know the core class (the proxy Class) is which class, and what Method.

2, Non-core code needs to repeat several times, it appears that the structure of the code is bloated, resulting in code REDUNDANCY.

3, Non-core class (proxy Class) need to implement the core class (the proxy Class) implementation of the interface, that is, they need to implement a common interface, but the core class implementation of the interface (the proxy Class) Prevail.

third, Dynamic Agent:

1, do not need to know the core class (the proxy Class) is specifically what class.

2, Non-core class (proxy Class) need to implement Invocationhandler Interface.

four, Static proxy Example:

    1, Business Introduction:

Assuming the senior students as the core class, the Student's family is the proxy class. The core business that senior students need to do is to study at home (studyathome), study at school (studyathome), and the Non-core business that families need to do: prepare meals (buy, wash, fry, cook). Prepare supplements (buy, boil, Add seasoning).

2, the specific code:

⑴, creating an interface for a senior student: g3student

        

 package aop_001;


// first define an interface that is used by the senior students to achieve, // declare in this interface a way to learn at home and to learn in school
public Interface g3student { publicvoid studyathome (String core); public void studyatschool (String core); }

  

⑵, Create a senior student implementation Class: g3student_boy, and this class implements the senior student interface (g3student)

  

 packageaop_001;//defines the class of a senior boy (target object, core object, target object), and realizes the interface of the senior Students.  public classG3student_boyImplementsg3student {//the first Three-boy business Process: learning at home     public voidStudyathome (String Core) {
//core things to learn at Home. System.out.println (core+ "learning at home"); } //the second business process for senior three boys: learning at school public voidStudyatschool (String Core) {//core things to learn at Home. System.out.println (core+ "learning in schoool"); }}

⑶, Create a senior Student's agent Class: g3student_proxy, and The realization of the senior students Interface : G3student. The red font needs to be understood

 packageaop_001;/*The purpose of the proxy object (g3student_proxy) is to help the core object (g3student_boy/g3student_girl) do Non-core things. * But the proxy object (g3student_proxy) must implement a common interface with the core object (g3student_boy/g3student_girl). */ public classG3student_proxyImplementsg3student {Define a senior student interface as a property, the goal is to call after the agent needs to do things to invoke the senior senior or senior senior girls need to do the core business,//but not the agent specifically to do these core things, just call Them.            Private G3student g3s;    Create an agent with the parameters of the senior student interface constructor, Judging but the incoming string for boy constructs an instance of a senior male, if the passed parameter is girl, constructs an instance of a senior Girl.        Public g3student_proxy (String Sex) {if ("boy". equals (sex)) {g3s = new G3student_boy ();        }else if ("girl". equals (sex)) {g3s = new G3student_girl (); }            }
public voidstudyathome (String Core) {//This is the process by which the agent (g3student_proxy) prepares the meal:System.out.println ("agent: ready to buy food"); System.out.println ("agent: Prepare to wash vegetables"); System.out.println ("agent: Prepare Stir fry"); System.out.println ("agent: Prepare to cook rice"); System.out.println ("-----------------"); //the method that invokes this specific instance is created by defining the property and creating the corresponding instance in the call to the constructor with the G3student_proxy parameter.G3s.studyathome (core);
     //This is the process by which agents (g3student_proxy) prepare supplements:System.out.println ("-----------------"); System.out.println ("agent: buy tonic"); System.out.println ("agent: Boil the Department of products"); System.out.println ("agent: Add seasoning"); System.out.println (); } public voidStudyatschool (String Core) {//This is the process that the agent prepares (g3student_proxy) the food needs to do:System.out.println ("agent: ready to buy food"); System.out.println ("agent: Prepare to wash vegetables"); System.out.println ("agent: Prepare Stir fry"); System.out.println ("agent: Prepare to cook rice"); System.out.println ("-----------------"); //the method that invokes this specific instance is created by defining the property and creating the corresponding instance in the call to the constructor with the G3student_proxy parameter.G3s.studyatschool (core);
System.out.println ("-----------------"); //This is the process by which agents (g3student_proxy) prepare supplements:System.out.println ("agent: Buy tonic"); System.out.println ("agent: Boil the Department of products"); System.out.println ("agent: Add seasoning"); System.out.println (); }}

        

⑷, Testing: Defining a testing class test

 package aop_001;  public class Test {    publicstaticvoid  main (string[] Args) {                        // Create a proxy object, and pass in the corresponding parameters to construct a concrete instance        New G3student_proxy ("boy");         // execute related methods through this proxy object (the proxy object and the core object have a common Interface)        B.studyathome ("xiao ming");        B.studyatschool ("xiao ming");     }}

 Test Results:

   3, summary:

From the above example we can simply simulate an instance of a static proxy, but we find that the agents do things because the core objects are more business and become more, and these agents do things are the same unchanged:

and, If we want to modify one of these processes in the process will be found to change more places, and all the same code, so this time using dynamic agent can easily solve the Problem.

Thanks for browsing!

     

Static agent of SPRINGAOP

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.