disable spring autowire

3.5k Views Asked by At

i ahave an app with struts2.2 and spring 3.1 and i want to disable spring autowire. I googled a little bit and found that i have to put at <beans> tab default-autowire="no", but this doesn't seems to work.

Then i fount that i can declare this for every <bean> tag like this : <bean autowire="no">, but this does not seems to work either.

When i enabled spring debug logger i can see a lot aof messages like this :

INFO: DEBUG [http-thread-pool-8080(3)] (ConstructorResolver.java:739) - Autowiring by type from bean name 'com.common.actions.PopupAction' via constructor to bean named 'intermedService'

and the corresponding entry in applicationConfig.xml is :

<beans default-autowire="no" 
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="PopupAction" scope="prototype" class="com.common.actions.PopupAction" autowire="no">
        <constructor-arg type="com.common.services.abs.iIntermedService" ref="intermedService"/>
        <constructor-arg type="com.common.services.abs.iLocationService" ref="locationService"/>
        <constructor-arg type="com.common.services.abs.iUserService" ref="userService"/>
        <constructor-arg type="com.common.services.abs.iPhoneService" ref="phoneService"/>
    </bean>

why does spring trying to autowire this action as long as i defined the dependency by hand here and i defined auto-wire="no"?

Or this message tells me that the wiring was made by type via constructor(as i wanted) and "Autowiring by type" means that from the 4 params he matched intermedService with my variable intermed service by type (and not by order or something else)?

3

There are 3 best solutions below

1
On BEST ANSWER

The Struts 2 Spring plugin sets autowiring to "name" by default. Currently I don't believe the plugin allows "none" as a value, but you could try using "auto"--I suspect this will not help, as it is still an autowiring bean factory.

This has been brought up before; I don't recall any real resolution. I've brought it up again to see if it can be addressed in the next release where we doing some significant cleanup.

Edit There's also the struts.objectFactory.spring.autoWire.alwaysRespect constant which defaults to false; try setting it to true. I don't recall which sense of the boolean means which, or if it has the effect--looking in to it now.

0
On

A way to get around it (until it gets fixed) is to just name your fields/constructor arguments and your beans differently, so that spring can't match them.

0
On

I believe default-autowire="no" is enabled by default . i.e, if you don't specify default-autowire, it means default-autowire="no" . Try setting autowire-candidate="false" if you don't want this bean to be autowired http://static.springsource.org/spring/docs/2.5.x/reference/beans.html