Spring, by default, uses JDK Dynamic proxies to apply AOP. (For more information on proxies see here).
What happens isthat a dynamic class is created (com.sun.proxy.$Proxy53
) which implements all the interfaces that the target class implements. For a JdbcTemplate
that is JdbcOperations
and InitializingBean
. So the dynamic class is a JdbcOperations
but not a JdbcTemplate
and hence the injection fails.
You have a couple of solutions
- Use the interface
JdbcOperations
instead of the classJdbcTemplate
to program against - Use class based proxies
- Use loadtime weaving
Now option 1 and 2 are the easiest whereas option 3 is the most powerful and complex to get started (see the links below).
For 1 in your class change
@Autowired
private JdbcTemplate jdbcTemplate;
To
@Autowired
private JdbcOperations jdbcTemplate;
If you are extending JdbcDaoSupport
you might be in a pickle and then it won't work.
Option 2, assuming you have <aop:aspectj-autoproxy />
set the proxy-target-class
attribute to true
. This will require cglib and will create class based proxies instead of interface based proxies.
For option 3 I refer to the reference guide as that involves (probably) a java-agent and might be a little more complicated to get working.
Links