Announcement Announcement Module
Collapse
No announcement yet.
Nothing done with the interceptor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        You are casting to the implementation cast to the interface (UserAdmin). Again that question has been answered numerous times too .

        Comment

        Working...
        X