时间:2021-07-01 10:21:17 帮助过:29人阅读
2.编写一个代理类,这里主要是实现一个InvocationHandler的接口
- <span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> cn.mycast.bank.db;
- </span><span style="color: #008080;"> 2</span>
- <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.lang.reflect.InvocationHandler;
- </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.lang.reflect.Method;
- </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.lang.reflect.Proxy;
- </span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.sql.Connection;
- </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> MyDatabaseHandler <span style="color: #0000ff;">implements</span><span style="color: #000000;"> InvocationHandler{
- </span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> Connection realConnection;
- </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> Connection WrapperConn;
- </span><span style="color: #008080;">10</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> MyDatabasePool databasesource;
- </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> Maxsize=10<span style="color: #000000;">;
- </span><span style="color: #008080;">12</span> <span style="color: #0000ff;">int</span> Currentsize=0<span style="color: #000000;">;
- </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> MyDatabaseHandler(MyDatabasePool databasesource){
- </span><span style="color: #008080;">14</span> <span style="color: #0000ff;">this</span>.databasesource=<span style="color: #000000;">databasesource;
- </span><span style="color: #008080;">15</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">16</span> <span style="color: #008000;">/*</span>
- <span style="color: #008080;">17</span> <span style="color: #008000;"> * Proxy类动态根据这里我们制定的接口生成一个class byte,然后通过classloader将class byte生成一个类对象,再将该对象委派给当前代理类的实例
- </span><span style="color: #008080;">18</span> <span style="color: #008000;">*/</span>
- <span style="color: #008080;">19</span> <span style="color: #000000;"> Connection bind(Connection realConn){
- </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">this</span>.realConnection=realConn;<span style="color: #008000;">//</span><span style="color: #008000;">需要代理的原始的对象</span>
- <span style="color: #008080;">21</span> <span style="color: #0000ff;">this</span>.WrapperConn=(Connection)Proxy.newProxyInstance(<span style="color: #0000ff;">this</span>.getClass().getClassLoader(), <span style="color: #0000ff;">new</span> Class[]{Connection.<span style="color: #0000ff;">class</span>}, <span style="color: #0000ff;">this</span><span style="color: #000000;">);
- </span><span style="color: #008080;">22</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> WrapperConn;
- </span><span style="color: #008080;">23</span> <span style="color: #008000;">//</span><span style="color: #008000;">这步是创建代理对象了,第一个参数指定类加载器:使得这个代理类能访问被代理对象,
- </span><span style="color: #008080;">24</span> <span style="color: #008000;">//</span><span style="color: #008000;">第二个参数指定代理类应该具有哪些接口:要不然怎么去代理被代理对象,
- </span><span style="color: #008080;">25</span> <span style="color: #008000;">//</span><span style="color: #008000;">第二个参数就是指定,代理对象额外完成的业务逻辑了:如前面所述的获取被代理类的执行时间。</span>
- <span style="color: #008080;">26</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">27</span> <span style="color: #000000;"> @Override
- </span><span style="color: #008080;">28</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> Object invoke(Object proxy, Method method, Object[] args)
- </span><span style="color: #008080;">29</span> <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Throwable {
- </span><span style="color: #008080;">30</span> <span style="color: #0000ff;">if</span>("close".equals(method.getName())){<span style="color: #008000;">//</span><span style="color: #008000;">这里代理原始类的close方法</span>
- <span style="color: #008080;">31</span> <span style="color: #0000ff;">this</span>.Currentsize++<span style="color: #000000;">;
- </span><span style="color: #008080;">32</span> <span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">this</span>.Currentsize<<span style="color: #0000ff;">this</span><span style="color: #000000;">.Maxsize){
- </span><span style="color: #008080;">33</span> <span style="color: #0000ff;">this</span>.databasesource.connPool.addLast(<span style="color: #0000ff;">this</span><span style="color: #000000;">.WrapperConn);
- </span><span style="color: #008080;">34</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
- </span><span style="color: #008080;">35</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.realConnection.close();
- </span><span style="color: #008080;">36</span> <span style="color: #0000ff;">this</span>.databasesource.Currentsize--<span style="color: #000000;">;
- </span><span style="color: #008080;">37</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">38</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">39</span> <span style="color: #0000ff;">return</span> method.invoke(<span style="color: #0000ff;">this</span><span style="color: #000000;">.realConnection, args);
- </span><span style="color: #008080;">40</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">41</span>
- <span style="color: #008080;">42</span> }
3.编写一个测试类
- <span style="color: #008080;"> 1</span> <span style="color: #0000ff;">package</span><span style="color: #000000;"> cn.mycast.bank.db;
- </span><span style="color: #008080;"> 2</span>
- <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.sql.Connection;
- </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> java.sql.SQLException;
- </span><span style="color: #008080;"> 5</span>
- <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Main {
- </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">/**</span>
- <span style="color: #008080;"> 8</span> <span style="color: #008000;"> * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> args
- </span><span style="color: #008080;"> 9</span> <span style="color: #008000;"> * </span><span style="color: #808080;">@throws</span><span style="color: #008000;"> SQLException
- </span><span style="color: #008080;">10</span> <span style="color: #008000;">*/</span>
- <span style="color: #008080;">11</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
- </span><span style="color: #008080;">12</span> System.out.println("创建连接.........."<span style="color: #000000;">);
- </span><span style="color: #008080;">13</span> MyDatabasePool databasepool=<span style="color: #0000ff;">new</span><span style="color: #000000;"> MyDatabasePool();
- </span><span style="color: #008080;">14</span>
- <span style="color: #008080;">15</span> System.out.println("客户端连接.........."<span style="color: #000000;">);
- </span><span style="color: #008080;">16</span> Connection conn=<span style="color: #0000ff;">null</span><span style="color: #000000;">;
- </span><span style="color: #008080;">17</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
- </span><span style="color: #008080;">18</span> conn=<span style="color: #000000;">databasepool.GetConnection();
- </span><span style="color: #008080;">19</span> System.out.println("連接池中的連接個數"+<span style="color: #000000;">databasepool.connPool.size());
- </span><span style="color: #008080;">20</span> } <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException e) {
- </span><span style="color: #008080;">21</span> <span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span>
- <span style="color: #008080;">22</span> <span style="color: #000000;"> e.printStackTrace();
- </span><span style="color: #008080;">23</span> }<span style="color: #0000ff;">finally</span><span style="color: #000000;">{
- </span><span style="color: #008080;">24</span> <span style="color: #0000ff;">if</span>(conn!=<span style="color: #0000ff;">null</span><span style="color: #000000;">){
- </span><span style="color: #008080;">25</span> <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
- </span><span style="color: #008080;">26</span> <span style="color: #000000;"> conn.close();
- </span><span style="color: #008080;">27</span> System.out.println("客户端關閉連接.........."<span style="color: #000000;">);
- </span><span style="color: #008080;">28</span> } <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException e) {
- </span><span style="color: #008080;">29</span> <span style="color: #000000;"> e.printStackTrace();
- </span><span style="color: #008080;">30</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">31</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">32</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">33</span> System.out.println("連接池中的連接個數"+<span style="color: #000000;">databasepool.connPool.size());
- </span><span style="color: #008080;">34</span> <span style="color: #000000;"> }
- </span><span style="color: #008080;">35</span>
- <span style="color: #008080;">36</span> }
创建连接..........
客户端连接..........
連接池中的連接個數5
客户端關閉連接..........
連接池中的連接個數6
动态代理工作原理图如下:
动态代理就是对被代理的类进行一次封装包裹,以便加上我们自己需要加的一些业务逻辑。
java之使用动态代理实现数据库连接的回收
标签: