设为首页 收藏本站
查看: 689|回复: 0

[经验分享] Apache

[复制链接]

尚未签到

发表于 2017-1-14 11:42:56 | 显示全部楼层 |阅读模式
  一. 什么是AOP?
  AOP 既是 面向切面编程.
  在两个功能模块中,插入一个AOP组件,而该组件与这两个模块间具有源码无关性,也就是一点也不耦合在这两个功能模块间,倘若其中一个功能模块调用了该组件中的某个方法,则该组件不是AOP组件.
  Spring 中运用 AOP 实现事务管理 就是一个很好的 例子.
  二. 用静态Proxy方式 通过AOP 模拟实现Spring中的事务管理
  UserDAO.java

public interface UserDAO {
public void save(User user);
}

  UserImpl.java

public class UserImpl implements UserDAO{
public void save(User user){
System.out.println("save user");
}
}
   UserDAOProxy.java

public class UserDAOProxy implements UserDAO{
private UserDAO _userDAO;
public UserDAOProxy(UserDAO userDAO){
_userDAO = userDAO;
}
public void save(User user){
UserTransaction t = null;
try{
t = (UserTransaction)(new InitialContext().lookup("java/tx") );
_userDAO.save(user);
t.commit();
}catch(Exception e){
if( t != null ){
try {
t.rollback();
} catch (Exception ex){
}
}
}
}
  Test Code

public void testProxyAOP(){
UserDAO userDAO = new UserDAOProxy( new UserImpl );
userDAO.save(); // 事务已经通过 UserDAOProxy 加载
}
  思想, 用 Proxy 类 UserDAOProxy.java 构造 一个AOP切面 实现一个绑定了事务的 UserDAO.
  1. 这个切面 与 UserImpl 代码无关.
  2. 通过这个切面我们给UserImpl绑定上了事物.
  静态Proxy实现的弊端 - 需要针对每个DAO实现一个DAOProxy. - impossible for the implementation of a large project
  
  三. 用动态Proxy 通过AOP 模拟实现Spring中的事务管理
  动态Proxy顾名思义就是自动实现DAOProxy的调用
  首先来看个例子,在User实体和操作实体之间做个切面,该切面的作用是判断该User是不是管理员,是则可以修改用户信息,若不是,则不能.
  User.java

public class User {
public static final int NORMAL_USER = 1;
public static final int ADMIN_USER = 2;
private int _userPrivilege = 0;
public User(int p){
checkUserPrivilege();
_userPrivilege = p;
}
public int getUserPrivilege(){
return _userPrivilege;
}
private void checkUserPrivilege() {
// TODO implementation.
}
}
   
  OperationDAO.java

public interface OperationDAO {
public void modifyCustomerInfo(User u);
}
  UserOperationDAOImpl.java

public class UserOperationDAOImpl implements OperationDAO {
// only Admin can modify user info
public void modifyCustomerInfo(User u) {
// TODO Auto-generated method stub
System.out.println("the user info modified ~~ ");
}
}

   UserOperationHandler.java (the dynamic proxy handler)

public class UserOperationHandler implements InvocationHandler {
private Object _source;
/**
* Returns an instance of a proxy class for the specified interfaces
* that dispatches method invocations to the specified invocation handler.
* @paramloader the class loader to define the proxy class
* @paraminterfaces the list of interfaces for the proxy class to implement
* @param   h the invocation handler to dispatch method invocations to
* @returna proxy instance with the specified invocation handler of a proxy class
* that is defined by the specified class loader and
* that implements the specified interfaces
*/
public Object bind(Object source){
_source = source;
return Proxy.newProxyInstance(_source.getClass().getClassLoader(),
_source.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
User u = (User)args[0];
if( u.getUserPrivilege() == User.ADMIN_USER ){
method.invoke(_source, args);
}else{
System.out.println("you have no privilege");
}
return null;
}
}
  UserOperationDAOImplTest.java

public class UserOperationDAOImplTest {
@Test
public void testAOPInvoke(){
UserOperationHandler handler = new UserOperationHandler();
OperationDAO dao = (OperationDAO) handler.bind( new UserOperationDAOImpl() );
User normalUser = new User( User.NORMAL_USER );
dao.modifyCustomerInfo(normalUser);
User adminUser = new User( User.ADMIN_USER );
dao.modifyCustomerInfo( adminUser );
}
}
  运行结果如下:

you have no privilege
the user info modified ~~
  这样,我们通过 AOP切面(UserOperationHandler ) 实现了User实体和Operation实体之间的简单的权限管理. 但需注意的是,UserOperationHandler 是与User实体,Operation实体间是独立的,无丝毫代码相关性.
  现在我们已经熟悉了什么是动态Proxy. 关键部分总结如下:
  1. return Proxy.newProxyInstance(_source.getClass().getClassLoader(), _source.getClass().getInterfaces(), this); 根据传入的“接口类型(_source.getClass().getInterfaces())”动态构造一个“代理类实例”返回, 该代理类就是JVM在内存中根据指定的“接口类型”动态构造的动态类,并实现了传入的所有接口. 这里看出, Dynamic要求所代理的类必须是某个接口的实现,否则则无法构造出相应的 动态类。(Spring 则是使用的动态Proxy管配置和管理事务,所以每个Service必须加上一个接口)
  2. UserOperationHandler#invoke() 方法将在被代理类(UserOperationDAOImpl)的方法之前触发. 这样我们就可以在“被代理类方法调用的前后”进行一些处理.
  再次, 修改静态 UserDAOProxy.java 使其成为 动态Proxy

public class UserDAOAOPProxy implements InvocationHandler {
private Object _source;
public Object bind(Object source) {
_source = source;
return Proxy.newProxyInstance(_source.getClass().getClassLoader(),
_source.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
Object result = null;
// added the transaction according with the action name.
if (!method.getName().startsWith("save")) {
UserTransaction tx = null;
try {
tx = (UserTransaction) (new InitialContext().lookup("java/tx"));
result = method.invoke( _source, args);
tx.commit();
} catch (Exception ex) {
if (null != tx) {
try {
tx.rollback();
} catch (Exception e) {
}
}
}
} else {
result = method.invoke( _source, args);
}
return result;
}
}
     这样,就实现了UserDAO的动态Proxy. 稍微需要注意的是,这里我通过Hard Coded把事务加在了“save" 方法上. 实际上我们可以通过配置文件导入,就像Spring那样,如下:  

<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
  这里涉及的只是简单的理解Spring如何通过AOP如何实现事务管理,而Spring的整个事务管理机制远比这个复杂,比如事务的PROPAGATION,传递之类的....
  
  
  四. CGLIB
      前面涉及的是如何通过接口动态实现AOP,而CGLIB正式通过实体类实现AOP
      个人感觉他的实现方式有异曲同工之妙,
           CGLIB是通过生成 目标类的 一个子类
           Dynamic Proxy是通过生成 目标类的 一个 Proxy代理类.
      两者的关系可以用下图表示
DSC0000.jpg
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-328329-1-1.html 上篇帖子: org.apache.jasper.JasperException: jsp.error.beans.property.conversion 下篇帖子: 关于org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/j
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表