Aspect-oriented programming and one of JBoss

  Abstract: The aspect-oriented programming and one of JBoss 

  Contents: Summary aspect-oriented programming (Aspect-Oriented Programming, AOP) is an exciting new model.    On the development of software systems, its influence will be and 15-20 in the same object-oriented.    Aspect-oriented programming and object-oriented programming is not competing not only the technical but also can be good complementary.    Object-oriented programming targeted primarily to the same level of public behavior modeling.    It is the weakness of the public will be applied to a number of unrelated acts between object model.    This is precisely AOP suitable areas.    AOP allows you to define cross-cutting relations, those relations for transnational separate, very different object model.    AOP allows you to levels of functionality rather than embedded functionality, it makes the code can be in a better and easier maintenance.    I like to think that OOP is the top-down software development, and AOP since left and the right software development, they are completely orthogonal technologies, and the mutual good added. 
  In OOP is a tool, inheritance, and polymorphism package, and the components of AOP is to inform / interceptors, Introduction, metadata and pintcuts Let us look at these definitions. 

  Notice / interceptors, a notice is a logic, this logic there is a specific incident triggered.    It is, this behavior can be inserted in the call and be called between, in a method invocation and practical ways between.    AOP really notice is the key.    Notice allows you to apply a transparent things like logs and records to an existing object model. 
  In JBoss AOP, we use the interceptors is to achieve a circular.    You can define interceptors, it interception Method Invocation, the constructor calls and visits domain.    Behind, we will clarify how the application of these interceptors to an existing object model. 

  Introduction Introduction is a method or an increase to an existing domain of the way.    They even allow you to change the existing category is significant interface, and the introduction of a mixed class that is to achieve a new interface.    Introduction allows you to inherit more generally into the Java class.    Introduction One of the main use cases when you have a connection, you want this there is a run-time when pretext.    You want your application across different aspects of the object level, but you still have to application developers can call to the specific aspects of APIs. 
  Apple apple = new Apple (); 
  LoggingAPI logging = (LoggingAPI) apple; 
  Apple.setLoggingLevel (VERBOSE); 
  Introduction to is a way, it will bind a new API to an existing object model. 

  Metadata metadata can be bundled with an additional category of information in the static or uptime.    Metadata is the more powerful force, you can bind dynamic metadata to a given subject examples.    Metadata is very powerful, when you really prepare for the general aspects of any object, and the development of logic need to know the information.    In the use of a good analogy is EJB metadata standard.    EJB release in the XML descriptors, you need to define a method based on the affairs of each attribute.    Application server guidance any time, any place, or to put up a panel because you BEAN configuration of XML metadata in the document has already defined as: Required, RequiresNew, Support, etc., bundled them in your EJB between management and services category. 
  C # metadata to become an integral part of the language.    XDoclet action is another example of metadata.    If you have been used XDoclet generation EJB descriptor document and publish, you will know the power of metadata.    In JDK1.5, when the metadata was added to java language, JCP agreed.    (See JSR175).    Despite a JSR175 until the facts, a good AOP framework should also provide a mechanism to define effective in the run-time type-class metadata. 

