Spring application has Cglib2AopProxy warnings

2019-02-03 07:32发布

Upon starting my application, I get numerous warnings along the lines of o.s.aop.framework.Cglib2AopProxy 'Unable to proxy method [public final void org.springframework.jdbc.core.support.JdbcDaoSupport.setDataSource(javax.sql.DataSource)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.' for about a dozen or so functions.

Now I perfectly understand that proxy-based aspects cannot be applied to final methods. However, I did not (on purpose, at least) try to weave any aspects into JdbcDaoSupport. I suspect it comes from <tx:annotation-driven />. Is there anything I can do to silence these warnings or, better yet, exclude those classes from the aspect weaving?

3条回答
三岁会撩人
2楼-- · 2019-02-03 07:55

You should use interfaces for dependency injection, the most reasons for this are described here and here.

You can read documentation about proxying mechanic for details why you see this warning.

And please vote for feature request of inspection for IntelliJ that may helps us to avoid this warnings. BTW It also contains a good explanation.

查看更多
smile是对你的礼貌
3楼-- · 2019-02-03 07:56

Maybe you have extended JdbcDaoSupport and added @Transactional annotations.

You can set the Cglib2AopProxy logger to log level ERROR to avoid the warn messages. For example if using log4j and log4j.properties:

log.logger.org.springframework.aop.framework.Cglib2AopProxy = ERROR
查看更多
何必那么认真
4楼-- · 2019-02-03 07:57

This is most likely caused by the @Transactional annotation, Spring wraps your DAO in a proxy to add the transactional behavior.

I would recommend to make your DAO implement an Interface (create and use an interface for your DAO), this will allow Spring to use a JDK dynamic proxy instead of having to use CGLib.

Using CGLIB has a limitation that methods marked as final in target class can’t be advised as final methods can’t be overridden (CGLIB creates a subclass of target class at runtime) but this limitation disappears in case of using JDK dynamic proxies.

Reference

查看更多
登录 后发表回答