Understanding Basic Spring Framework and Total Flow

I am new to Spring Framework. I ask you to inform us about the basic understanding of Spring. I know Java and JSF, but I don't know anything about Struts or other frameworks. I searched the Internet and was able to download the PDF file, but I do not understand the Spring stream, as I understand the JSF stream. Please provide me with links for an easy way to understand Spring Stream.

+7
source share
3 answers

I am new to Spring Framework too. So far, the document below is the simplest. I read it too, hope this can help you.

Introduction to Spring Framework

+3
source
  • Transition 1 - The user sends a request to the server by sending a form / clicking a hyperlink, etc. The request is initially provided by WEB.XML.
  • Transition 2 - WEB.XML directs the request to the DispatcherServlet, looking through the label.
  • Transition 3 - Inside DispatcherServlet, First 'HandlerMapping passed the request to the appropriate controller.
  • Transition 4 - The controller maps the request to the corresponding model class. All BUSINESS LOGIC runs inside the model class.
  • Transition 5 - If the database requires the Model to request a route to a suitable DAO. All database operations must be performed in the DAO.
  • Transition6 - If necessary, add attributes to the request / session / application scope and return to the model.
  • Transition 7 - If necessary, attach the attributes to the request / session / application area and return to the Controller.
  • Transition 8 - The controller simply returns it to any view (JSP / HTML, etc.).
  • Transition 9 - JSP / Html is viewed by the user.

Spring Application Stream MVC:

+7
source

I'm new to Spring too, and I had a similar question. First of all, I want to recommend Craig Walls 'Spring in Action' book, it was very useful and easy to understand for me, also http://www.tutorialspoint.com/spring/ helped me figure out many things. If I understood your question correctly, we could split the "Spring stream" into the Spring IoC container and Spring bean life cycles. Here is a very small overview with exapmle on the Spring bean life cycle. A bean goes through several steps between creating and destroying a Spring container. These steps are:

  • Instantiate
  • Fill Properties
  • BeanNameAware`s setBeanName ()
  • BeanFactoryAware`s setBeanFactory
  • ApplicationContextAware`s setApplicationContext ()
  • Initialization of BeanPostProcessors
  • InitializingBean`s afterPropertiesSet ()
  • Invoke a custom init method
  • After initialization of BeanPostProcessors
  • DisponsableBean`s destroy
  • Call custom destroy method

Each step provides its own customization options. Here is some code that simply "tracks" the 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 the main class, we create an ApplicationContext, get a bean and display a message:

 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 it, the output lines correspond to the stages of the life cycle in this way:

  • 1.Instantiate

ClassA: default constructor called.

  • 2. Buy properties

Class A: The message set method is called.

  • 3.BeanNameAware`s setBeanName ()

ClassA: bean set name: classA

  • 4.BeanFactoryAware`s setBeanFactory

ClassA: beanFacrory set.

  • 5.ApplicationContextAware`s setApplicationContext ()

ClassA: set of application contexts:

  • 6.Pre-initialization BeanPostProcessors

CustomPostProcessor: beforeInitialization on: classA

  • 7.InitializingBean`s afterPropertiesSet ()

ClassA: afterPropertiesSet () is called because the InitializingBean interface.

  • 8.Call custom init-method

The ClassA method is called: customInitMethod ().

  • 9.Post-initialization BeanPostProcessors

CustomPostProcessor: afterInitialization on: classA

  • The program prints a message

Class A: A method for receiving a message was called. messagA: Hello Spring!

  • 10.DisponsableBean`s destroy

ClassA: destroy () is called for the DisposableBean interface.

  • 11.Call custom destroy-method

ClassA: method called customDestroyMethod ().

+2
source

All Articles