Reputation: 11
In my application I am using both Mongo and Aerospike. While creating AerospikeConfig I have to autowire MappingAerospikeConverter and while creating this bean i am getting internal conflicts with bean name.
ERROR
APPLICATION FAILED TO START
Description:
Parameter 1 of constructor in org.springframework.data.aerospike.convert.MappingAerospikeConverter required a single bean, but 3 were found: - org.springframework.data.aerospike.convert.CustomConversions: defined in null - customConversions: defined by method 'customConversions' in class path resource [com/spring/rule/engine/config/RuleEngineConfig.class] - mongoCustomConversions: defined by method 'mongoCustomConversions' in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class]
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.aerospike.cache.AerospikeCacheManager;
import org.springframework.data.aerospike.convert.AerospikeTypeAliasAccessor;
import org.springframework.data.aerospike.convert.MappingAerospikeConverter;
import org.springframework.data.aerospike.mapping.AerospikeMappingContext;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import com.aerospike.client.AerospikeClient;
@EnableAutoConfiguration
@Configuration
@Import(value = {MappingAerospikeConverter.class, AerospikeMappingContext.class, AerospikeTypeAliasAccessor.class,
SimpleTypeHolder.class})
public class AerospikeConfig {
@Value("${fs.aerospike.hostName}")
private String hostName;
@Value("${fs.aerospike.portNumber}")
private int port;
@Autowired
private MappingAerospikeConverter mappingAerospikeConverter;
@Bean
public AerospikeClient aerospike() {
return new AerospikeClient(hostName, port);
}
@Bean
public AerospikeCacheManager cacheManager(AerospikeClient aerospikeClient) {
return new AerospikeCacheManager(aerospikeClient, mappingAerospikeConverter);
}
}
Upvotes: 0
Views: 433
Reputation: 451
With more recent Spring Data Aerospike 5.0.0 it can be even easier: for basic configuration you only need application.properties, and an annotation to enable repositories, like so:
application.properties:
# Aerospike
spring.aerospike.hosts=localhost:3000
spring.data.aerospike.namespace=test
Configuration class:
@Configuration
@EnableAerospikeRepositories(basePackageClasses = AerospikeUserRepository.class)
public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration {
}
From there you can add more properties or override configuration methods like customConverters()
or getClientPolicy()
if needed. You can look at the demo projects
Upvotes: 0
Reputation: 1050
You don't have to define mappingAerospikeConverter
, aerospikeClient
and aerospikeCacheManager
beans just extend AbstractAerospikeDataConfiguration
as described below (you can do the same thing in Mongo's configuration class).
I did manage to use both Aerospike and Mongo in the same project.
Versions I used:
Aerospike:
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>spring-data-aerospike</artifactId>
<version>3.4.1</version>
</dependency>
Mongo (latest):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Configuration classes:
Aerospike:
@Configuration
@EnableConfigurationProperties(AerospikeConfigurationProperties.class)
@EnableAerospikeRepositories(basePackageClasses = AerospikeUserRepository.class)
public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration {
@Autowired
private AerospikeConfigurationProperties aerospikeConfigurationProperties;
@Override
protected Collection<Host> getHosts() {
return Collections.singleton(new Host(aerospikeConfigurationProperties.getHost(), aerospikeConfigurationProperties.getPort()));
}
@Override
protected String nameSpace() {
return aerospikeConfigurationProperties.getNamespace();
}
}
I used an additional configuration properties class for configuration params and then I passed the values in the application.properties file but its not mandatory:
@Data
@Component
@ConfigurationProperties(prefix = "aerospike")
public class AerospikeConfigurationProperties {
private String host;
private int port;
private String namespace;
}
Mongo:
@Configuration
@EnableMongoRepositories(basePackageClasses = MongoUserRepository.class)
public class MongoConfiguration extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "test";
}
}
application.properties:
# aerospike
aerospike.host=localhost
aerospike.port=3000
aerospike.namespace=test
# mongo
spring.data.mongodb.username=root
spring.data.mongodb.password=example
spring.data.mongodb.database=test
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
Upvotes: 4