0

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

@Aspect
public class LoggingAspect {

    @Autowired
    public MessageSource messageSource

    @Pointcut("within(@Log *)")
    public void executionOfAnyMethodInLogAnnotationClass(){}

    @Before(value= "executionOfAnyMethodInLoggableAnnotationClass")
    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 or class annotated with @Log is called.

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

I have define bean in my Appconfig as below, to make spring to set dependencies to aspect and it works.

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

But, if a class is annotated with @Log which has argument constructor then spring, on server start throws BeanCreationException for the @Log annotated class

 @Log
 public class TypeBuilder {

     TypeBuilder(EnumType type, String value) { ... }

     public void build(){ ... }

 }

Exception:

Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mycompany.TypeBuilder]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.mycompany.TypeBuilder.<init>()
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1101)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
        at com.mycompany.Application.main(Application.java:22)   
user1300877
  • 169
  • 1
  • 3
  • 11
  • 1
    Add your `@Log` annotation. Although I highly doubt it is related to that annotation. How is the `TypeBuilder` being created? Is it component-scanned? If so you would need `@Autowired` on the constructor to make it work. – M. Deinum Jul 22 '15 at 13:12
  • Type builder is created through component-scan – user1300877 Jul 23 '15 at 08:42
  • Then you need `@Autowired` on your constructor else it will fail and that has nothing to do with a `@Log` annotation. – M. Deinum Jul 23 '15 at 08:44
  • Hi, Thanks for the response. That works for the spring managed beans with arguments constructor. But there are few classes which are not spring managed i.e they are instantiated using "new" keyword and if they are annotation @Log on class level, even then the same exception is thrown?. Not sure why spring is trying to create a instant of these classes which are included in component scan(as they are in scan package) and are not spring managed. can help on this please? – user1300877 Jul 24 '15 at 16:06
  • Spring will only instantiate an instance if it is a component, hence what is your `@Log` annotation look like. (Requested this earlier but you still haven't added it). – M. Deinum Jul 24 '15 at 16:46
  • Thank you to pointing at the problem. I had spring annotation in Log @interface. It worked after I removed. – user1300877 Jul 26 '15 at 12:31

0 Answers0