简体   繁体   English

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

[英]Spring bean injection without explicit dependency declaration?

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: 我在该项目上工作了几个月,甚至没有注意到这一点,昨天,在编辑一个类文件(使用getter / setter插入新的依赖项bean)之后,我忘了明确添加:

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

in the appropriate spring context xml. 在适当的spring上下文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. 我将Web应用程序发布到tomcat,进入调试,当我看到使用该服务bean的代码行时,我意识到我忘记了将其声明为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. 可以看出,如果重要的话,类字段的名称与被注入的bean相同。 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. 所以我猜想它必须与春季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. 我必须补充一点,我没有从头开始创建此项目,它已经配置好,它使用spring-aop进行事务划分和一些日志记录目的。

EDIT 编辑
Some additional info: project integrates ZK Ajax and Hibernate as well. 一些其他信息:项目还集成了ZK Ajax和Hibernate。 This service bean is basically a wrapper around a DAO bean; 这个服务bean基本上是DAO bean的包装器。 DAO bean is in turn a wrapper around spring's HibernateTemplate. DAO bean依次是spring的HibernateTemplate的包装。 Service and DAO beans are singleton-scoped. Service和DAO bean是单例作用域的。 Service that is being injected is injected into the prototype-scoped MVC controller bean. 注入的服务被注入到原型作用域的MVC控制器bean中。 Service bean is from the package used for DB transaction demarcation: Service 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>

So, can anyone explain to me what is going on, or what could be the reason for this? 那么,谁能向我解释发生了什么,或者是什么原因呢?
Thanks. 谢谢。

On the root beans tag of the xml there is an attribute default-autowire-byname / default-autowire-bytype . 在xml的root bean标记上,有一个属性default-autowire-byname / default-autowire-bytype If that is set to true then spring will inject the dependencies automatically. 如果将其设置为true,那么spring将自动注入依赖项。 By default it is set to false - I am guessing that in your case one of the above flag is set to true. 默认情况下,它设置为false-我猜在您的情况下,上述标志之一设置为true。

AOP is not involved in dependency injection. AOP不参与依赖项注入。 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). 您正在看到代理,因为被注入的对象正在使用需要aop的spring的某些功能(例如事务,安全性等)。

Check for one of the following in your configurations . 在您的配置中检查以下内容之一。

context:annotation-config or context:component-scan along with context:annotation-config或context:component-scan以及

  1. @Autowired annotation along with @Component on spring beans for autowiing by type . @Autowired批注以及@spring bean上的@Component,用于按类型自动装配。

  2. @Resource annotation which does autowiring by name + @Component in your spring beans as you have not defined any beans in the spring configuration. @Resource批注会在名称中自动按名称+ @Component在Spring Bean中自动装配,因为您尚未在Spring配置中定义任何Bean。 You sping beans will have the first letter of the classname lowercased . 您的sping bean将使用类名的首字母小写。

  3. default-autowire="bytype" as @gkamal has discussed. @gkamal已讨论过default-autowire =“ bytype”。

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

  5. default-autowire="autodetect". default-autowire =“自动检测”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM