Results 1 to 4 of 4

Thread: Nothing done with the interceptor

  1. #1
    Join Date
    Apr 2008
    Posts
    3

    Default Nothing done with the interceptor

    hi


    i try to use spring interceptor

    applicationContext.xml
    Code:
    <bean id="userAdminDao" class="com.server.admin.UserAdminImpl">
      <property name="userDao" ref="adminJdbcDao"/>
    </bean> 
    
    <bean id="securityInterceptor" class="com.server.admin.SecurityInterceptor"></bean>
        
    <bean id="interceptedService" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="target">
        <ref bean="userAdminDao"/>
      </property>
      <property name="interceptorNames">
        <list>
          <value>securityInterceptor</value>
        </list>
      </property>
    </bean>

    interceptor code
    Code:
    public class SecurityInterceptor implements MethodInterceptor {
    
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            long startTime = System.currentTimeMillis();
    
            Object result = methodInvocation.proceed();
    
            long duration = System.currentTimeMillis() - startTime;
            Method method = methodInvocation.getMethod();
    
            String methodName = method.getDeclaringClass().getName() + "." + method.getName();
            System.out.println("Method '" + methodName + "' took " + duration + " milliseconds to run");
    
            return null;
        }
    }
    my test class
    Code:
    public class MonServiceTest extends TestCase {
    
        public void testMonService() {
    
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
            UserAdminImpl userAdmin = (UserAdminImpl) context.getBean("userAdminDao");
            userAdmin.getUser("4");
    
        }
    }
    i don't see the output of the SecurityInterceptor,so it's not executed..

    any idea?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Use the search, this question has been answered numerous times.

    You aren't using the proxied instance. Your 'userAdminDao' isn't proxied your 'interceptedService' is. Use the latter. To prevent access to your unproxied instance define it as an anonymous inner bean.

    Next to that read chapter 6 of the reference guide that explains spring AOP.
    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

  3. #3
    Join Date
    Apr 2008
    Posts
    3

    Default

    Quote Originally Posted by Marten Deinum View Post
    Use the search, this question has been answered numerous times.

    You aren't using the proxied instance. Your 'userAdminDao' isn't proxied your 'interceptedService' is. Use the latter. To prevent access to your unproxied instance define it as an anonymous inner bean.

    Next to that read chapter 6 of the reference guide that explains spring AOP.
    done but i get a beautifull
    Code:
    $Proxy0
    java.lang.ClassCastException: $Proxy0

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

    Default

    You are casting to the implementation cast to the interface (UserAdmin). Again that question has been answered numerous times too .
    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

Posting Permissions

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