Green Root
Green Root

Reputation: 694

AspectJ: @AfterReturning methods not calling

I have a problem with execution of AspectJ pointcuts. My aspect is:

@Component
@Aspect
public class UploadToDefaultAspect {
private static Logger logger = Logger.getLogger(UploadToDefaultAspect.class);
private Sardine sardine;
private String webCloudDataDir;

@Autowired private ConfigurationFactory configuration;
@Autowired private GeoProcessorDAO geoService;

@PostConstruct
private void init() {
    webCloudDataDir = configuration.getConfigurationValue(DEFAULT_CLOUD_LOCATION) + DIR_CLOUD_DATA;
}

@AfterReturning(
        pointcut = "execution(* web.service.SystemService.uploadGeoTZCSV(..))", 
        returning = "country")
    public void uploadAfterReturning( Country country ) throws CloudException {
    // upload stuff
    }
}

web.service.SystemService is an interface with several methods including uploadGeoTZCSV:

Country uploadGeoTZCSV( MultipartFile geoTZFile ) throws CSVUploadException;

When I debug uploadGeoTZCSV it works but aspect's method is not calling at all (it is managed by Spring and I see invocation of init() in the logs). Also I have aop configured in the applicationContext.xml:

<aop:aspectj-autoproxy proxy-target-class="true"/>

Spring 4.2.3.RELEASE, AspectJ 1.8.7:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>

Where I am wrong?

Thank you.

UPDATE 1. It also does not work when I move AOP configuration into the XML:

    <aop:aspectj-autoproxy/>

<!-- AOP beans -->
<bean id="uploadToDefaultAspect" class="web.aop.UploadToDefaultAspect" init-method="init"/>
<aop:config>
    <aop:aspect id="uploadToDefaultAspect" ref="uploadToDefaultAspect">
        <!-- @AfterReturning -->
        <aop:pointcut id="pointCutAfterReturning"
            expression="execution(* web.service.SystemService.uploadGeoTZCSV(..))"/>
         <aop:after-returning method="uploadGeoTZCSVAfterReturning" returning="country" 
            pointcut-ref="pointCutAfterReturning"/>
    </aop:aspect>
</aop:config>

What is going on?

Upvotes: 1

Views: 1362

Answers (1)

Green Root
Green Root

Reputation: 694

Finally, I found a solution - just add @EnableAspectJAutoProxy to the implementation of web.service.SystemService:

@Service
@EnableAspectJAutoProxy
public class SystemServiceImpl implements SystemService {

Upvotes: 2

Related Questions