package com.backend.springtok.entity;
public class Doctor implements Staff {
private String qualification;
private Nurse nurse;
public Doctor(String qualification) {
this.qualification = qualification;
}
public void assist() {
System.out.println("doctor is assisting");
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public Nurse getNurse() {
return nurse;
}
public void setNurse(Nurse nurse) {
this.nurse = nurse;
}
@Override
public String toString() {
return "Doctor{" +
"qualification='" + qualification + '\'' +
", nurse=" + nurse +
'}';
}
}
3. Create a application context
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// to create a bean
Doctor doctor = context.getBean(Doctor.class);
Using Class based configuration
create a config bean class
@Configuration
@ComponentScan( basePackages = "com.backend.springtok.entity")
public class BeanConfig {
@Bean
public NewDoctor getNewDoctor() {
return new NewDoctor();
}
}
2. Create class to construct bean of
@Scope(scopeName = "singleton")
// you can define the any other scope like prototype as well
public class NewDoctor implements Staff, BeanNameAware {
private String qualification;
public void assist() {
System.out.println("new doctor is assisting");
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
@Override
public String toString() {
return "NewDoctor{" +
"qualification='" + qualification + '\'' +
'}';
}
@Override
public void setBeanName(String s) {
System.out.println("call for setBeanName");
}
@PostConstruct
public void postConstruct(){
System.out.println("post construct called");
}
}
3. Add this to your spring.xml under the beans tag
4. Create application context to initialise and manage beans
ApplicationContext anotherContext =
new AnnotationConfigApplicationContext(BeanConfig.class);
var newDoctor = anotherContext.getBean(NewDoctor.class);
newDoctor.assist();
newDoctor.setQualification("MBBS++");
System.out.println(newDoctor);
Using the javax.annotation.api , we can annotate different methods at different bean life-cycle to execute some code. Example, in NewDoctor class in above example, we have @PostContruct method which is called after the bean is created by spring.
@PostConstruct
public void postConstruct(){
System.out.println("post construct called");
}
Creating a protoptye bean inside singleton class
If you declare a prototype bean inside a singleton class, and create 2 different instance of singleton, there prototype bean will also be the same
// example : prototype
@Service
@Scope("prototype")
public class WeatherService {
String time = LocalDateTime.now().toString();
int temp = new Random().nextInt(60);
public String getTodayTemp() {
return time + " -> " + temp;
}
}
// service : singleton
@Service
public class UserService {
@Autowired
WeatherService weatherService;
@Autowired
ApplicationContext context;
@Autowired
private ObjectFactory<WeatherService> wFactory;
// use this to create seperate beans for each singleton instance
public String getCurrentTemp() {
return weatherService.getTodayTemp();
}
public String getCurrentTempDiff() {
// not recommended
// return context.getBean(WeatherService.class).getTodayTemp();
return wFactory.getObject().getTodayTemp();
}
}