Results 1 to 7 of 7

Thread: Aspect not being triggered

  1. #1

    Default Aspect not being triggered

    Ok, I've been working on this Spring AOP stuff for four days now and am having no luck. Everything seems to be configured correctly and I get no errors so I'm at a loss. I've been working with the examples from Spring in Action 2nd Ed. Pretty much the aspect is to be ran after each of my handleRequestInternal() on my controllers that extend AbstractController.

    I am using Spring 2.5 and have the Spring AOP 2.5 via Maven. Here is what I have. Any ideas on what I am doing wrong here would be awesome.

    I'll leave out the controller class that is to be advised since its a standard AbstractServlet class that only has the handleRequestInternal(HttpServletRequest request, HttpServletResponse response)

    advice class
    Code:
    public class HibernateSessionAdvice {
    	
    	public void clearSession() {
    		HibernateSessionFactory.getSession().clear();
    		
    		System.out.println("************* clearSession() ran *****************");
    	}
    
    }
    advisor class
    Code:
    public class HibernateSessionAdvisor implements AfterReturningAdvice {
    	
    	HibernateSessionAdvice manager = new HibernateSessionAdvice();
    
    	@Override
    	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
    			Object arg3) throws Throwable {
    	
    		manager.clearSession();
    		
    	}
    	
    	public HibernateSessionAdvice getManager() {
    		return manager;
    	}
    
    	public void setManager(HibernateSessionAdvice manager) {
    		this.manager = manager;
    	}
    }
    springapp-servlet.xml
    Code:
    <!-- AOP Mapping -->
    	
    	   <!-- Advice classes -->
    	   <bean id="sessionAdvice" class="com.hks.advice.HibernateSessionAdvice"/>
    	   
    	   <!-- Advisor classes -->
    	   <bean id="sessionAfterAdvice" class="com.hks.advisors.HibernateSessionAdvisor">
    	   		<property name="manager" ref="sessionAdvice"></property>
    	   </bean>
    	   
    	   <!-- Pointcut beans -->
    	   <bean id="whatToDoPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
    	   		<property name="pattern" value=".*handleRequestInternal" />
    	   </bean>
    	   
    	   <bean id="sessionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
    	   		<property name="advice" ref="sessionAfterAdvice" />
    	   		<property name="pointcut" ref="whatToDoPointcut"></property>
    	   </bean>

  2. #2

    Default

    OK so Ive tried about every example of AOP type example in the book. Spring AOP, AspectJ and finally "pure-POJO" aspects. Still a no go.

    The pure-pojo issue is the same as the spring aop. Aspect method never gets triggered.

    The spring aop example i already have above. The AspectJ issue that I think Im having is that it tries to auto wire all my beans (i think?).

    Code:
    SEVERE: StandardWrapper.Throwable
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource' defined in ServletContext resource [/WEB-INF/ottrweb-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Must set property 'expression' before attempting to match
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    	at org.springframework...
    The only bean with annotations is the HibernateSessionAdvice class
    Code:
    <bean id="sessionAdvice" class="com.hks.advice.HibernateSessionAdvice"/>
    	   
    	   <aop:aspectj-autoproxy />
    	   
    	    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            	<property name="basename" value="messages"/>
        	</bean>
    Code:
    @Aspect
    public class HibernateSessionAdvice {
    	
    	@Pointcut("execution(* *.handleRequestInternal(..))")
    	public void somethingToDo() {}
    	
    	@AfterReturning
    	public void clearSession() {
    		HibernateSessionFactory.getSession().clear();
    		
    		System.out.println("************* clearSession() ran *****************");
    	}
    
    }

  3. #3

    Default

    I'm starting to wonder if there is a dependency issue that I am having with maven. Here is my pom. Sorry for all the reading. I cant believe that this is so complicated.

    Code:
      <dependencies>
        <dependency>
          <groupId>org.apache.openejb</groupId>
          <artifactId>javaee-api</artifactId>
          <version>5.0-1</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-core</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-beans</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-context</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>commons-attributes</groupId>
        	<artifactId>commons-attributes-api</artifactId>
        	<version>2.1</version>
        </dependency>
        <dependency>
        	<groupId>commons-attributes</groupId>
        	<artifactId>commons-attributes-compiler</artifactId>
        	<version>2.1</version>
        </dependency>
        <dependency>
        	<groupId>log4j</groupId>
        	<artifactId>log4j</artifactId>
        	<version>1.2.14</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-aop</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-agent</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-tomcat-weaver</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>asm</groupId>
        	<artifactId>asm</artifactId>
        	<version>2.2.3</version>
        </dependency>
        <dependency>
        	<groupId>aopalliance</groupId>
        	<artifactId>aopalliance</artifactId>
        	<version>1.0</version>
        </dependency>
        <dependency>
        	<groupId>oro</groupId>
        	<artifactId>oro</artifactId>
        	<version>2.0.8</version>
        </dependency>
        <dependency>
        	<groupId>asm</groupId>
        	<artifactId>asm-commons</artifactId>
        	<version>2.2.3</version>
        </dependency>
        <dependency>
        	<groupId>asm</groupId>
        	<artifactId>asm-util</artifactId>
        	<version>2.2.3</version>
        </dependency>
        <dependency>
        	<groupId>org.aspectj</groupId>
        	<artifactId>aspectjrt</artifactId>
        	<version>1.6.2</version>
        </dependency>
        <dependency>
        	<groupId>org.aspectj</groupId>
        	<artifactId>aspectjweaver</artifactId>
        	<version>1.6.2</version>
        </dependency>
        <dependency>
        	<groupId>org.aspectj</groupId>
        	<artifactId>aspectjlib</artifactId>
        	<version>1.6.2</version>
        </dependency>
        <dependency>
        	<groupId>cglib</groupId>
        	<artifactId>cglib-nodep</artifactId>
        	<version>2.1_3</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-tx</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-orm</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-jdbc</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>javax.persistence</groupId>
        	<artifactId>persistence-api</artifactId>
        	<version>1.0</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-web</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-webmvc</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-webmvc-struts</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-webmvc-portlet</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>struts</groupId>
        	<artifactId>struts</artifactId>
        	<version>1.2.9</version>
        </dependency>
        <dependency>
        	<groupId>commons-httpclient</groupId>
        	<artifactId>commons-httpclient</artifactId>
        	<version>3.0.1</version>
        </dependency>
        <dependency>
        	<groupId>org.freemarker</groupId>
        	<artifactId>freemarker</artifactId>
        	<version>2.3.14</version>
        </dependency>
        <dependency>
        	<groupId>jasperreports</groupId>
        	<artifactId>jasperreports</artifactId>
        	<version>2.0.5</version>
        </dependency>
        <dependency>
        	<groupId>commons-io</groupId>
        	<artifactId>commons-io</artifactId>
        	<version>1.3.1</version>
        </dependency>
        <dependency>
        	<groupId>portlet-api</groupId>
        	<artifactId>portlet-api</artifactId>
        	<version>1.0</version>
        </dependency>
        <dependency>
        	<groupId>poi</groupId>
        	<artifactId>poi</artifactId>
        	<version>2.5.1</version>
        </dependency>
        <dependency>
        	<groupId>com.servlets</groupId>
        	<artifactId>cos</artifactId>
        	<version>09May2002</version>
        </dependency>
        <dependency>
        	<groupId>jexcelapi</groupId>
        	<artifactId>jxl</artifactId>
        	<version>2.6</version>
        </dependency>
        <dependency>
        	<groupId>velocity</groupId>
        	<artifactId>velocity</artifactId>
        	<version>1.5</version>
        </dependency>
        <dependency>
        	<groupId>velocity-tools</groupId>
        	<artifactId>velocity-tools</artifactId>
        	<version>1.2</version>
        </dependency>
        <dependency>
        	<groupId>com.lowagie</groupId>
        	<artifactId>itext</artifactId>
        	<version>2.1.3</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.poi</groupId>
        	<artifactId>poi</artifactId>
        	<version>3.0.1-FINAL</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework.webflow</groupId>
        	<artifactId>org.springframework.webflow</artifactId>
        	<version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework.webflow</groupId>
        	<artifactId>org.springframework.binding</artifactId>
        	<version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework.webflow</groupId>
        	<artifactId>org.springframework.js</artifactId>
        	<version>2.0.5.RELEASE</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-jms</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.hibernate</groupId>
        	<artifactId>hibernate</artifactId>
        	<version>3.2.5.ga</version>
        </dependency>
        <dependency>
        	<groupId>javax.transaction</groupId>
        	<artifactId>jta</artifactId>
        	<version>1.1</version>
        </dependency>
        <dependency>
        	<groupId>org.directwebremoting</groupId>
        	<artifactId>dwr</artifactId>
        	<version>2.0.3</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.tiles</groupId>
        	<artifactId>tiles-api</artifactId>
        	<version>2.0.7</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.tiles</groupId>
        	<artifactId>tiles-core</artifactId>
        	<version>2.0.7</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.tiles</groupId>
        	<artifactId>tiles-jsp</artifactId>
        	<version>2.0.7</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.tiles</groupId>
        	<artifactId>tiles-servlet</artifactId>
        	<version>2.1.0</version>
        	<classifier>j4</classifier>
        </dependency>
        <dependency>
        	<groupId>commons-collections</groupId>
        	<artifactId>commons-collections</artifactId>
        	<version>3.2.1</version>
        </dependency>
        <dependency>
        	<groupId>org.springframework</groupId>
        	<artifactId>spring-aspects</artifactId>
        	<version>2.5.6</version>
        </dependency>
        <dependency>
        	<groupId>org.aspectj</groupId>
        	<artifactId>aspectjtools</artifactId>
        	<version>1.6.2</version>
        </dependency>
      </dependencies>

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    I suggest chapter 6.6.1 of the reference guide.

    Spring uses proxies and as a result can only intercept EXTERNAL method calls. You are trying to advice an INTERNAL method call. (The handleRequestInternal is called from the handleRequest method).

    Next to that I suggest changing the code and inject a session factory instead of using an ugly static accessor method.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    OK so I get what your saying. In order for a method to be advised by Spring AOP the method needs to be called from an external class. That explains why the advice on the handleRequestInternal() wasn't being triggered. But that's not the only method that I can't get things to work for. I have also set it to trigger on one of my business delegate methods that is called EXTERNALLY from my controller class.

    The controller class and bd class are both defined beans on my mvc-servlet.xml.

    Code:
    <!-- BMT Beans -->
    	<bean name="/bmt/agnisForms.htm" class="com.hks.web.controllers.AgnisFormController" />
    	<bean name="agnisFormBD" class="com.hks.web.business.delegates.AgnisFormsBusinessDelegate" />
    	
    	<!-- AOP Mapping -->
    	
    	   <!-- Advice classes -->
    	   <bean id="sessionAdvice" class="com.hks.advice.HibernateSessionAdvice"/>
    	   
    	   <aop:config>
    	   	<aop:aspect ref="sessionAdvice">
    	   		<aop:after-returning method="clearSession" pointcut="execution(* *.getAgnisForm(..))"/>
    	   	</aop:aspect>
    	   </aop:config>
    Code:
    public class AgnisFormController extends AbstractController {
    	
    	   protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 
    	   		throws ServletException, IOException, Exception {
    		   
    	      AgnisFormsBusinessDelegate afbd = new AgnisFormsBusinessDelegate();
    	      
    	      AgnisFormsUIProxy proxy = new AgnisFormsUIProxy();
    	      
    	      proxy.setAgnisForm(
    	    		  afbd.getAgnisForm("C:\\...",
    	    				  new Object[]{(Integer)request.getSession().getAttribute("CURR_PATIENT_ID"), 
    	    				  request.getSession().getAttribute("CURRENTUSER_LOGIN_ID")})
    	      );
    	      
    	      return new ModelAndView("bmt", "proxy", proxy);
    	   }
    	}
    Code:
    public class AgnisFormsBusinessDelegate {
    
    	   public CADSRForm getAgnisForm(String path, Object[] ids) {
    		   
    	      CustomAGNISFormHandler formHandler = new CustomAGNISFormHandler();
    	      
    	      CADSRForm form = formHandler.execute(new File(path));
    	      
    	      AgnisFormQuestionAnswers afqa = new AgnisFormQuestionAnswers();
    	      
    	      return form.getPopulateMethod().equalsIgnoreCase("formName") ? afqa.populateAgnisForm(form, ids) : form;
    	   }
    	}

  6. #6
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    It looks like you are calling methods on non-beans (you are directly creating objects using 'new'). Spring AOP works only with Spring beans (in context of your example; there are other situations where this is not precisely correct). You should be obtaining beans from the application context (typically injected as a dependency). Then calling methods on such beans will have the behavior you expect.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  7. #7

    Default

    Thanks for the reply. You are absolutely correct. I just figured this out myself 20mins ago. Here is what I have now and it is finally working.

    Hope your books good. Just ordered it yesterday.

    web.xml

    Code:
    <context-param>
    	     <param-name>contextConfigLocation</param-name>
    	     <param-value>/WEB-INF/web-servlet.xml</param-value>
    	</context-param>
      	
    
    	<listener>  
    	    <listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    web-servlet.xml
    Code:
    <!-- BMT Beans -->
    	<bean name="/bmt/agnisForms.htm" class="com.hks.web.controllers.AgnisFormController" />
    	<bean name="agnisFormBD" class="com.hks.web.business.delegates.AgnisFormsBusinessDelegate" />
    	<bean name="agnisFormUIProxy" class="com.hks.web.ui.proxies.AgnisFormsUIProxy" />
    	
    	<!-- AOP Mapping -->
    	
    		<aop:config>
    	   	<aop:aspect ref="sessionAdvice">
    	   		<aop:after-returning method="clearSession" pointcut="execution(* *.getAgnisForm(..))"/>
    	   	</aop:aspect>
    	   </aop:config>
    	
    	   <!-- Advice classes -->
    	   <bean id="sessionAdvice" class="com.hks.advice.HibernateSessionAdvice"/>
    BusinessDelegate class
    Code:
    public class AgnisFormController extends AbstractController {
    	
    	   protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 
    	   		throws ServletException, IOException, Exception {
    		   
    		   WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
    		   
    	      AgnisFormsBusinessDelegate afbd = (AgnisFormsBusinessDelegate) ctx.getBean("agnisFormBD");
    	    	  
    	      AgnisFormsUIProxy proxy = (AgnisFormsUIProxy) ctx.getBean("agnisFormUIProxy");
    	      
    	      proxy.setAgnisForm(
    	    		  afbd.getAgnisForm(<arg params> );
    	      
    	      return new ModelAndView("bmt", "proxy", proxy);
    	   }
    	}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •