Results 1 to 9 of 9

Thread: Intercepting Spring Core classes

  1. #1
    Join Date
    Feb 2012
    Location
    Curitiba
    Posts
    6

    Default Intercepting Spring Core classes

    Hi everyone, I'm trying to intercept an unmapped bean. Actually, I'm trying to intercept query methods in NamedParameterJdbcOperations and JdbcOperations implementations. Note that my JdbcTemplates classes isn't a mapped bean, but using internal method getXXXJdbcTemplate in XXXDaoSupport class.

    Is possible create a MethodInterceptor to intercept the queryXXX methods?

    Thanks

    Josivan

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

    Default

    No. Spring AOP only allows for interception of external method calls on registered beans. If it is more you want use loadtime or compile timeweaving.
    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
    Feb 2012
    Location
    Curitiba
    Posts
    6

    Default

    I've tried to do something like this:
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource" />
    </bean>

    Then, I've created a method interceptor to jdbcTemplate bean.
    Now, I've this message:

    Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing org.springframework.jdbc.core.JdbcOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.SpringProxy,org.springframewor k.aop.framework.Advised] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing org.springframework.jdbc.core.JdbcOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.SpringProxy,org.springframewor k.aop.framework.Advised] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate': no matching editors or conversion strategy found

    What can I try to intercept that?

    tks

    Josivan

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

    Default

    I suggest a read of the reference guide especially the part that explains AOP and how that is applied in Spring.

    In short you will get an object that acts like an object implementing all interfaces of the JdbcTemplate (you could use JdbcOperations) if you want something else use classbased proxies but this requires cglib on the classpath.

    Also why do you want to intercept the jdbctemplate methods? If it is for the queries etc. there are better options.
    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
    Join Date
    Feb 2012
    Location
    Curitiba
    Posts
    6

    Default

    Hi, The idea is capture the parameters sent to all queries invocation. To print sql commands, the log is perfect. But I need get all parameters, without map each bean or method bean to intercept. Then I thought that intercepting the JdbcTemplate method invocation shall solve my problem.

    I'll trying, if I solve I'll post here.

    Thanks a lot

    Josivan

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Why not simply use a logging jdbc wrapper?! Those are around the internet and serve your purpose without hacking around yourself?
    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

  7. #7
    Join Date
    Feb 2012
    Location
    Curitiba
    Posts
    6

    Default

    Hi, unfortunatelly I can't use some libs. Here, at my company, exists a list with acceptable frameworks (called green list or white list itens). Spring is ok for me, but others no.

    Thanks again

    Josivan

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    So instead of using a widely adopted and available wrapper (which you can install and configure under 1h) you spend days in hacking around and (re)inventing your own... Very wise... I would try and convince them otherwise...

    (Or take a look at those frameworks and use that information to write your own wrapper which gives you a solution which works with everything from JPA to plain JDBC).
    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

  9. #9
    Join Date
    Feb 2012
    Location
    Curitiba
    Posts
    6

    Default

    I agree with you. But here, some things are really complicated. Is a closed world. The easy way is to say: I can't implement this requirement. Maybe I do it.

    Josivan

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
  •