Learn notes-mixin classes and multiple inheritance

Source: Internet
Author: User
Tags aop define implement inheritance interface jboss
Notes | Inherit Mixins and introductions
Many people say that AOP is an interceptor, pointcut expression, aspect and implementation (advices). Actually AOP
More than that, another important feature in JBoss AOP is the ability to transparently pass interfaces to Java classes. You
You can enforce an interface implemented by a class, and you can even attach another class (mixins) to implement this interface. Just like C + +
The multiple integrations inside.

Introduced
First we introduce how to force an existing class to implement the interface that you want it to implement.
The first example is the ability to persist a non-sustainable class. This will be a very useful code for you.

public class POJO
{
Private String field;
}

It's easy to make it persistent.
<introduction class= "POJO" >
<interfaces>java.io.Serializable</interfaces>
</introduction>

The XML definition above lets the AOP framework know that you want to apply the serializable interface to your class. You can be free here.
Add any interface to your class, just adding the interfaces element is all you need.
If your class does not implement an interface, the AOP framework will increase the way you implement it to your class. These methods are delegated to the AOP framework, but
Need to use interceptors or implementations for maintenance, otherwise nullpointerexception will be thrown.

Mixin It up
The introduction of the interface is only limited to you, when implementing the interface, if your class is not implemented, use the internal interceptor or implementation to implement. This
A place is where the mixin will be used. The AOP framework allows you to define the Mixin class to implement the interface you want to implement. Instantiation of this class
Will be the first time you call the method that introduces the interface.

We continue to use a externalizable class to implement the Java.io.Externalizable interface.

public class POJO
{
Private String field;
}

It's easy to make him realize externalizable.

<introduction class= "POJO" >
<mixin>
<interfaces>
Java.io.Externalizable
</interfaces>
<class>ExternalizableMixin</class>
<construction>new Externalizablemixin (This) </construction>
</mixin>
</introduction>

Same as above. Here only requires Pojo to implement an interface where you can implement many of your interfaces here.

The class element here defines the Mixin class that implements the interface Externalizable, Pojo to implement.
The construction element allows you to use Java code to define the method you want to use when instantiating the Mixin class. JBOSS AOP
Use a field to make the instantiated Pojo hold the handle of your implementation class Externalizablemixin. This field will use the
The code you provide in the construction is initialized. This refers to an instance of Pojo itself.

The final thing is that you have to implement the Mixin class.
public class Externalizablemixin implements Java.io.Externalizable
{
POJO POJO;

Public Externalizablemixin (POJO POJO)
{
This.pojo = Pojo;
}

public void Readexternal (ObjectInput in) throws IOException, ClassNotFoundException
{
POJO.STUFF2 = In.readutf ();
}
public void Writeexternal (ObjectOutput out) throws IOException
{
Out.writeutf (POJO.STUFF2);
}
}

Multiple Inheritance multiple inheritance
The point to note is that mixin enables multiple integrations within your Java interior. Although on the outside you don't need to directly
Mapping multiple integrations or declaring bindings in XML, but in fact it enables you to achieve the advantages of C + + over the years.
Although most experts are opposed to using multiple integrations, it is appropriate to use abstract mixin classes to implement multiple integrations
It's very helpful in many places for your design.




Related Article

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.