Pass the @Value annotation, the key value in properties. When I create my own key and value in properties file, I start the key name with “my” to differentiate it from conflicting with one already used in libraries, in most case.
This scheme can be useful when you should use value according to the system environment, like production or development.
Above AwsConfig class has @Configuration annotation. @Component annotation works in the same way.
How to Use Config Class
Then, here’s a sample code using AwsConfig above.
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
openclassAwsS3Gateway{
@Autowired
lateinit varawsConfig:AwsConfig
vars3ClientCache:AmazonS3Client?=null
privatefun getS3Client():AmazonS3Client{
// ToDo; S3Client Session Management
if(s3ClientCache==null)
s3ClientCache=generateS3Client()
returns3ClientCache!!
}
privatefun generateS3Client():AmazonS3Client{
returnAmazonS3Client(awsConfig.credentials)
}
// ...
}
Owing to @Autowired annotation, Spring assigns awsConfig Configration class instance automatically.