我想用NO接口定义一个Spring Bean CLASS [英] I want to define a Spring Bean CLASS with NO Interface

查看:137
本文介绍了我想用NO接口定义一个Spring Bean CLASS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 HibernateDaoSupport 的spring bean。我希望将这个bean注入我的控制器,但我不希望它实现任何接口。我只是想从我的其余代码中引用具体类(也许不使用AopProxy?)有没有人有办法这样做?

I have a spring bean that extends HibernateDaoSupport. I want this bean injected into my Controllers, but I do NOT want it to implement any interface. I just want to refer to the concrete class from within the rest of my code (not use the AopProxy perhaps?) Does anyone have a way of doing this?

<bean id="mySampleService" class="com.sample.MySampleService">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

@Autowired
private MySampleService mySampleService;

... getters and setters ....

我知道它是一个好主意使用接口,这是IoC的重点,但请不要建议我使用接口。

I know it's a good idea to use the interface and that's the whole point of IoC, but PLEASE DON'T suggest I use the interface.

推荐答案

如果要代理的类(在您的情况下通过事务代理)实现任何接口( InitializingBean HibernateDaoSupport 实现在您的case),Spring默认使用基于JDK动态代理的代理策略。

If class to be proxied (by transactional proxy in your case) implements any interface (InitializingBean implemented by HibernateDaoSupport in your case), Spring by default uses proxying strategy based on JDK dynamic proxies.

因此,它会创建一个类型为 InitializingBean 的代理,这显然不能注入到类型的字段中 MySampleService

So, it creates a proxy of type InitializingBean, that, obviously, cannot be injected into a field of type MySampleService.

如果您不想使用界面,可以通过声明覆盖事务方面使用的策略< tx:annotation-driven> proxy-target-class =true

If you don't want to use interface you can override the strategy used by transactional aspect by declaring <tx:annotation-driven> with proxy-target-class = "true".

参见:

  • 7.6 Proxying mechanisms

这篇关于我想用NO接口定义一个Spring Bean CLASS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