1

In spring 4 @Autowired is not working for class which extends Region which extends Map

giving exception

No qualifying bean of type [com.gemstone.gemfire.pdx.PdxInstance] found for dependency [map with value type com.gemstone.gemfire.pdx.PdxInstance]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

It is probably assuming a collection injection point. How to do a work around. Even adding a @Qualifier is giving same error.

  • Please provide the relevant code to replicate the issue. – Luiggi Mendoza Sep 02 '15 at 16:28
  • 1
    @Autowired is actually working .... but your PdxInstance is not a spring managed bean ... you should post your spring config file, the java class etc. in order to get help – Pras Sep 02 '15 at 16:40

1 Answers1

2

So, if I am following you correctly (hard to know for sure without a code snippet), I assume you have something like this...

class MyRegion<K, V> extends Region<K, V> {
  ...
}

Then...

@Component
class MyApplicationComponent {

  @Autowired
  private MyRegion<?, PdxInstance> region;

}

Yeah?

So, the problem is, you cannot use @Autowired to inject, or auto-wire a Region reference into your application components. You must use @Resource, like so...

@Component
class MyApplicationComponent {

  @Resource(name = "myRegion")
  private MyRegion<?, PdxInstance> region;

}

The reason is, Spring (regardless of version), by default, whenever it autowires a "Map" into an application component attempts to create a mapping of all Spring beans defined in the Spring ApplicationContext. I.e. bean ID/Name -> bean reference.

So, given...

<bean id="beanOne" class="example.BeanTypeOne"/>

<bean id="beanTwo" class="example.BeanTypeTwo"/>

...

<bean id="beanN" class="example.BeanTypeN"/>

You end up with an auto-wired Map in your application component of...

@Autowired
Map<String, Object> beans;

beans.get("beanOne"); // is object instance of BeanTypeOne
beans.get("beanTwo"); // is object instance of BeanTypeTwo
...
beans.get("beanN"); // is object instance of BeanTypeN

So, what is happening in your case is, there are no beans in the Spring context defined in terms of type (GemFire's) PdxInstance. That is the data in your (custom) Regions(s). So when it tries to assign all the beans in the Spring context or your autowired (custom) Region, which Sprig identifies as a "Map", it cannot assign beans of other types to PdxInstance, taking "Generic" type into account.

So, in short, use @Resource to autowire any GemFire Region, custom or otherwise.

Also, I question the need to "extend" a GemFire Region. Perhaps it is better to use a wrapper ("composition") instead.

Hope this helps.

Cheers!

John Blum
  • 7,381
  • 1
  • 20
  • 30