Announcement Announcement Module
Collapse
No announcement yet.
autowire="no" doesn't work in version 2.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • autowire="no" doesn't work in version 2.0

    I am trying to use spring 2.0.2 and I am having problems. Spring says to me that there are several autowire by type candidates, even when I set autowire="no" to ALL beans in my app contexts.
    What should I do?

  • #2
    Hi mvalle

    Can you please post a snippet of your configuration? And if possible, that bit of your code that instantiates your Spring container (if you are not using the web-based convenience filter/listener to instantiate your application context).

    Ciao
    Rick

    Comment


    • #3
      Hello Rick,

      I am instanciating spring factory via junit, whiting eclipse 3.2 (lomboz). Here is some of my code:
      /////////////////////////////////////////////////////////////////
      //Test class
      package com.atosorigin.pme.business.command.calculation;

      import org.apache.commons.chain.Context;

      import com.atosorigin.pme.test.BaseTest;
      import com.atosorigin.pme.test.TestContext;


      public class DeleteExecutedCalculationCommandTest extends BaseTest {
      DeleteExecutedCalculationCommand command;
      public void testExecute() throws Exception {
      Context ctx = new TestContext();
      command.execute(ctx);
      }
      public DeleteExecutedCalculationCommand getCommand() {
      return command;
      }
      public void setCommand(DeleteExecutedCalculationCommand command) {
      this.command = command;
      }

      }

      ////////////////////////////////
      //Base test class
      package com.atosorigin.pme.test;

      import org.springframework.test.AbstractDependencyInjecti onSpringContextTests;

      public class BaseTest extends AbstractDependencyInjectionSpringContextTests {

      // specifies the Spring configuration to load for this fixture
      protected String[] getConfigLocations() {
      return new String[] { "classpath:applicationContext-test.xml" };
      }

      }


      /////////////////////////////////////////////////
      //Class to be tested

      public class DeleteExecutedCalculationCommand implements BusinessCommand {

      // property
      private ParameterConfigurationHistDAO histDao;
      private CalculationProcessHistDAO procHistDao;
      private MirrorReportsDAO reportDao;
      private BusinessCommand findStatus;
      private BusinessCommand saveProcess;

      /* (non-Javadoc)

      * @see com.atosorigin.commons.business.process.BusinessCo mmand#execute(org.apache.commons.chain.Context)

      */

      @SuppressWarnings("unchecked")

      public boolean execute(Context context) throws BusinessException {



      OperatorVO operator = (OperatorVO) context.get(CalculationMessagesVO.OPERATOR);

      CalculationPeriodVO period = (CalculationPeriodVO) context.get(CalculationMessagesVO.PERIOD);

      String newStatus = (String) context.get(FindExecutionStatusByAlias.STATUS_ALIA S);



      try

      {

      // 1. check if the period is open (only open periods may be cancelled and recalculated)

      if (!period.getClosed())

      {

      // 2. retrieves the latest history of parameter configuration for the Operator

      //ParameterConfigurationHistDAO histDao = (ParameterConfigurationHistDAO) DAOFactory.getInstance().getDAO( ParameterConfigurationHistVO.class );

      ParameterConfigurationHistVO configHist = histDao.getLatest(operator);



      // 3. uses parameter configuration history to fetch calculation process information

      // (in fact, its latest history information)

      //CalculationProcessHistDAO<CalculationProcessHistVO > procHistDao = (CalculationProcessHistDAO<CalculationProcessHistV O>) AbstractDAOFactory.getInstance().getDAO(Calculatio nProcessHistVO.class);
      CalculationProcessHistVO procHist = procHistDao.selectLatestByParameterConfigHistInPer iod(configHist, period, CalculationProcessHistVO.CALC_PROC_ALIAS);

      //4. if there's no calculation process information, returns and do nothing

      if (null != procHist) {

      //5. uses the history to delete calculation execution information

      //MirrorReportsDAO<MirrorReportsVO> reportDao = (MirrorReportsDAO<MirrorReportsVO>) AbstractDAOFactory.getInstance().getDAO(MirrorRepo rtsVO.class);

      reportDao.deleteByProcessHistory(procHist);



      // Set calculation process to cancelled.

      CalculationProcessVO process = (CalculationProcessVO) procHist.getProcess();

      //BusinessCommand findStatus = new FindExecutionStatusByAlias();

      // BusinessCommand saveProcess = new SaveExecutionProcessCommand();

      if ((newStatus == null) || ("".equals(newStatus))) {

      newStatus = ExecutionStatusVO.CANCELLED_ALIAS;

      }

      context.put(FindExecutionStatusByAlias.STATUS_ALIA S, newStatus);

      context.put(SaveExecutionProcessCommand.PROCESS, process);

      ChainFacade.executeDefaultChain(context, findStatus, saveProcess);

      }

      }

      } catch (DatabaseException e) {

      throw (new BusinessException("business.error.general_database ", e));

      }

      return false;

      }

      public ParameterConfigurationHistDAO getHistDao() {
      return histDao;
      }

      public void setHistDao(ParameterConfigurationHistDAO histDao) {
      this.histDao = histDao;
      }

      public CalculationProcessHistDAO getProcHistDao() {
      return procHistDao;
      }

      public void setProcHistDao(CalculationProcessHistDAO procHistDao) {
      this.procHistDao = procHistDao;
      }

      public MirrorReportsDAO getReportDao() {
      return reportDao;
      }

      public void setReportDao(MirrorReportsDAO reportDao) {
      this.reportDao = reportDao;
      }

      public BusinessCommand getFindStatus() {
      return findStatus;
      }

      public void setFindStatus(BusinessCommand findStatus) {
      this.findStatus = findStatus;
      }

      public BusinessCommand getSaveProcess() {
      return saveProcess;
      }

      public void setSaveProcess(BusinessCommand saveProcess) {
      this.saveProcess = saveProcess;
      }



      }

      ///////////////////////////////
      My test class has a property called
      DeleteExecutedCalculationCommand command;
      I would like to have
      BusinessCommand command;
      but several classes are inherited of business command. Spring complains about the fact the implementation class cannot be found by using type autowiring. However, my appContext exchanges autowiring to off:
      <bean
      id="com.atosorigin.pme.business.command.calculatio n.DeleteExecutedCalculationCommandTest"
      class="com.atosorigin.pme.business.command.calcula tion.DeleteExecutedCalculationCommandTest"
      autowire="no">
      <property name="command" ref="DeleteExecutedCalculationCommand" />
      </bean>

      It just didn't work. I can put anything in this autowire attribute of bean type element, autowire is always understood by spring as "byType"

      Comment


      • #4
        It makes it soooo much easier if you put your code in [ code] [ /code] tags. What is it you don't want autowired? You can set this explicitly to what ever you want in the unit test by calling setAutowireMode.
        http://www.springframework.org/docs/...owireMode(int)

        Depending on the what you want to do, you could try using byName.
        http://www.springframework.org/docs/...ctory-autowire

        Comment


        • #5
          Hi mvalle

          From the top... you have a file called 'applicationContext-test.xml'... I am assuming that this file (not posted unfortunately) contains a number of beans that all inherit from or implement a common base class or interface called 'BusinessCommand'.

          You also have a JUnit text fixture called 'DeleteExecutedCalculationCommandTest', which extends Spring's AbstractDependencyInjectionSpringCoontextTests (ADISCT). The ADISCT class allows you to automatically inject dependencies straight into the instance of the fixture that is instantiated by the JUnit TestRunner... that last point is important, in that it is the JUnit TestRunner that is going to be instantiating your DeleteExecutedCalculationCommandTest class. You do not need to (and I would go further to say that you must not) declare the 'DeleteExecutedCalculationCommandTest' as a bean in your 'applicationContext-test.xml' file. If you do, then JUnit is going to instantiate the 'DeleteExecutedCalculationCommandTest', and then Spring is going to instantiate another totally distinct, separate instance of the 'DeleteExecutedCalculationCommandTest' class. The fact that you have turned off autowiring for the 'DeleteExecutedCalculationCommandTest' bean is all well and good, but you have not turned off autowiring for the 'DeleteExecutedCalculationCommandTest' that the JUnit TestRunner has instantiated.

          The ADISCT class autowires itself using autowire-byType. My advice to you would be to do the following, and then report back if you are still having any issues.

          1) Delete this bean definition... yes, get rid of it totally (just the bean definition mind, not the test class itself).

          Code:
          <bean
          id="com.atosorigin.pme.business.command.calculatio n.DeleteExecutedCalculationCommandTest"
          class="com.atosorigin.pme.business.command.calcula tion.DeleteExecutedCalculationCommandTest"
          autowire="no">
              <property name="command" ref="DeleteExecutedCalculationCommand" />
          </bean>
          2) Since you have numerous 'BaseCommand' beans defined in 'applicationContext-test.xml', you need to enable autowire-byName in your 'DeleteExecutedCalculationCommandTest' fixture programmatically. Find below an example of how to do that... see also the links that Karl helpfully provided in his earlier reply.

          Code:
          public final class DeleteExecutedCalculationCommandTest extends BaseTest {
          
              private DeleteExecutedCalculationCommand command;
          
              public DeleteExecutedCalculationCommandTest() {
                  setAutowireMode(AUTOWIRE_BY_NAME);
              }
              
              public void testExecute() throws Exception {
                  Context ctx = new TestContext();
                  command.execute(ctx);
              }
              
              //
              // !!! You will have to change the name of this property to reflect the name of the DeleteExecutedCalculationCommand bean definition !!!
              // 
              // !!! As you can see here, I am assuming that it is called '<bean id="bingoCommand" class="..."' in your XML file
              //
              // (In real life calling a bean bingoCommand is a no-no, I'm just using the odd name here so that get a flavour of what to do.)
              //
              public void setBingoCommand(DeleteExecutedCalculationCommand command) {
                  this.command = command;
              }
              
          }
          Give that a whirl.

          Ciao
          Rick

          Mmm, after having gone and had a butchers at the chapter on testing in the Spring Reference Manual that describes the ADISCT class, I find that it is a tad light on details. I'll address that in time for the 2.0.3 release.

          Comment

          Working...
          X