Skip to content

Spring Beans

Yash edited this page Aug 24, 2018 · 3 revisions

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

Bean definition contains the information called configuration metadata, which is needed for the container to know the following −

  • How to create a bean
  • Bean's lifecycle details
  • Bean's dependencies

Spring Configuration Metadata

Spring IoC container is totally decoupled from the format in which this configuration metadata is actually written. Following are the three important methods to provide configuration metadata to the Spring Container −

  • XML based configuration file.
  • Annotation-based configuration spring 3.0
  • Java-based configuration YouTube

XML based configuration file.

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- A simple bean definition -->
   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with initialization, destruction method and lazy init set on -->
   <bean id = "..." class = "..." init-method = "..." destroy-method = "..." lazy-init = "true">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions go here -->
   
</beans>

Annotation-based configuration Started from Spring 2.5, guru.com


It contains web.xml file but we will configure all the configuration using annotations. JavaConfigWebApplicationContext

// Java Configuration
@Configuration
@EnableWebMvc
@ComponentScan(value = {"com.github.yash777"})
@EnableAutoConfiguration
@Deprecated
public class MvcConfig_ApplicationContext extends WebMvcConfigurerAdapter {
/*
Since 3.2
AbstractAnnotationConfigDispatcherServletInitializer extends
AbstractDispatcherServletInitializer extends
AbstractContextLoaderInitializer implements WebApplicationInitializer
*/
}

// Dispatcher Servlet
public class MyFrontController_DispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Deprecated
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { MvcConfig_ApplicationContext.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

Spring Boot Java-based configuration

To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory. By default, the bean name will be the same as the method name

public class Foo {
    public void init() {
        // initialization logic
    }
}
public class Bar {
    public void cleanup() {
        // destruction logic
    }
}
@Configuration
public class AppConfig {
    @Bean(initMethodName="init")
    public Foo foo() {
        return new Foo();
    }
    @Bean(destroyMethodName="cleanup")
    public Bar bar() {
        return new Bar();
    }
    @Bean(scope=DefaultScopes.PROTOTYPE)
    public Encryptor encryptor() {
        // ...
    }
}

Clone this wiki locally