Create a application using singleton property and prototype property.
Create a boolean variable that will verify if the classes are singleton or not
//applicationContext.xml – prototype
//main
public static void main(String[] args) { //get application context file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/com/springboot/beanScope-appContext.xml"); //get bean
Coach coachOne = context.getBean("coach", Coach.class);
Coach coachTwo = context.getBean("coach", Coach.class);
boolean isSingleton = coachOne == coachTwo;
//will return true if singleton scope or false if prototpe scope
System.out.println("Singleton = " + isSingleton); //close context
context.close();
}Exista doua metode apeleaza o data cu clasa spring bean, acestea sunt ?
init si destroy
init-method
destroy-method
Creati un exemplu de init si destroy methods
//application context file
//Main class
public class BeanScopeDemoApp {public static void main(String[] args) { //get application context file
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("file:src/main/java/com/springboot/beanLifeCycle-appContext.xml"); //get bean
Coach coach = context.getBean("coach", Coach.class);System.out.println(coach.getDailyWorkout());
//close context
context.close();}
}
//Output BaseballCoach inside doMyStartupStuff method daily workout BaseballCoach inside doMyCleanupStuff method
When the destroy method is not called ?
when using scope=prototype
How can you destroy prototype beans ?
//implement DisposableBean interface
public class TrackCoach implements Coach, DisposableBean {
...
// add a destroy method
@Override
public void destroy() throws Exception {
System.out.println("TrackCoach: inside method doMyCleanupStuffYoYo");
}
}
//If using this destroy method, the bean from config file does not need destroy-method property!
//Processor Class
public class MyCustomBeanProcessor implements BeanPostProcessor, BeanFactoryAware, DisposableBean {
private BeanFactory beanFactory;
private final List prototypeBeans = new LinkedList<>();
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // after start up, keep track of the prototype scoped beans.
// we will need to know who they are for later destruction
if (beanFactory.isPrototype(beanName)) {
synchronized (prototypeBeans) {
prototypeBeans.add(bean);
}
}
return bean;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public void destroy() throws Exception { // loop through the prototype beans and call the destroy() method on each one
synchronized (prototypeBeans) { for (Object bean : prototypeBeans) {
if (bean instanceof DisposableBean) {
DisposableBean disposable = (DisposableBean) bean;
try {
disposable.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
prototypeBeans.clear();
}
}}
Ce inseamna Singleton ?
Singleton inseamna ca putem folosi bean-ul de mai multe ori in aplicatie insa tot timpul se va face referire catre acelasi bean, adica aceeasi locatie in memorie.
Care sunt Spring Bean Scopes ?
singleton - create a single shared instance of the bean. default scope
prototype - creates a new bean instance for each container request
request - scoped to an HTTP web request. only used for web apps
session - scoped to an HTTP web session. Only used for web apps.
global-session - scoped to a global HTTP web session. only used for web apps
Coach theCoach = new Coach(); Daca printam obiectul asa:
System.out.println(theCoach);
rezultatul va fii asemanator:
Coach@523567, ce reprezinta numarul de dupa @ ?
Reprezinta locatia din memorie a obiectului.
Care este diferenta dintre scope singleton si scope prototype attribute ?
Diferenta dintre scope=”prototype” si scope=”singleton” este ca: