Tutorials to .com

Tutorials to .com » Jsp » Skills » Study Notes

Study Notes

Print View , by: iSee ,Total views: 20 ,Word Count: 1000 ,Date: Wed, 26 Aug 2009 Time: 4:05 PM

Methods and Notes
Necessary to combine your java code and AOP combination, you can use some of the labels, like the new JDK5.0 version of the meta-data and XDoclet
. For your java method, somewhat like the use of synchronized on the same. When you approach your mark as a synchronized, an indication
You are a high number of JVM for your use of this method is needed in the implementation of synchronized. Note tag allows you to define new keywords used to process your own
Special behavior. AOP allows you to have on these woven into your applications, to enable applications to capacity for implementation.

Let's see if we use their own definitions of the labels @ Oneway to make a way in the background, the implementation of another thread.
import org.jboss.aspects.Oneway;

public class Foo
(
@ Oneway public static void someMethod () {...}

public static void main (String [] args)
(
someMethod (); / / executes in background
)
)

When our MAIN method calls Msomeethod method when starting it and the MAIN method of execution is parallel to one another synchronization method
.
To implement this class, we must first define their own tags to the JAVA syntax.
package org.jboss.aspects;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@ Target ((ElementType.METHOD))
public @ interface Oneway ()
That's enough. @ Target can limit the scope of application of your label. Here you can only be used for Oneway methods. Above these
Is pure inside J2SE5.0 applications.

The following is our connection, to execute @ Oneway behavior.

package org.jboss.aspects;

public OnewayAspect
(
private static class Task implements Runnable
(
private MethodInvocation invocation;

public Task (MethodInvocation invocation)
(
this.invocation = invocation;
)
public void run ()
(
try (invocation.invokeNext ();)
catch (Throwable ignore) ()
)
)


public Object oneway (MethodInvocation invocation) throws Throwable
(
MethodInvocation copy = invocation.copy ();
Thread t = new Thread (new Task (copy));
t.setDaemon (false);
t.start ();
return null;
)
)

Our aspect is that simple.
Finally we want to define an entry point

<aop>
<aspect class="org.jboss.aspects.OnewayAspect"/>

<bind pointcut="execution(void *-> @ org.jboss.Oneway (..))">
<advice name = "oneway"
aspect = "org.jboss.aspects.OnewayAspect" />
</ bind>
</ aop>

This definition shows that only a void to be marked as @ Oneway was only our OnewayAspect the implementation of the method. So that you can put your
Added to other applications, this is a simple, clear, easy JAVA predictions.

Field and comments
Adoption of AOP we can also define the meaning of our own fields, for example, we can define a ThreadLocal field, if the
Using the previous method, we have to use a lot of GET () and SET () can be achieved. Now we can directly define a
@ ThreadLocal type.

To use this new type can be like this:
import org.jboss.aspects.Threadbased;

public class Foo
(
@ Threadbased private int counter;
)

First, we want to define this type of
package org.jboss.aspects;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@ Target ((ElementType.FIELD))
public @ interface Threadbased ()

This can be, and now all the fields marked as @ Threadbased will be applied to come here, but here limit this type of
Only user field.

Here's what to do to achieve our specific work areas.

package org.jboss.aspects;

import org.jboss.aop.joinpoint .*;
import java.lang.reflect.Field;

public class ThreadbasedAspect
(
private ThreadLocal threadbased = new ThreadLocal ();

public Object access (FieldReadInvocation invocation)
throws Throwable
(
/ / Just in case we have a primitive,
/ / We can't return null
if (threadbased.get () == null)
return invocation.invokeNext ();
return threadbased.get ();
)

public Object access (FieldWriteInvocation invocation)
throws Throwable
(
threadbased.set (invocation.getValue ());
return null;
)
)
The final definition of entry points
<aop>
<aspect class = "org.jboss.aspects.ThreadbasedAspect"
scope = "PER_JOINPOINT" />
<bind pointcut="field(* *-> @ org.jboss.aspects.Threadbased) ">
<advice name = "access"
aspect = "org.jboss.aspects.ThreadbasedAspect" />
</ bind>
</ aop>

Our hope is to have carried out an instance of each field when making a Threadlocal static variables, so
Defined entry points when we use the PER_JOINPOINT, let us know that JBOSS AOP must be instantiated for each field
A new ThreadLocal, or JBOSS AOP will we instantiate a ThreadbasedAspect, if so
Make all of the fields share a Threadlocal, this is obviously not what we expected.

And the same as above, where we see a clean, easily integrated way to achieve the definition of a new type of JAVA.



Java skills and code Articles


Can't Find What You're Looking For?


Rating: 3.0

Comments

No comments posted.