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.
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).