相关疑难解决方法(0)

你如何设计一个继承类?

我听说它说"设计继承"是"困难的",但我从来没有发现过这种情况.任何人(以及任何人,我的意思是Jon Skeet)都可以解释为什么这可能是困难的,陷阱/障碍/问题是什么,为什么凡人的程序员不应该尝试它,只是让他们的课程密封以保护无辜者?

好吧,我嘲笑后者 - 但我很想知道是否有人(包括Jon)真的遇到"继承设计"的困难.我真的从来没有把它视为一个问题,但也许我忽略了一些我认为理所当然的东西 - 或者在没有意识到的情况下搞砸了什么!

编辑:感谢到目前为止所有优秀的答案.我相信共识是​​,对于典型的应用程序类(WinForm子类,一次性实用程序类等),不需要考虑任何类型的重用,更不用说通过继承重用了,而对于库类,考虑重用是至关重要的.通过设计中的继承.

我并没有真的想到一个WinForm类来实现一个GUI对话框,作为一个有人可能会重用的类 - 我有点认为它是一个一次性的对象.但从技术上讲,它是一个类,有人可能会继承它 - 但它不太可能.

很多大规模的发展,我所做的一直是基础库和框架类库,因此设计为通过继承的重用是至关重要的-我只是从来没有认为这是"难",它只是.;-)

但我也从未考虑过与WinForms等常见应用任务的"一次性"类别相比.

当然,欢迎设计继承的更多提示和陷阱; 我也试着投入一些.

language-agnostic inheritance

12
推荐指数
2
解决办法
2099
查看次数

运行Spring单元测试的AOP问题

我有一个Spring Web应用程序,它配置为使用JDK代理AOP.AOP注释(例如@Transactional)在接口上声明,而不是在实现类中声明.

应用程序本身工作正常,但是当我运行单元测试时,它似乎试图使用CGLIB来实现AOP功能(而不是JDK代理).这导致测试失败 - 我在下面添加了堆栈跟踪.

我不明白为什么在运行测试时使用CGLIB,因为Spring配置与应用程序运行时大致相同.一个可能的重要区别是测试配置使用DataSourceTransactionManager而不是JTA事务管理器.测试类本身都扩展了AbstractJUnit4SpringContextTests,可能是这个类以某种方式硬连线使用CGLIB?

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy25]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:213)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:488)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:363)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1343)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    ... 79 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy25
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) …
Run Code Online (Sandbox Code Playgroud)

java junit aop spring transactions

9
推荐指数
1
解决办法
2万
查看次数