Results 1 to 4 of 4

Thread: after returning advice

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Default after returning advice

    This is my first attempt at implementing Spring's AOP. What i am looking to do is to use an after returning advice when a method successfully executes. In context to what I am trying to accomplish. I want to send emails as soon as someone signs up in our system.
    Please look at my attachment for the code example that i am using

    The same configuration works as a standalone java project with Spring.jar added to it. What am i doing wrong here? been stuck with this for the last day or so.

    Thanks for the help!!
    Attached Files Attached Files

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    1. Provided expression ('*') can't be used as a regexp pattern. Use '.*' instead;
    2. You want to match all methods. There is no point to use a RegexpMethodPointcutAdvisor then. I.e. the mapping may be simplified to
      HTML Code:
      	<bean id="getUsersService" class="org.springframework.aop.framework.ProxyFactoryBean">
      		<property name="proxyInterfaces">
      			<value>org.service.UserService</value>
      		</property>
      		<property name="interceptorNames">
      			<list>
      				<value>sendEmailAdvice</value>
      			</list>
      		</property>
      		<property name="target">
      			<bean id="userService" class="org.impl.UserServiceImpl">
      				<property name="userDao" ref="userDao"/>
      			</bean>
      		</property>
      	</bean>
      
      	<bean id="sendEmailAdvice" class="org.aop.SendEmailAdvice"/>
    3. Spring 2.x offers much more convenient way to configure aspects. E.g.:
      HTML Code:
      	<bean id="userService" class="org.impl.UserServiceImpl">
      		<property name="userDao" ref="userDao"/>
      	</bean>
      	
      	<bean id="sendEmailAdvice" class="org.aop.SendEmailAdvice"/>
      	
      	<aop:config>
      		<aop:advisor advice-ref="sendEmailAdvice" pointcut="execution(* org.service.UserService+.*(..))"/>
      	</aop:config>

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Default

    1. I did try .* instead of the (".") and I couldn't get it to work.
    2. The reason for using the RegexpMethodPointcutAdvisor was to match .*insert.* method in my service. I apologize for not being clear on that part.
    3. Taking advantage of Spring 2.x to configure aspects threw an exception
    nested execption is java.lang.NoClassDefFoundError: org/aspect/weaver/BCException which says that i don't have the aspectJ dependency added. I was trying to take advantage of Spring's native implementation of AOP (i think it uses a part of AspectJ). Please correct me if I am wrong.

    Further searching and reading lead me to this configuration which worked.

    HTML Code:
    <bean id="sendEmailAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    	<property name="advice" ref="sendEmailAdvice"/>
    	<property name="pattern">
    		<value>.*insert.*</value>
    	</property>
    </bean>
    
    <bean id="getUsersService" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    		<property name="beanNames"><value>userService</value></property>		
    		<property name="interceptorNames">
    			<list>
    				<value>sendEmailAdvisor</value>
    			</list>
    		</property>
    </bean>
    
    <bean id="sendEmailAdvice" class="org.aop.SendEmailAdvice"/>
    However this configuration worked for me. I want to know what i was doing wrong before in my earlier configuration as to why it did not work with ProxyFactoryBean.

    Thanks,
    Sathish

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    1. Your initial config worked fine for me after changing '*' to '.*'. It's absolutely unclear what did you have from 'I did try .* instead of the (".") and I couldn't get it to work';
    2. BeanNameAutoProxyCreator is related to the old spring aop. I suggest you to follow spring team recommendation and use new aop config facilities;
    3. If you have a problem with AOP create small standalone test-case that reproduces the problem and post it here. It's not possible to understand how to fix your problem if there is no even problem description;

Tags for this Thread

Posting Permissions

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