Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashOperations not being autowired [DATAREDIS-8] #586

Closed
spring-projects-issues opened this issue Mar 17, 2011 · 9 comments
Closed

HashOperations not being autowired [DATAREDIS-8] #586

spring-projects-issues opened this issue Mar 17, 2011 · 9 comments
Labels
type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link

Andrew Berman opened DATAREDIS-8 and commented

The documentation states that if you inject the RedisTemplate it should automatically convert to the proper operation. It doesn't do it for HashOperations. If that is by design due to the added key in the hash then the documentation should be updated


Affects: 1.0.0.M3

Referenced from: commits a6f79e2

@spring-projects-issues
Copy link
Author

Andrew Berman commented

Here's the exception: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.data.keyvalue.redis.core.HashOperations] found for dependency

@spring-projects-issues
Copy link
Author

Costin Leau commented

What's your configuration. It works in our test suite for the following config:

<bean id="redisTemplate" class="org.springframework.data.keyvalue.redis.core.RedisTemplate".../>
	
<bean id="injected" class="org.springframework.data.keyvalue.redis.RedisViewPE" 
		p:hash-ops-ref="redisTemplate"/>

Where RedisViewPE has setter

public void setHashOps(HashOperations<Object, String, Object> hashOps) {
	this.hashOps = hashOps;
}

@spring-projects-issues
Copy link
Author

Andrew Berman commented

I followed the documentation (though I'm using Java-based config):

Config:

@Bean
public RedisTemplate<String,Object> redisTemplate() {
...
}

Bean:

@Autowired
private RedisTemplate<String,Object> redisTemplate;

@Autowired
private HashOperations<Object, String, Object> hashOps;

This is how the documentation shows it. But now I see you're doing things differently. You're injecting hashOps manually with redisTemplate, so I guess that's what I need to do. Can you update the documentation to reflect this? Or should it work the way I have it and I'm doing something wrong?

@spring-projects-issues
Copy link
Author

Costin Leau commented

The docs might need updating. Can you add a qualifier when injecting the hashOps and see whether it changes the situation (autowiring doesn't do any conversion unless told so):
@Autowired
@Qualifier("redisTemplate")
hashOps...

See http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-autowired-annotation-qualifiers

@spring-projects-issues
Copy link
Author

Costin Leau commented

To add, you could also use @Resource since in this case it's about autowiring by name rather then by type (which doesn't exist as it requires conversion)

@spring-projects-issues
Copy link
Author

Andrew Berman commented

This is the error I get when using Qualifier:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.data.keyvalue.redis.core.HashOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=redisTemplate)}

This is the error with Resource(name="redisTemplate"):

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'redisTemplate' must be of type [org.springframework.data.keyvalue.redis.core.HashOperations], but was actually of type [org.springframework.data.keyvalue.redis.core.RedisTemplate]

Error with just @Resource:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.data.keyvalue.redis.core.HashOperations] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(mappedName=, shareable=true, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}

@spring-projects-issues
Copy link
Author

Costin Leau commented

Hi Andrew,

Sorry for the late reply - I've actually investigated this issue after you reported it but forgot to add a comment.
The behaviour above, for @Resource, is a simply a bug - the container should do the conversion (just as it does with @Value). I've raised this with Juergen and now added an issue to not lose track of it: https://jira.springsource.org/browse/SPR-8480

I'm not sure whether the fix will make it into 3.0.6 but it should into the 3.1 branch

@spring-projects-issues
Copy link
Author

Andrew Berman commented

Cool, thanks Costin!

@spring-projects-issues
Copy link
Author

Costin Leau commented

Seems the issue has been fixed in Spring 3.0.6 and 3.1.RC1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant