0

I am just writing a very basic Aspect @before function as I am new to AspectJ.

I have read spring documentation and experimenting with it in my example. But Aspect is not appearing to be woven with my code.

Here is my controller whose method I wish to advice. Please note it is a simple java method and not any request method.

public void printData()
{
    System.out.println("just to test aspect programming");
}

It is invoked from one of the request method in my web application.

@RequestMapping("/")    
public String doLogin(ModelMap modelMap)
{
    System.out.println("doLogin" +loginService);

    String message=messageSource.getMessage("message", null, "default", Locale.UK);
    System.out.println("Message is:" + message);
    printData();
    LoginForm loginForm=new LoginForm();
    modelMap.put("loginForm",loginForm);
    return "login";
}

My Aspect class is as follows:
package com.neha.javabrains;

import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Component
@Aspect
public class LogTime {

  @Before("execution(* com.neha.javabrains.LoginController.printData(..))")  
  public void loggedData()
  {
      System.out.println("In Pointcut Expression");
  } 
}

My Config class is as below:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- <bean/> definitions here -->
<context:component-scan base-package="com.neha.javabrains" />
<context:annotation-config/>  
<mvc:annotation-driven /> 
<mvc:default-servlet-handler />

<mvc:resources mapping="/resources/**" location="/resources/" />
<tx:annotation-driven transaction-manager="txManager"/>
<aop:aspectj-autoproxy />

    <bean id="loginDAO" class="com.neha.javabrains.LoginDAO">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">  
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
    </bean>

   <bean id="loginFormValidator" class="com.neha.javabrains.LoginFormValidator"/>

   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
   <property name="basename" value="message" />
   <property name="defaultEncoding" value="UTF-8" />
   </bean>

   <beans profile="dev">
  <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:books"></property>  
        <property name="username" value="system"></property>  
        <property name="password" value="xyz"></property>  
    </bean>  
    </beans>

    <beans profile="prod">
  <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>  
        <property name="username" value="system"></property>  
        <property name="password" value="abc"></property>  
    </bean>  
    </beans>

</beans>

When I run my app it is working fine but just ASpect Before method is not getting executed as its sysout is not being printed.

It seems as if Aspect is not even attached to my method.

Please help!!!!!!!!!!!

cb4
  • 6,689
  • 7
  • 45
  • 57
  • 1
    Spring AOP uses proxy, only calls INTO the object will be intercepted. You are doing an internal method call, isn't passing through the proxy and thus nothing will be intercepted. – M. Deinum Nov 25 '16 at 14:49
  • http://stackoverflow.com/a/13564700/1166537 – Raghav Nov 25 '16 at 14:49
  • It worked.Thanks a lot Guys.Now i will play around it. :) – Ankit Chaudhary Nov 25 '16 at 15:02
  • Before and after annotation worked but for the same around is not working. ` @Around("execution(* com.neha.javabrains.LoginController.doLogin(..))") public void loggedTime(ProceedingJoinPoint joinPoint) { Long startTime= System.currentTimeMillis(); try { joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); } Long endTime= System.currentTimeMillis(); Long elapsedTime=endTime-startTime; System.out.println("Total Execution Time"+ elapsedTime + "in" +joinPoint.getSignature().getName()); }` – Ankit Chaudhary Nov 25 '16 at 15:25

0 Answers0