【问题标题】:AspectJ load time Weaving not working properly within EAR deployed classesAspectJ 加载时间编织在 EAR 部署的类中无法正常工作
【发布时间】:2016-05-04 17:04:45
【问题描述】:

我正在开发一个由许多 WAR 文件组成的多模块项目。这些项目使用 Spring 4.0.5 来管理依赖关系,并使用 AspectJ 1.8.5 和加载时间编织来支持 AOP(Spring 基本 AOP 支持在这个项目中是不够的)。另外,在META-INF目录下有一个aop.xml,配置非常简单:

<aspectj>
  <aspects>
   <!--No need to specify any aspect here-->
  </aspects>
  <weaver options="-XnoInline -verbose">
    <include within="com.myproject..*" />   
  </weaver>
</aspectj>

AOP 主要用于提供各种带有事务行为的服务和DAO(用@Service 和@Repository 注释)(用spring 的@Transactional 注释)。例如:

@Service(value = "alertService")
public class AlertServiceImpl implements IAlertService, Serializable {
  ...
  @Transactional(rollbackFor = Exception.class)
  @Override
  public Alert createAlert(Alert alert) {
    alertDAO.create(alert);
    return alert;
  }
  ...
}

或者是一个抽象的 Base DAO,应用程序的其他 DAO 都继承自该 DAO

@Repository
public abstract class BaseDAO<E extends IBusinessObject, PK extends Serializable> {
  ...
  @Transactional(readOnly = true)
  public <C extends E> C findFirstByFields(Class<C> type, String[] fields, Object[] values, String[] dependencies)
      throws ModelException {
    List<C> list = findAllByFields(type, fields, values, null, null, 1, null);
    if (list.isEmpty()) {
      return null;
    } else {
      C obj = list.get(0);
      loadDependencies(obj, dependencies);
      return obj;
    }
  }
  ...
}

最终项目使用 maven 构建并在 JBoss EAP 6.1 上运行。

因此,使用此配置,在构建和部署 WAR 文件时一切正常。但是,如果我只在其中一个 WAR 文件中构建一个简单的 EAR 文件,加载时编织会编织除一个 BaseDAO 之外的所有类。

我已经为此苦苦挣扎了很多次,我唯一能得到的是,在应用程序启动时,编织在两种情况下几乎相同,除了在 EAR 中,BaseDAO 没有被编织。我不知道为什么。知道为什么会这样吗?

P.D.:从 WAR 启动应用程序时,启用 AspectJ 编织日志会显示此内容:

 ...
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure1'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure3'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure5'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.subproject.services.Repository$AjcClosure1'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.model.dao.BaseDAO$AjcClosure1'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.model.dao.BaseDAO$AjcClosure3'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.model.dao.BaseDAO$AjcClosure5'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.model.dao.BaseDAO$AjcClosure7'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.subproject.model.dao.LastDocumentNumberDAO$AjcClosure1'
 ...

但是当启动 EAR 里面有这个战争时,BaseDAO 类被完全忽略:

 ...
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure1'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure3'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.commons.services.CommonService$AjcClosure5'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.subproject.services.Repository$AjcClosure1'
 ServerService Thread Pool -- 227 ? [ModuleClassLoader@1b3a9e75] debug generating class 'com.myproject.subproject.model.dao.LastDocumentNumberDAO$AjcClosure1'
 ...

【问题讨论】:

    标签: java jboss aop aspectj spring-aop


    【解决方案1】:

    我们终于找到了问题所在。它在 jboss-deployment-structure.xml 中。 项目中的每场战争都有自己的 jboss-deployment-structure.xml,但有一些例外:

    <jboss-deployment-structure>
      <deployment>
        <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
        <exclusions>
          <module name="org.apache.log4j" />
          <module name="org.slf4j" />
          <module name="org.apache.commons.logging" />
          <module name="org.log4j" />
          <module name="org.jboss.logging" />
          <module name="org.javassist" />
          <module name="org.hibernate.validator" />
          <module name="org.jboss.ws.cxf" />
        </exclusions>
        <exclude-subsystems>
          <subsystem name="webservices" />
        </exclude-subsystems>
      </deployment>
    </jboss-deployment-structure>
    

    但在 EAR 部署中,缺少 EAR 的特定 jboss-deployment-structure.xml,因此类加载器以非常不同的方式工作,从而产生了我在问题中发布的问题。只需将 jboss-deployment-structure.xml(EAR 之一,即 WAR 的组合)放在 EAR 的 META-INF 中即可解决问题。

    希望这对有类似问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多