Coder Social home page Coder Social logo

spring-ioc-demo's Introduction

Spring IoC

Spring IoC

This is a simple project to demonstrate the Spring IoC container, using XML configuration and Java annotations.

  • @Component: This annotation is used to mark a class as a Spring bean. The IoC container will automatically detect and register the bean.
      @Component("beanName")
      public class BeanType {
          // Bean code
      }
  • @Autowired: This annotation is used to inject a bean into another bean.
        @Autowired
        private BeanType bean;
    If we have:
    • Single Constructor: If your bean class has only one constructor, then starting from Spring 4.3, you can omit @Autowired. Spring will automatically use that constructor for dependency injection.
    • Multiple Constructors: If there are multiple constructors, at least one must be annotated with @Autowired to clarify which one Spring should use for injection.
  • @Resource: This annotation is used to inject a bean into another bean. It is similar to @Autowired, but it is more powerful and can be used to inject beans by name.
  • When we have multiple beans of the same type, we can use the @Qualifier annotation to specify which bean we want to inject.
    @Autowired
    @Qualifier("beanName")
    private BeanType bean;
  • @Value: This annotation is used to inject a value into a bean.
    @Value("value")
    private String value;
  • @Configuration: This annotation is used to specify a class as a configuration class.
      @Configuration
      public class AppConfig {
          @Bean
          public BeanType bean() {
              return new BeanType();
          }
      }
  • @PostConstruct: This annotation is used to specify a method that should be executed after the bean has been initialized.
      @PostConstruct
      public void init() {
          // Initialization code
      }
  • @PreDestroy: This annotation is used to specify a method that should be executed before the bean is destroyed.
        @PreDestroy
        public void destroy() {
            // Cleanup code
        }

spring-ioc-demo's People

Contributors

hmzelidrissi 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.