Giter Site home page Giter Site logo

spring-lab's Introduction

Spring Lab

Beans

Bean Scopes

  • Singleton: (default) @Scope("singleton"), single instance for each Spring container and bean id;
  • Prototype: @Scope("prototype") - creates a new instance every time the bean is requested. Unlike other scopes, destruction callback methods are not called by Spring on prototype beans, as they are created by the Spring container and handed to the application, which is then responsible for any needed cleanups.
  • Request (web-aware): @RequestScope - creates a new instance for each HTTP request;
  • Session (web-aware): @SessionScope - creates a new instance for each HTTP session;
  • Application (web-aware): @ApplicationScope - creates a single instance for each ServletContext (not for each ApplicationContext as the singleton bean scope);
  • WebSocket (web-aware): @Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS) - creates a new instance for each WebSocket session.

Bean lifecycle

A Spring bean lifecycle can be divided into creation and destruction. We can customise the execution of these phases by implementing hook methods through annotations or interfaces.

Most commonly, this is done by the JSR-250 annotations:

  • @PostConstruct: called after all injections have happened;
  • @PreDestroy: called when the bean reached the end of its scope, right before destruction.

Additionally, you can hook into the bean lifecycle through:

  • Providing a custom init() and destroy() implementation and setting them to be called from the bean definition class.
  • Implementing *Aware interfaces;
  • Implementing the InitializingBean and DisposableBean interfaces;

Bean Creation

  1. Instantiation
  2. Populate properties
  3. Pre-initialisation
  4. After properties set
  5. Custom initialisation = @PostConstruct
  6. Post-initialisation
  7. Bean ready

Bean Destruction

  1. Pre-destroy = @PreDestroy
  2. Destroy
  3. Custom destruction
  4. Bean destroyed

spring-lab's People

Contributors

bgasparotto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.