lundi 2 mars 2015

Spring JavaConfig how do I refer to beans that I have defined to create new beans

Here's an excerpt from my Applications beans definition, I want to be able to refer to beans that I have defined.



@Configuration
@ComponentScan({"com.abc.config", "com.abc.config.common"})
public class ApplicationConfig {
@Bean(name = "AWSCredentialsProvider")
AWSCredentials credentialsProvider() { return new AWSCredentials(/*Omitted*/); }
@Bean(name = "DynamoDBClient")
AmazonDynamoDBClient dynamoDBClient() {
AmazonDynamoDBClient dynamoDB = new AmazonDynamoDBClient(credentialsProvider());
return dynamoDB;
}
@Bean S3Repository s3Repository() {
AmazonS3 s3 = new AmazonS3Client(credentialsProvider());
return new S3Repository(s3);
}
@Bean LevelMapper levelMapper() { return new LevelMapper(s3Repository()); }
@Bean ImageDownloader imageDownloader() { return new ImageDownloader(s3Repository()); }
}


Right now what I'm doing is calling methods like s3Repository() in two places; this way I'll be creating two instances of the repository whereas I would like there to be only one instance across the application. Something like credentialsProvider() is lightweight so I don't mind a new instance being created for every bean.


Aucun commentaire:

Enregistrer un commentaire