boredPanda
boredPanda

Reputation: 19

spring Aspect annotation could not be resolved as a type

Hellom I'am trying to implement an aspect using spring annotation and AspectJ but I keep getting the error Aspect cannot be resolved to a type

here is my pom.xml :

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.11</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.11</version>
    </dependency>

and my logging class

package com.stock.mvc.aspect;

@Aspect
public class LoggingAspect {

  @before("execution(* * getRadius())")
  public void loggingAdvice(){
    System.out.println("call method before getRadius");
  }
}

Upvotes: 0

Views: 5479

Answers (5)

user25203283
user25203283

Reputation: 1

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

After removing the runtime scope in the pom.xml file. Then library will got recognized and @Aspect works fine now.

Upvotes: 0

Dhiraj Kumar
Dhiraj Kumar

Reputation: 1

the error was that @Aspect was not resolved to a type basically, the issue was that the dependencies were not being recognized.

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>

The issue was solved by removing the runtime scope in the pom.xml file.

runtime Then my library got recognized and @Aspect works fine now.

Upvotes: 0

Ramesan PP
Ramesan PP

Reputation: 164

For me the error was that @Aspect and @Before were not resolved to a type.

Also the org.aspectj library was not being called.

So basically,the issue was that the dependencies were not being recognized.

These are the only dependencies needed to implement the aspectJ library.

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <scope>runtime</scope>
</dependency>

The issue was solved by removing the runtime scope in the pom.xml file.

<scope>runtime</scope>

Then my library got recognized and @Aspect works fine now.

Upvotes: 5

xerx593
xerx593

Reputation: 13261

XXX could not be resolved to a type.

...sounds very much like a compiler error message, typical for missing import statement(s) ("organize imports" in your IDE "Source" menu):

package com.stock.mvc.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect public class ...

AND @Before is case-sensitive and written camel case.

Upvotes: 0

Ilya Lysenko
Ilya Lysenko

Reputation: 1892

Try to add these dependensies:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>

Upvotes: 0

Related Questions