Section 6 Flashcards

1
Q

Create a application using singleton property and prototype property.

Create a boolean variable that will verify if the classes are singleton or not

A

//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();
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exista doua metode apeleaza o data cu clasa spring bean, acestea sunt ?

A

init si destroy

init-method
destroy-method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Creati un exemplu de init si destroy methods

A

//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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When the destroy method is not called ?

A

when using scope=prototype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you destroy prototype beans ?

A
//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();
    }

}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ce inseamna Singleton ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Care sunt Spring Bean Scopes ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Coach theCoach = new Coach();
Daca printam obiectul asa:

System.out.println(theCoach);

rezultatul va fii asemanator:
Coach@523567, ce reprezinta numarul de dupa @ ?

A

Reprezinta locatia din memorie a obiectului.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Care este diferenta dintre scope singleton si scope prototype attribute ?

A

Diferenta dintre scope=”prototype” si scope=”singleton” este ca:

  • Daca se foloseste valoarea singleton atunci cand se va apela obiectul BaseballCoach, se va accesa tot timpul aceeasi locatie in memorie deoarece se va crea un singur obiect care va putea fi accesat de mai multe ori din aplicatie.
  • Daca se foloseste valoarea prototype atunci cand se va apela obiectul BaseballCoach, se va accesa tot timpul alta locatie in memorie deoarece se va creea cate un obiect diferit de fiecare data cand se instantiaza obiectul BaseballCoach.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly