Hot questions for Using AspectJ in jboss

Question:

I have a maven Java EE 6 project and I have into every methods a Logger information to show in the console the beginning with parameters and the end too.

In some methods i forgot the make that so i want to use aspectJ to manage the beginning and the end to every called methods.

I use Jboss EAP6 as server and Jboss developper Studio as IDE and i found some tuturials on the net but always it talkes about spring or java aspactJ project. I installed the pluging aspectJ on my IDE and i try to add an aspect it told me that my maven project is not an aspectJ project so how can solve that?

this my maven pom.xml

 <dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.3</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>snapshots</id>
        <name>repo1-maven</name>
        <url>http://central.maven.org/maven2</url>
    </repository>
    <repository>
        <id>JBoss repository</id>
        <url>http://repository.jboss.org/nexus/content/groups/public/</url>
    </repository>
</repositories>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

this my interface:

public interface IClient {
    void addClient(ClientBean clt);
}

this is the implementation:

public class ClientImpl implements IClient {
    private List<ClientBean> ListOfCustomers;

    public ClientImpl() {
        setListOfCustomers(new ArrayList<ClientBean>());
    }

    public void addClient(ClientBean clt) {
        ListOfCustomers.add(clt);
    }

    public List<ClientBean> getListOfCustomers() {
        return ListOfCustomers;
    }

    public void setListOfCustomers(List<ClientBean> listOfCustomers) {
        ListOfCustomers = listOfCustomers;
    }
}

this is a class whitch i try to make my aspectJ:

@Aspect
public class ClientAspect {
    @Pointcut("execution(* *.*(..))")
    void anyCallMethod() {}

    @Before(value = "anyCallMethod()")
    public void befor(JoinPoint joinPoint) {
        System.out.println("Before, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }

    @After(value = "anyCallMethod()")
    public void after(JoinPoint joinPoint) {
        System.out.println("After, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }
}

i have a man class to test and when i run my project it gives me nos log on the console


Answer:

There are several issues with your POM:

  • [major] The AspectJ Maven Plugin will not be applied if you only configure it in the pluginManagement section, but do not actually use it in the actual plugins section.
  • [medium] You use an outdated version 1.4 of AspectJ Maven Plugin, which by default uses AspectJ compiler 1.6.11. I recommend you to use the current plugin version 1.7 which is based on AspectJ 1.8.2, but can be overridden to use the current version 1.8.4 in the plugin's dependencies sub-section, which I also recommend you to do because 1.8.4 contains several fixes in comparison to 1.8.2.
  • [minor] Your project's dependency on AspectJ Runtime should match the version used by the AspectJ Maven Plugin.
  • [minor] You do not need the project dependency on aspectjweaver which is only necessary for load-time weaving. For compile-time weaving you already use the plugin and during runtime then you only need aspectjrt.

Update:

Here is a sample config from one of my own projects (Java SE, no application server, but it should be equivalent):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.7</java.source-target.version>
    <aspectj.version>1.8.4</aspectj.version>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.source-target.version}</complianceLevel>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>

Question:

I'm working on a multimodule project composed by many WAR files. The projects use Spring 4.0.5 for managing dependencies and AspectJ 1.8.5 with load time weaving to support AOP (Spring basic AOP support is not enogh in this project). Also, there are an aop.xml in the META-INF directory with very simple configuration:

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

AOP is used mainly to provide various services and DAOs (anotated with @Service and @Repository) with Transactional behaviour (with spring's @Transactional annotation). For example:

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

Or an abstract Base DAO from which the rest of application's DAOs inherit

@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;
    }
  }
  ...
}

Finally the project uses maven for building and runs on JBoss EAP 6.1.

So with this configuration everything works fine when building and deploying WAR files. But if i build a simple EAR file only with one of those WAR files inside, load time weaving weaves all clases but one, the BaseDAO.

I've been struggling with this for many time and the only thing i can get is that on application boot, weaving works almost equal in both cases except that within the EAR, BaseDAO is not weaved. I can't figure out why. Any idea of why this is happening?

P.D.: Enabling AspectJ weaving log shows this when booting application from a WAR:

 ...
 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'
 ...

But when booting the EAR with this war inside, BaseDAO class is ignored completely:

 ...
 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'
 ...

Answer:

Finally we found where the problem was. It was in the jboss-deployment-structure.xml. Every war in the project had its own jboss-deployment-structure.xml with some exclusions:

<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>

But in the EAR deployment the EAR's specific jboss-deployment-structure.xml was missing so the classloader was working in a very diferent way, creating the issues that i post in my question. Simply putting the jboss-deployment-structure.xml (the EAR one, that is a combination of the WAR ones) in the EAR's META-INF solved the problem.

Hope this helps someone with similar problems.