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

Second instance of DynamoDBMapperConfig being created #233

Open
GynnRickerbyNZPost opened this issue Feb 5, 2019 · 14 comments
Open

Second instance of DynamoDBMapperConfig being created #233

GynnRickerbyNZPost opened this issue Feb 5, 2019 · 14 comments
Labels

Comments

@GynnRickerbyNZPost
Copy link

GynnRickerbyNZPost commented Feb 5, 2019

In updating to 5.1.0, on startup we're now getting:

Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dynamoDBMapper' defined in class path resource [{Propriety class details removed}]: Unsatisfied dependency expressed through method 'dynamoDBMapper' parameter 1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig' available: expected single matching bean but found 2: dynamoDBMapperConfig,dynamoDB-DynamoDBMapperConfig

We're creating the DynamoDBMapperConfig as per https://github.com/derjust/spring-data-dynamodb/wiki/Alter-table-name-during-runtime.
Looks like something else is also now creating a DynamoDBMapperConfig.

Should these be created now in a different fashion?

@tiagocpeixoto
Copy link

@GynnRickerbyNZPost

I had the same problem and I found out that I was configuring another DynamoDBMapperConfig bean.

So I removed this extra bean and the error stopped, remaining only the dynamoDB-DynamoDBMapperConfig.

Best regards,

Tiago Peixoto.

@GynnRickerbyNZPost
Copy link
Author

GynnRickerbyNZPost commented Feb 7, 2019

Yes, but I need to configure the DynamoDBMapperConfig to specify table name overrides.

The dynamoDB-DynamoDBMapperConfig one is being created internally (and seems didn't use to be in previous versions), and there's no obvious new documentation on how to turn it off.

@tiagocpeixoto
Copy link

@GynnRickerbyNZPost

Have you tried to set dynamoDBMapperConfigRef property of @EnableDynamoDBRepositories to point to your custom config?

For exemple (in Kotlin):

@EnableDynamoDBRepositories(dynamoDBMapperConfigRef = "customDynamoDBMapperConfig")
class DynamoDbConfig() {

    @Bean("customDynamoDBMapperConfig")
    fun dynamoDBMapperConfig(): DynamoDBMapperConfig = DynamoDBMapperConfig.DEFAULT
}

@GynnRickerbyNZPost
Copy link
Author

Yes. And works fine in 5.0.4.

@Configuration
@EnableDynamoDBRepositories(dynamoDBMapperConfigRef = "dynamoDBMapperConfig")
public class DynamoDBConfig {

  	@Bean("dynamoDBMapperConfig")
	public DynamoDBMapperConfig dynamoDBMapperConfig(final TableNameOverride tableNameOverrider) {

@GynnRickerbyNZPost
Copy link
Author

A quick looking around the code, it looks like DynamoDBMapperConfigFactory was added in 5.1.0, as a FactoryBean which Spring Boot is picking up.

Unfortunately since I've got it working with 5.0.4 I don't have the spare time (project manager is happy with older version working) to go back and work out the appropriate config to override the FactoryBean to use my own settings.

@emckissick
Copy link

I ran into this same problem in Spring Boot, but didn't have to override the factory bean, I just removed the ConfigRef from the @EnabledDynamoDBRepositories annotation. If you have a DynamoDBMapperConfig bean the postProcessAfterInitialization method in the DynamoDBMapperConfigFactory will find it and use it.

Have you tried changing
@EnableDynamoDBRepositories(dynamoDBMapperConfigRef = "dynamoDBMapperConfig")
to just this
@EnableDynamoDBRepositories
If this is the correct intent, the documentation found here Alter table name during runtime may need to be updated.

@tiagocpeixoto
Copy link

I created a PR. Hope it helps!

Feel free to give me feedback.

tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Feb 25, 2019
tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Mar 9, 2019
tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Mar 9, 2019
@tiagocpeixoto
Copy link

I made a new fix related to this issue.

@derjust derjust added the bug label Mar 11, 2019
@efenderbosch
Copy link

Any ETA on this? We'd been waiting for 5.1 to go to Spring Boot 2.1, but now we'll need to wait for this fix as well.

@efenderbosch
Copy link

I figured out a couple ways to get around this until the next release.

First, when you are creating your @Bean instances of the config and mapper, tag them with @org.springframework.context.annotation.Primary

Second, and not as good, in my opinion, use @javax.inject.Named on other beans that get the above beans injected.

@drenda
Copy link

drenda commented Apr 19, 2019

Hi, just to make it clear: I tried this library v5.1 following step by step the tutorial and I've the exception reported above:

Parameter 1 of constructor in org.socialsignin.spring.data.dynamodb.repository.util.Entity2DynamoDBTableSynchronizer required a single bean, but 2 were found:
	- dynamoDBMapper: defined by method 'dynamoDBMapper' in class path resource [cloud/optix/server/config/DynamoDBConfig.class]
	- dynamoDB-DynamoDBMapper: defined in null

I do not have a multiple dynamoDBMapperConfig, I'm just using the basic configuration. Should I move back to the previous version? I'm using Spring Boot 2.1.4

Thanks

@stevebakh
Copy link

stevebakh commented Apr 30, 2019

@emckissick's suggestion worked for us. I'm not sure if there are any other possible pitfalls to using this approach, but simply removing the dynamoDBMapperConfigRef from the @ EnableDynamoDBRepositories annotation and including our own bean definition of the mapper config worked a treat.

Example:

@Configuration
@ConfigurationProperties("amazon.aws")
@Setter
@EnableDynamoDBRepositories(basePackages = "com.package.adapters.dynamodb")
public class PersistenceConfig {

    @NotBlank
    private String accessKey;

    @NotBlank
    private String secretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
        return AmazonDynamoDBClientBuilder
                .standard()
                .withCredentials(credentialsProvider)
                .build();
    }

    @Bean
    public DynamoDBMapperConfig dynamoDBMapperConfig() {
        return new DynamoDBMapperConfig.Builder()
                .withTableNameOverride(withTableNamePrefix("local."))
                .build();
    }
}

@rratliff
Copy link

Like others, I already had a @Bean DynamoDBMapperConfig defined.

For me a combination of removing dynamoDBMapperConfigRef from the @EnableDynamoDBRepositories annotation and also marking my existing DynamoDBMapperConfig bean definition as @Primary seemed to do the trick.

tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Jun 15, 2019
tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Jun 15, 2019
tiagocpeixoto added a commit to tiagocpeixoto/spring-data-dynamodb that referenced this issue Jun 15, 2019
@sunmingtao
Copy link

sunmingtao commented Dec 23, 2020

The following does the trick for me. Primary annotation and the removal of dynamoDBMapperConfigRef

//Make sure there is no dynamoDBMapperConfigRef
@EnableDynamoDBRepositories(basePackageClasses = User.class) 

@Bean
@Primary
public DynamoDBMapper dynamoDBMapper(AmazonDynamoDB amazonDynamoDB, DynamoDBMapperConfig config) {
    return new DynamoDBMapper(amazonDynamoDB, config);
}

@Bean
@Primary
public DynamoDBMapperConfig dynamoDBMapperConfig() {
     return DynamoDBMapperConfig.DEFAULT;
}

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

No branches or pull requests

9 participants