A Spring bean is not usually proxied unless some special external behavior needs to be applied. For example, AOP advice, transaction management, bean scope, etc.
Your context seems to be incomplete. If you've declared some AOP joinpoint that matches a JdbcTemplate
method, then that bean will be proxied. You can specify proxy settings, for example if Spring should proxy-target-class
instead of the interface. If you have the CGLIB library on your classpath, you shouldn't have any problem by setting
<aop:config proxy-target-class="true"> ...
A small example of JDK proxies
public static void main(String[] args) throws Exception {
JdbcTemplate template = new JdbcTemplate();
Object proxy = Proxy.newProxyInstance(template.getClass().getClassLoader(), template.getClass().getInterfaces(), new ProxyJdbcTemplateHandler(template));
System.out.println(proxy.getClass());
System.out.println(proxy.getClass().getSuperclass());
System.out.println(Arrays.toString(proxy.getClass().getInterfaces()));
}
public static class ProxyJdbcTemplateHandler implements InvocationHandler {
private JdbcTemplate target;
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// do something with target
return null;
}
public ProxyJdbcTemplateHandler(JdbcTemplate target) {
this.target = target;
}
}
prints
class $Proxy0
class java.lang.reflect.Proxy
[interface org.springframework.jdbc.core.JdbcOperations]
The proxy has the type of the superinterface of JdbcTemplate
, but not the type itself. Its parent class is actually Proxy
. For that you will need to use CGLIB proxies, by providing CGLIB jars on your classpath and specifying proxy-target-class="true"
in your configuration.