Three major work flows of SSH frameworks and three major work flows of ssh
I. Struts2 workflow:
1. When a user initiates a request on the client, the client initializes a servlet container request;
2. The servlet container passes the request to the context container, and the context container finds the target web project.
3. parse the configuration in the struts tag in web. xml:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4. Then implement a series of struts filters, such as ActionContextClearUp;
5. FilterDispatcher will be called. Before it is forwarded to a certain Actio, you must first ask
ActionMapper (it determines whether an Action is to be executed );
6. If yes, FilterDispatch will send the request to our ActionProxy (Action proxy );
7. Then, ActionProxy queries the framework configuration through Configure Manager and finds the action configuration information.
Instance Action object;
8. Execute the Action object method and return the result;
9. Jump to the specific processing page or another Action in struts. xml based on the returned result;
10. Respond to our users.
Ii. Hibernate workflow:
1. When we use myeclipse to add the hibernate component, this component will automatically generate the HibernateSessionFactory class and
Hibernate. cfg. xml file;
2. In hibernate. cfg. xml, sessionFatory is configured mainly, including the connection information of the database and cache information;
<session-factory> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.url"> jdbc:oracle:thin:@127.0.0.1:1521:orcl </property> <property name="connection.username">system</property> <property name="connection.password">java</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="myeclipse.connection.profile">oeconn</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="cache.provider_class"> org.hibernate.cache.EhCacheProvider </property></session-factory>
3. Who will read and parse such an important configuration file? Of course it is HibernateSessionFactory.
There is a static code block in this class:
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration(); private static ServiceRegistry serviceRegistry; static { try { configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } }
In this static code block: Use the configure method of the Configuration class to load and parse hibernate. cfg. xml.
SessionFactory object (which is used to create the session object), so the code of this segment will be run when the tomcat server is started;
Here we will mention the session creation process:
1) Why should the session be placed in ThreadLocal <Session>?
Although our template class obtains database connections or session objects through the resource pool, the template class needs
Database connections and session resources are bound. However, these resources are non-thread-safe, that is, they cannot be shared by multiple threads.
Hibernate can solve thread security issues without synchronization, so it places the session in ThreadLocal.
2) create a session for a single instance through SessionFatory
public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; }
4. Create the transaction object session. BeginTransaction Method
5. Create a persistent object session. createQuery Method
6. Submit the transaction
7. Disable session and sessionFactory.
3. Spring workflow:
1. Add the Spring component to myeclipse to generate an applicationContext. xml file.
2. Add the following configuration to web. xml:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
The web project enters the web. xml scan and finds the ContextLoaderListener class and listener, which indicates that there is a Spring component.
Find the classpath value in the <context-param> tag and go to applicationContext. xml;
The Data Source Information (database configuration information) is added to the beans tab ).
<! -- Add data source --> <bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "> <property name =" driverClassName "value =" oracle. jdbc. driver. oracleDriver "> </property> <property name =" url "value =" jdbc: oracle: thin :@ localhost: 1521: orcl "> </property> <property name =" username "value =" empdb "> </property> <property name =" password "value =" java "> </property> </bean>
3. Control reverse injection of bean through Ioc. The object does not need to be new
, Spring will help you complete it and hand it over to him for hosting.
<! -- Inject the JdbcTemplate class --> <bean id = "jdbctemp" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" dataSource "> </property> </bean> <! -- Inject dao class --> <bean id = "bdao" class = "com. dao. bankDao "> <property name =" jtemp "ref =" jdbctemp "> </property> </bean>