I am new to the Spring Framework. Please guide me regarding basic understanding of Spring. I know Java and JSF, but I don't know anything about Struts or other frameworks. I have searched the Internet and was able to download a PDF, but I don't understand the Spring flow like I understand JSF flow. Please provide me with links for a simple way to understand Spring's flow.
I'm new to Spring Framework too. Up to now, the document below is the most basic one. I'm reading it as well, hope it could help you.
- Transition 1 – User sends request to server by submitting form / by clicking hyperlink etc. Request is initially given to WEB.XML.
- Transition 2 – WEB.XML routes request to DispatcherServlet by looking at tag.
- Transition 3 – Inside DispatcherServlet, First ‘HandlerMapping’ handed over request to suitable ‘Controller’.
- Transition 4 – Controller maps request to proper Model class. All BUSINESS LOGIC is done inside Model class.
- Transition 5 – If database operation is needed then Model class will route request to suitable DAO. All database operations should be carried out in DAO.
- Transition6 – If needed then attach attributes into request/session/application scope and return back to Model.
- Transition 7 – If needed then attach attributes into request/session/application scope and return back to Controller.
- Transition 8 – Controller simply returns it to any View (JSP/HTML etc).
- Transition 9 – JSP/Html is viewed back to user.
I'm new to Spring Framework too. Up to now, the document below is the most basic one. I'm reading it as well, hope it could help you.
I am new to Spring too and time ago had similar question. First of all I want to recommend you 'Spring in Action' book by Craig Walls, I found it very usefull and easy to understand, also http://www.tutorialspoint.com/spring/ helped me to figure many things out. If I understood your question right, then we can divide “Spring's flow” into Spring IoC container's and Spring bean's life cycles. Here is very small overview with exapmle on Spring bean's life cycle. A bean goes through several steps between creation and destruction in the Spring container. These steps are:
- Instantiate
- Populate properties
- BeanNameAware`s setBeanName()
- BeanFactoryAware`s setBeanFactory
- ApplicationContextAware`s setApplicationContext()
- Pre-initialization BeanPostProcessors
- InitializingBean`s afterPropertiesSet()
- Call custom init-method
- Post-initialization BeanPostProcessors
- DisponsableBean`s destroy
- Call custom destroy-method
Each step provides own oportunities for customization. Here is some code which simply “traces” bean`s life:
For bean ClassA:
public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}
For CustomPostProcessor:
public class CustomPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: beforeInitialization on: "
+ beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: afterInitialization on: "
+ beanName);
return bean;
}
}
In main class we creating ApplicationContext, getting bean and printing message out:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"META_INF/spring/app-context.xml");
ClassA objA = (ClassA) context.getBean("classA");
System.out.println(objA.getMessageA());
context.registerShutdownHook();
}
In app-context.xml we have:
<bean id="classA" class="ClassA" init-method="customInitMethod"
destroy-method="customDestroyMethod">
<property name="messageA" value="messagA: Hello Spring!" />
</bean>
<bean class="CustomPostProcessor" />
As I understand output lines correspond to life cycle stages this way:
- 1.Instantiate
ClassA: default constructor called.
- 2.Populate properties
ClassA: message set method called.
- 3.BeanNameAware`s setBeanName()
ClassA: bean name set: classA
- 4.BeanFactoryAware`s setBeanFactory
ClassA: beanFacrory set.
- 5.ApplicationContextAware`s setApplicationContext()
ClassA: application context set:
- 6.Pre-initialization BeanPostProcessors
CustomPostProcessor: beforeInitialization on: classA
- 7.InitializingBean`s afterPropertiesSet()
ClassA: afterPropertiesSet() called because InitializingBean interface.
- 8.Call custom init-method
ClassA: customInitMethod() method called.
- 9.Post-initialization BeanPostProcessors
CustomPostProcessor: afterInitialization on: classA
- Program prints message
ClassA: message get method called. messagA: Hello Spring!
- 10.DisponsableBean`s destroy
ClassA: destroy() called because DisposableBean interface.
- 11.Call custom destroy-method
ClassA: customDestroyMethod() method called.
© 2022 - 2024 — McMap. All rights reserved.