1

Following problem:

I'm developing a WCF Service that uses RabbitMQ to connect to an API. We use spring as a DI container.

We made a consumer class (some custom logic for rabbit MQ + logging)

Trimmed down version:


public class Consumer : DefaultBasicConsumer
{
    public Consumer(IModel channel)
            : base(channel)
        {}
}

And we have a ConnectionManager class:


public class ConnectionManager
{
    public IModel Channel { get; set; }
    public IConnection Connection { get; set; }

    private readonly ConnectionFactory _connectionFactory;

    public ConnectionManager()
    {
        _connectionFactory = SetupConnectionFactory();

        Connection = _connectionFactory.CreateConnection();
        Channel = Connection.CreateModel();
    }
}

Now the problem, when wiring up everyting with Spring.NET. We want to inject the Channel property of the ConnectionManager class into the Consumer constructor.

Spring config so far (Trimmed down):

 <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <object name="connectionManager" type="Epex.ConnectionManager, EpexData" singleton="true"/>

    <object name="consumer" type="Epex.Consumer, EpexData">
      <constructor-arg ref="Do something funky here"/>
    </object>
</spring>

So what do I place on the Do Something funky here?

We could also rewrite and inject the ConnectionManager in the consumer (Last option)

Preben Huybrechts
  • 5,853
  • 2
  • 27
  • 63
  • A solution is posted here http://stackoverflow.com/questions/6739899/spring-net-propertyretrievingfactoryobject-property-is-null . You can use `PropertyRetrievingFactoryObject` or an expression in SpEL. Documentation: http://www.springframework.net/doc-latest/reference/html/objects.html#objects-advancedproperty-setting – Andreas Aug 18 '12 at 12:52

1 Answers1

2

You can modify ConnectionManager

public class ConnectionManager
{
  public IModel Channel { get; set; }
  public IConnection Connection { get; set; }

  private readonly ConnectionFactory _connectionFactory;

  public ConnectionManager()
  {
    _connectionFactory = SetupConnectionFactory();

    Connection = _connectionFactory.CreateConnection();
    Channel = Connection.CreateModel();
  }

  public IModel GetChannel()
  {
    return Channel;
  }
}
 <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <object name="connectionManager" type="Epex.ConnectionManager, EpexData" singleton="true"/>

    <object name="consumer" type="Epex.Consumer, EpexData">
      <constructor-arg>
        <object factory-object="connectionManager" factory-method="GetChannel" />
      </constructor-arg>
    </object>
</spring>
Kirill Muratov
  • 368
  • 2
  • 8