Common-pool源码编译——小例子

最近在研究apache里面所有的common的源代码,并且在源码基础上编写测试的例子
源码的下载地址 http://download.csdn.net/detail/m47838704/9511153

UserInfo.java

package mh.test;

public class UserInfo {

    private String name;
    private int age;

    public UserInfo() {
        // TODO Auto-generated constructor stub
    }
    public UserInfo(String name, int age) {
        // TODO Auto-generated constructor stub
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "用户名:"+this.name+",年龄:"+this.age;
    }
}

UserFactory.java

package mh.test;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

public class UserFactory implements PooledObjectFactory<UserInfo>{

    @Override
    public PooledObject<UserInfo> makeObject() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("创建一个新的对象");
        return new DefaultPooledObject<UserInfo>(new UserInfo());
    }

    @Override
    public void destroyObject(PooledObject<UserInfo> p) throws Exception {
        // TODO Auto-generated method stub

        UserInfo user = p.getObject();
        System.out.println("销毁对象"+user.toString());
        user = null;
    }

    @Override
    public boolean validateObject(PooledObject<UserInfo> p) {
        // TODO Auto-generated method stub
        if(p.getObject() instanceof UserInfo){
            System.out.println("是一个合法的对象");
            return true;
        }
        System.out.println("是一个非法的对象");
        return false;
    }

    @Override
    public void activateObject(PooledObject<UserInfo> p) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("重新初始化对象");
    }

    @Override
    public void passivateObject(PooledObject<UserInfo> p) throws Exception {
        // TODO Auto-generated method stub
        UserInfo user = p.getObject();
        System.out.println("对象已经被归还:"+user.toString());
    }

}

TestPool.java

package mh.test;

import org.apache.commons.pool2.impl.GenericObjectPool;

public class TestPool {

    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        GenericObjectPool<UserInfo> pool = new GenericObjectPool<UserInfo>(new UserFactory());
        try {
            UserInfo user = pool.borrowObject();
            user.setAge(10);
            user.setName("mh");
            System.out.println(user.toString());
            pool.returnObject(user);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

你可能感兴趣的:(apache,源代码,common-poo)