1.思维导图
2.代码部分
1 //1、定义装饰类(增强类)实现和被装饰类(被增强类)相同的接口 2 public class UserDaoWrapper implements UserDao { 3 4 //2、在装饰类中声明被装饰类的引用 5 private UserDao userDao ; 6 7 public UserDaoWrapper(){ 8 } 9 public UserDaoWrapper(UserDao userDao) { 10 this.userDao = userDao; 11 } 12 13 @Override 14 public void addUser() { 15 System.out.println("权限校验"); 16 userDao.addUser(); 17 System.out.println("日志记录"); 18 } 19 20 @Override 21 public void deleteUser() { 22 userDao.deleteUser(); 23 } 24 25 @Override 26 public void updateUser() { 27 userDao.updateUser(); 28 } 29 30 @Override 31 public void selectUser() { 32 33 userDao.selectUser(); 34 } 35 }
1 public static void main(String[] args) { 2 3 UserDao userDao = new UserDaoImpl();//被代理类对象 4 UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance( 5 userDao.getClass().getClassLoader(), 6 userDao.getClass().getInterfaces(), 7 new MyInvocationHandler(userDao)); 8 userDaoProxy.addUser(); 9 userDaoProxy.deleteUser(); 10 userDaoProxy.updateUser(); 11 userDaoProxy.selectUser(); 12 } 13 14 /** 15 * 增强代理类 16 */ 17 class MyInvocationHandler implements InvocationHandler{ 18 19 //声明一个被代理类的引用 20 private UserDao userDao; 21 22 public MyInvocationHandler(UserDao userDao) { 23 this.userDao = userDao; 24 } 25 26 @Override 27 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 28 // Method method : 被代理类的原方法 29 // Object[] args :被代理类的方法的实际参数 30 //执行被代理类中的原方法 31 String methodName = method.getName(); 32 Object returnValue = null; 33 if ("addUser".equals(methodName)) { 34 //只有addUser需要增强 35 System.out.println("权限校验"); 36 returnValue = method.invoke(userDao, args);//执行被代理类中的原方法 37 System.out.println("日志记录"); 38 } else { 39 //其他的方法不增强 40 method.invoke(userDao,args);//执行被代理类中的原方法 41 } 42 43 return returnValue; 44 } 45 }
-
增强deleteUser方法
1 public static void main(String[] args) { 2 UserDao userDao = new UserDaoImpl(); 3 //使用匿名内部类对象 4 UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance( 5 userDao.getClass().getClassLoader(), 6 userDao.getClass().getInterfaces(), 7 new InvocationHandler() {//处理增强 8 @Override 9 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 10 String methodName = method.getName(); 11 Object returnValue = null; 12 if ("deleteUser".equals(methodName)) { 13 System.out.println("权限校验"); 14 returnValue = method.invoke(userDao, args); 15 System.out.println("日志记录"); 16 } else { 17 returnValue = method.invoke(userDao, args); 18 } 19 return returnValue; 20 } 21 }); 22 userDaoProxy.deleteUser(); 23 24 userDaoProxy.addUser(); 25 }
1 public class MyDataSource { 2 3 int initPoolSize = 3;//连接池中初始连接数 4 LinkedList<Connection> connections = new LinkedList<>(); 5 6 public MyDataSource(){ 7 //当连接池初始化时,创建一些连接,并存储起来! 8 createConnections(); 9 } 10 11 public void createConnections(){ 12 for (int i = 1 ; i <= initPoolSize ; i++) { 13 try { 14 Connection connection = JDBCUtils.getConnection(); 15 //存储连接,要么数组,要么集合(单列、双列) 16 //单列集合:ArrayList、LinkedList、HashSet、TreeSet 17 //ArrayList:查询修改快,增删慢 18 //LinkedList:查询修改慢,增删快 19 //HashSet:去重 20 //TreeSet:排序 21 //到底是使用ArrayList还是LinkedList,取决于连接池的特性,移除连接,添加添加! 应该使用LinekdList 22 connections.add(connection); 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 29 /** 30 * 取出连接 31 * @return 32 */ 33 public Connection getConnection(){ 34 //连接池中的连接够用 35 36 if (connections.size() == 0 || connections.isEmpty()) { 37 //连接池中的连接不够用 , 新建一些连接存储连接池中 38 createConnections(); 39 } 40 Connection connection = connections.remove(0); 41 return connection; 42 } 43 44 /** 45 * 归还连接 46 * @param connection 47 */ 48 public void addBack(Connection connection){ 49 connections.add(connection); 50 } 51 }
1 public class ConnectionWrapper implements Connection { 2 3 //protected:只能在子父类进行访问 4 protected Connection connection; 5 6 public ConnectionWrapper() { 7 } 8 9 public ConnectionWrapper(Connection connection) { 10 this.connection = connection; 11 } 12 13 @Override 14 public Statement createStatement() throws SQLException { 15 return connection.createStatement(); 16 } 17 }
- 连接装饰类MyConnectionWrapper
1 public class MyConnectionWrapper extends ConnectionWrapper { 2 3 //声明被代理类的引用 4 private List<Connection> connectionList; 5 6 7 public MyConnectionWrapper(Connection connection, List<Connection> connectionList) { 8 super(connection); 9 this.connectionList = connectionList; 10 } 11 12 @Override 13 public void close() throws SQLException { 14 //增强close:归还连接 15 //把Connection规划给连接池 16 connectionList.add(connection); 17 } 18 }
1 public class MyDataSource { 2 3 int initPoolSize = 3; 4 LinkedList<Connection> connections = new LinkedList<>(); 5 6 public MyDataSource(){ 7 createConnections(); 8 } 9 10 public void createConnections(){ 11 for (int i = 1 ; i <= initPoolSize ; i++) { 12 try { 13 Connection connection = JDBCUtils.getConnection(); 14 connections.add(connection); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 } 19 } 20 21 /** 22 * 取出连接 23 * @return 24 */ 25 public Connection getConnection(){ 26 //连接池中的连接够用 27 if (connections.size() == 0 || connections.isEmpty()) { 28 //连接池中的连接不够用 , 新建一些连接存储连接池中 29 createConnections(); 30 } 31 Connection connection = connections.remove(0);//普通连接对象 32 //根据普通连接对象 获取 一个连接代理对象(增强连接对象) 33 Connection connectionProxy = (Connection) Proxy.newProxyInstance( 34 connection.getClass().getClassLoader(), 35 connection.getClass().getInterfaces(), 36 new InvocationHandler() { 37 @Override 38 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 39 //处理增强close方法 40 String methodName = method.getName(); 41 Object returnValue = null; 42 if ("close".equals(methodName)) { 43 //如果是close方法,归还连接 44 returnValue = connections.add(connection); 45 } else { 46 //如果不是close方法,就执行原有功能 47 returnValue = method.invoke(connection, args); 48 } 49 return returnValue; 50 } 51 }); 52 return connectionProxy; 53 } 54 }
Java内置注解
@Overirde
@Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
@Deprecated
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @interface Deprecated { }
@SuppressWarings
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { String[] value(); }