package com.javabykiran;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
public class Merchant {
@Value("${data.merchId}")
int merchantId;
@Value("${data.legalBusinessName}")
String legalBusinessName;
@Autowired
@Override
public String toString() {
return "Merchant [merchantId=" + merchantId + ",
legalBusinessName=" + legalBusinessName + ",
merchAddress=" + merchAddress + "]";
}
}
package com.javabykiran;
import org.springframework.beans.factory.annotation.Value;
public class MerchAddress {
@Value("${data.officeNo}")
String officeNo;
@Override
public String toString() {
return "MerchAddress [officeNo=" + officeNo + "]";
}
}
package com.javabykiran;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = { "data.properties" })
public class ConfigClass {
@Bean
Merchant merchantBean() {
return new Merchant();
}
/*
* @Bean({"m1", "m2"})
* Merchant merchantBeans() {
* return new Merchant();
* }
*/
// this will be injected automatically into MerchAddress
@Bean
MerchAddress addressBean() {
return new MerchAddress();
}
}
data.officeNo=234
data.legalBusinessName=jbk
data.merchId=1733
package com.javabykiran;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Client {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigClass.class);
Merchant merchant = context.getBean(Merchant.class);
System.out.println(merchant);
}
}