Spring bean注入没有显式的依赖声明?

发布于 2024-12-22 17:12:54 字数 1359 浏览 1 评论 0原文

我在这个项目上工作了几个月,甚至没有注意到这一点,昨天,在编辑一个类文件(插入新的依赖 bean 及其 getter/setter)后,我忘记显式添加:

<property name="deviceService" ref="deviceService"/>

在适当的 spring 上下文 xml 中。
我将我的 Web 应用程序发布到 tomcat,进入调试,当我看到使用此服务 bean 的代码行时,我意识到我忘记将其声明为依赖项。
但随后,奇怪的事情发生了——尽管如此,豆子还是被注入了......
这种行为让我有点困惑。我当然不是弹簧专家,过去几个月我一直在使用它,但这不是我期望的事情。可以看出,类字段的名称与正在注入的 bean 相同(如果有的话)。在调试器中,我在依赖项字段中看到了类似的内容:

deviceService=$Proxy5 (id=107)
   * h=JdkDynamicAopProxy (id=147)

所以我猜测它必须与 Spring AOP 做一些事情。

我必须补充一点,我并不是从头开始这个项目,它已经配置好了,它使用 spring-aop 进行事务划分,以及一些日志记录目的。

编辑
一些附加信息:项目还集成了 ZK Ajax 和 Hibernate。该服务 bean 基本上是 DAO bean 的包装器; DAO bean 又是 Spring 的 HibernateTemplate 的包装器。服务和 DAO bean 是单例范围的。正在注入的服务被注入到原型范围的 MVC 控制器 bean 中。服务bean来自用于数据库事务划分的包:

<tx:advice id="serviceTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceMethodsRMS"
        expression="execution(* org.irvas.amregina.backend.service.*.*(..))" />
    <aop:advisor advice-ref="serviceTxAdvice" pointcut-ref="serviceMethodsRMS" />
</aop:config>

那么,谁能向我解释一下发生了什么,或者可能是什么原因?
谢谢。

I was working for a few months on the project without even noticing this, and yesterday, after editing a class file (inserting new dependency bean with its getter/setter), I forgot to explicitly add:

<property name="deviceService" ref="deviceService"/>

in the appropriate spring context xml.
I publshed my web app to tomcat, entered debug, and the moment I saw the line of code using this service bean, I realized that I forgot to declare it as the dependecy.
But then, strange thing happend - the bean was injected nevertheless...
This behaviour confuses me a bit. I'm surely not a spring expert, I've been using it for the past several months, however this is not something that I expected to be possible. The name of the class field, as it can be seen, is the same as bean that is being injected, if that matters. In the debugger I saw something like this for the dependency field:

deviceService=$Proxy5 (id=107)
   * h=JdkDynamicAopProxy (id=147)

so I'm guessing it has to do something with spring AOP.

I must add that I didn't start this project form scratch, it was already configured, it uses spring-aop for transactions demarcation, and some logging purposes.

EDIT
Some additional info: project integrates ZK Ajax and Hibernate as well. This service bean is basically a wrapper around a DAO bean; DAO bean is in turn a wrapper around spring's HibernateTemplate. Service and DAO beans are singleton-scoped. Service that is being injected is injected into the prototype-scoped MVC controller bean. Service bean is from the package used for DB transaction demarcation:

<tx:advice id="serviceTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceMethodsRMS"
        expression="execution(* org.irvas.amregina.backend.service.*.*(..))" />
    <aop:advisor advice-ref="serviceTxAdvice" pointcut-ref="serviceMethodsRMS" />
</aop:config>

So, can anyone explain to me what is going on, or what could be the reason for this?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中二柚 2024-12-29 17:12:54

在 xml 的根 beans 标记上,有一个属性 default-autowire-byname / default-autowire-bytype。如果设置为 true 那么 spring 将自动注入依赖项。默认情况下它设置为 false - 我猜测在您的情况下上述标志之一设置为 true。

AOP 不涉及依赖注入。您看到代理是因为正在注入的对象正在使用需要 aop 的 spring 的某些功能(如事务、安全性等)。

On the root beans tag of the xml there is an attribute default-autowire-byname / default-autowire-bytype. If that is set to true then spring will inject the dependencies automatically. By default it is set to false - I am guessing that in your case one of the above flag is set to true.

AOP is not involved in dependency injection. You are seeing the proxy because the object that is being injected is using some feature of spring that needs aop (like transaction , security etc).

眉黛浅 2024-12-29 17:12:54

检查您的配置中是否存在以下一项。

context:annotation-config 或 context:component-scan 以及

  1. @Autowired 注释以及 spring bean 上的 @Component,用于按类型自动配置。

  2. @Resource 注释,它会在 spring bean 中按名称 + @Component 自动装配,因为您尚未在 spring 配置中定义任何 bean。 spring beans 的类名首字母将小写。

  3. default-autowire="bytype" 正如@gkamal 所讨论的。

  4. default-autowire="byname" + 一个@Component。

  5. default-autowire="autodetect"。

Check for one of the following in your configurations .

context:annotation-config or context:component-scan along with

  1. @Autowired annotation along with @Component on spring beans for autowiing by type .

  2. @Resource annotation which does autowiring by name + @Component in your spring beans as you have not defined any beans in the spring configuration. You sping beans will have the first letter of the classname lowercased .

  3. default-autowire="bytype" as @gkamal has discussed.

  4. default-autowire="byname" + an @Component.

  5. default-autowire="autodetect".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文