1

I have a aspect class in maven project : my-aspect-project

@Aspect
public class LoggingAspect {

@Autowired
public MessageSource messageSource

@Pointcut("execution(@Log * *(..))")
public void executionOfLogAnnotationMethod(){}

@Before(value= "executionOfLoggableAnnotationMethod")
public void logBefore(JointPoint jp){
Logger.log(Level.info,messageSource.getMessage("before.log.message"),new String[] {
jp.getTarget().getClass().getSimpleName(),
jp.getSignature().getName(), 
Arrays.deepToString(jp.getArgs())
});

}

I have used aspectj-maven-plugin for compile time weaving in maven as below

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin-version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<configuration>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showWeaveInfo>true</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<verbose>true</verbose>
<Xlint>warning</Xlint>
<aspectLibraries>
<aspectLibrary>
<groupId>com.mycompany-myproject</groupId>
<artifactId>my-aspect-project</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</execution>
</executions>
</plugin>

Aspect works as expected and advice is invoked whenever a method annotated with @Log is called, but messageSource autowired in aspect is null as result, the properties file is not read and log message is null.

I am using spring boot application and no xml is used.

I read this [http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-aj-configure] and I not sure how this would work in my case,

I tried to define bean in my Appconfig as below,

@Bean()
public LoggingAspect loggingAspect() {
    LoggingAspect loggerAspect =  Aspects.aspectOf(LoggingAspect.class);
    loggerAspect.messageSource = messageSource;//Autowired in same class
    return loggerAspect;
}

This is also not working. I have also added a debug point in bean method loggingAspect() which is not invoked at all.

So Please let me know,

1)How to make spring annotation to work with my aspect class annotated with @Aspect?

2)I also want to use spring profile to enable or disable aspects and as spring annotation are not working in my aspect, I am not sure how to make this work?

Thanks in Advance

user1300877
  • 169
  • 1
  • 3
  • 11

0 Answers0