1annotation inversion of control, bean scope

https://www.udemy.com/spring-hibernate-tutorial/learn/v4/t/lecture/5232898?start=0

  • Bean Scope

4. Bean Scope

  • Default Scope: Singleton

4.1 What is a Singleton?

  • Spring Container creates only one instance of the bean, by default

  • It is cached in memory

  • All requests for the bean will return a SHARED reference to the SAME bean

e.g.

<bean id= "myCoach" class = "TrackCoach" scope="singleton"></bean>
  • You can add custom code during bean initialization

    • Calling custom business logic methods

    • Setting up handles to resources(db, sockets, file etc)

  • You can add custom code during bean destruction

    • Calling custom business logic method

    • Clean up handles to resources(db, sockets, file etc)

5.1.1 Init: method configuration

<bean id="myCoach" class = "com.TrackCoach" init-method="doMyStartupStuff">...</bean>
  • Any method name. Must be "public void".

5.1.2 Destroy: method configuration

<bean id="myCoach" class = "com.TrackCoach" init-method="doMyStartupStuff" destrop-method = "doMyCleanupStuff">
...
</bean>
  • Any method name. Must be "public void".

5.1.3 Development process

  1. Define your methods for init and destrop

  2. Configure the method names in Spring config file

5.1.4 note

Defining init and destroy methods

When you are defining init and destroy methods should follow these rules:

  • The methods should be public void.

  • The methods should be no-arg, meaning they shouldn't accept any method arguments.

For "prototype" scoped beans, Spring does not call the destroy method. Gasp!

In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance.

Thus, although initialization lifecycle callback methods are called on all objects regardless of scope,in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.

This also applies to both XML configuration and Annotation-based configuration.

6. Spring configuration with Annotations--inversion of control

What are Java Annotations?

  • Special labels/markers added to Java Classes

  • Provide meta-data about the class

  • Processed at compile time or run-time for special processing

Why Spring Configuration with Annotation?

  • XML configuration can be verbose

  • Configure your Spring beans with Annotations

  • Annotations minimizes the XML configuration

6.1 Scanning for Component Classes

  • Spring will scan your Java classes for special annotations

  • Automatically register the beans in the Spring container

6.2 Development Process

  1. Enable component scanning in Spring config file

  2. Add the @Component Annotation to your Java classes

  3. Retrieve bean from Spring container

Step1: Enable component scanning in Spring config file

e.g.

<beans...>
<context: component-scan base-package="com.springdemo" />
</beans>

Step2:

e.g.

Coach theCoach = context.getBean("thatSillyCoach", Coach.class);
  • Default bean id: the class name, make first letter lower-case.

Last updated