Search code examples
javaspringhibernateaopaspectj

Sometimes classes not get woven during load time weaving


I have some classes marked with @Configurable annotation with @Autowired fields and some of them works fine but other throw NullPointerException due to fields not getting @Autowired correctly.

My configuration is like this

@Configuration
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@EnableAsync(mode = AdviceMode.PROXY, proxyTargetClass = true)
@Import({
    ...
})
@ComponentScan(
    basePackageClasses = {
        ...
    }
)
public class RootConfig implements AsyncConfigurer {

aop.xml:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="aero.geoscan..*"/>
    </weaver>
</aspectj>

Solution

  • Spring may load weaver bean lately and thus not process all neccessary classes. Try to add @DependsOn("loadTimeWeaver") to your @Configuration.

    And if it doesn't help add <weaver options="-verbose -debug -showWeaveInfo"> to your aop.xml. It will help to resolve problem by providing additional logs