Pointcuts
  If interceptors, Introduction and metadata AOP is the characteristics of adhesive is then pointcuts.    Pointcuts tell AOP framework, those interceptors to bind those categories, what will be applied to the original data type or those that will be an introduction into those categories.    Pointcuts define the AOP will be characterized by how you used in the Application category. 

  Movements in the AOP 
  Example 1.    The use of interceptors 
  With a 4.0 JBoss AOP framework.    The JBoss application server framework and closely integrated, but you can in your application, run it separate.    Read until you see it in action, you will fully understand this concept, so let's use one from JBoss AOP examples to illustrate this module is how all the parts work together.    In the remaining part of this chapter, we will set up an example to track the use of AOP framework. 

  Definition of an interceptor to achieve our tracking framework, we must make the first thing is to define a interceptors, it will be practical work.    In JBOSS AOP, all of the interceptors must be achieved org.jboss.aop.Interceptor interface. 

  Public interface Interceptor 
  ( 
  Public String getName (); 
  Public InvocationResponse invoke (Invocation invocation) throws Throwable; 
  ) 

  In JBoss AOP, were intercepted all domains, and the constructor method was transferred into the general invoke call.    Method parameters are entered an Invocation object, and the return value of, or access to the domain structure was filled with a InvocationResponse object.    Invocation of this object also drives the interceptor chain.    In order to clearly illustrate this, let us look at, in this case, all of the objects to is how to meet together. 

  Import org.jboss.aop .*; 
  Import java.lang.reflect .*; 

  Public class TracingInterceptor implements Interceptor 
  ( 
  Public String getName () (return TracingInterceptor;) 
  Public InvocationResponse invoke (Invocation invocation) 
  Throws Throwable 
  ( 
  String message = null; 

  If (invocation.getType () == InvocationType.METHOD) 
  ( 
  Method method = MethodInvocation.getMethod (invocation); 
  Message = method: + method.getName (); 
  ) 
  Else if (invocation.getType () == InvocationType.CONSTRUCTOR) 
  ( 
  Constructor c = ConstructorInvocation.getConstructor (invocation); 
  Message = constructor: + c.toString (); 
  ) 
  Else 
  ( 
  / / Do nothing for fields. Just too verbose. 
  / / Do nothing for the domain.    Too cumbersome. 
  Return invocation.invokeNext (); 
  ) 

  System.out.println (Entering + message); 

  / / Continue on. Invoke the real method or constructor. 
  / /.    Calling the real method or constructor 
  InvocationResponse rsp = invocation.invokeNext (); 
  System.out.println (Leaving + message); 
  Return rsp; 
  ) 
  ) 

  The above interceptor will intercept all of a domain, or the constructor method call.    If the type of call is a method or constructor, with a signature method or constructor will be output to the source control platform. 
  Binding interceptors well, so that we define interceptors.    But how binding this interceptors to the actual class?    In order to do this, we need to define a pointcut.    For JBoss AOP, pointcuts in an XML file definition.    Let us look at what this looks like. 

  <? Xml version = "1.0" encoding = "UTF-8"> 





  Pointcut above the bundled TracingInterceptor to a class called POJO.    It looks a little trouble, we would like to have a follow-up for each class to create a pointcut?    Fortunately, the interceptor-pointcut attribute can be used any type of formal expressions.    So if you want to track by the JVM included in the category of expression will become .*.    If you just want to follow a particular package, then the expression would be com.acme.mypackge .*. 
  When running alone JBoss AOP, any META-INF/jboss-aop.xml model with the XML file JBoss AOP will be included in the running time.    If relevant path to be included in any JAR or directory of your CLASSPATH, that particular XML document will be activated, from JBoss AOP included in the running time. 

  Running this example we will use the pointcut defined above to run examples.    POJO class looks as follows: 

  Public class POJO 
  ( 
  Public POJO () () 
  Public void helloWorld () (System.out.println (Hello World!);) 
  Public static void main (String [] args) 
  ( 
  POJO pojo = new POJO (); 
  Pojo.helloWorld (); 
  ) 
  ) 

  TracingInterceptor will be intercepted on the main (), POJO (), and helloWorld () call.    Output appears as follows: 

  Entering method: main 
  Entering constructor: public POJO () 
  Leaving constructor: public POJO () 
  Entering method: helloWorld 
  Hello World! 
  Leaving method: helloWorld 
  Leaving method: main 

  Here you can download JBoss AOP and ion code.    Compiler and implementation: 

  $ Cd oreilly-aop/example1 
  $ Export CLASSPATH =.; jboss-common.jar; jboss-aop.jar; javassist.jar 
  $ Javac *. java 
  $ Java-Djava.system.class.loader = org.jboss.aop.standalone.SystemClassLoader POJO 

  JBoss AOP interceptors to bind to bytecode operation.    Because there is no compiler steps AOP ClassLoader running time must have total control.    If you are running on non-JBoss application server, you must use JBoss included in the development of a type of coverage included in the class-system. 
  TraceingInterceptor not tracking domain visit, because it is cumbersome.    For developers, realizing get () and set () method to package domain visit is a general practice.    If TracingInterceptor to filter out, and do not follow these methods, it is very good.    This example shows that you can use JBoss AOP metadata to achieve a method based on the filtering.    General, metadata for more complex issues, such as the definition of Service attributes, each method or lasting security role of mapping, but the examples should be sufficient to note how the metadata in AOP-enabled applications. 

  Definition of metadata to increase the filtering capabilities, we will provide a logo, you can use this to closure marked track.    We will return to our AOP XML files to define the labels, it will delete the get () and set () method of tracking.    In fact, the main () function of the tracking is meaningless, so we filter it out. 

  <? Xml version = "1.0" encoding = "UTF-8"> 



  True 


  True 

  The above definition of a set of XML called tracing attributes.    This is a filtering properties will be bundled with each set to get started or on the methods.    The regular expression format with JDK 1.4-definition of the expression.    Invocation by metadata objects in the TracingInterceptor the visit. 

  Onjava matrix open source technology authorized by the translation and publication. 
  If you have any views on this article or suggestions, please Matrix Forum your comments. 
  Specify: If the matrix interested in the translation of the article series, please click oreilly javaworld articles and details of the Translation Project Show You can also click - translation of the author's neo Show detailed information. 
  Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 
  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply