我在 Maven 项目中有一个方面类: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())
});
}
}
我已经使用 aspectj-maven-plugin 在 maven 中进行编译时编织,如下所示
<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 按预期工作,并且每当调用带有注释的方法或类时都会调用建议@Log
。
我正在使用 spring boot 应用程序并且没有使用 xml。
我在我的 Appconfig 中定义了 bean,如下所示,以使 spring 将依赖项设置为方面并且它可以工作。
@Bean()
public LoggingAspect loggingAspect() {
LoggingAspect loggerAspect = Aspects.aspectOf(LoggingAspect.class);
loggerAspect.messageSource = messageSource;//Autowired in same class
return loggerAspect;
}
但是,如果一个带有@Log
参数构造函数的类被注释,那么spring,在服务器启动时为带@Log
注释的类抛出 BeanCreationException
@Log
public class TypeBuilder {
TypeBuilder(EnumType type, String value) { ... }
public void build(){ ... }
}
例外:
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)