Use cglib to easily implement lazyload)

Source: Internet
Author: User
In easydbo, an ultra-lightweight open-source ORM system, you can set attributes to delayed loading based on actual conditions, which greatly improves application performance. For example, if a domain object contains an order attribute, you can set it as follows: <one-to-one name = "order" type = "com. easyjf. DBO. example. order "column =" orderid "Key =" CID "lazy =" true ">
</One-to-one> latency loading in easydbo is implemented using cglib. Let's take a look at the implementation principles of latency loading: 1. Design a latency loader (callback ), implement the lazyloader interface of cglib. The interface has a loadobject method to actually load objects. 2. When creating the domain object dom (PO) for the first time, use the Create method of cglib's enhancer to create an attribute object that requires delayed loading, in this case, the above latency loader must be used as the parameter. 3. When a user accesses a delayed loading object for the first time, the user will call the loadobject method in the delayed loader to actually load the object. The following is a very simple example. We can see from the background that when we use new TB () and then access its CID attribute, the delayed loading bean has not yet been created, the testbean object will be created and initialized only when you access its TB bean properties: Beantest. JavaPublic class beantest extends testcase {
 
Public void testbean ()
{
TB = new TB ();
System. Out. println (Tb. getcid ());
System. Out. println ("--");
System. Out. println (Tb. getbean (). GetUserName ());
} Class lazy implements net. SF. cglib. Proxy. lazyloader
{
Public object loadobject () throws exception {
// Todo auto-generated method stub
System. Out. println ("start to delay loading! ");
Testbean bean = new testbean ();
Bean. setusername ("test ");
Return bean;
}
} Class TB {
Private string CID;
Private testbean bean;
Lazyloader lazy = new lazy ();
Public Tb ()
{
Cid = "1245454 ";
Bean = (testbean) net. SF. cglib. Proxy. enhancer. Create (testbean. Class, lazy );
}
Public testbean getbean (){
Return bean;
}
Public void setbean (testbean bean ){
This. Bean = bean;
}
Public String getcid (){
Return CID;
}
Public void setcid (string CID ){
This. cid = CID;
}
}} Testbean. JavaPublic class testbean {private string username;
Private integer sequence;
Public integer getsequence (){
Return sequence;
}
Public void setsequence (integer sequence ){
This. Sequence = sequence;
}
Public String GetUserName (){
Return username;
}
Public void setusername (string username ){
This. Username = username;
}
Public void test (){
// Todo auto-generated method stub

}
}

 

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.