May 19th, 2012, 11:17 PM
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?
May 21st, 2012, 01:46 AM
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.
May 21st, 2012, 02:02 PM
I've tried to do something like this:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
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?
May 22nd, 2012, 01:02 AM
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.
May 22nd, 2012, 07:12 AM
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
May 22nd, 2012, 07:14 AM
Why not simply use a logging jdbc wrapper?! Those are around the internet and serve your purpose without hacking around yourself?
May 22nd, 2012, 07:38 AM
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.
May 22nd, 2012, 07:47 AM
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).
May 22nd, 2012, 08:05 AM
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.
Tags for this Thread