-
Notifications
You must be signed in to change notification settings - Fork 0
Spring Beans
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 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 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
Spring Java-based container configuration StackPost, Interface WebApplicationInitializer
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() {
// ...
}
}Spring Boot is a project that is built on the top of the Spring Framework. It provides an easier and faster way to set up, configure, and run both simple and web-based applications. It is a Spring module that provides the RAD (Rapid Application Development) feature to the Spring Framework.