How spring is managing Hibernate and struts (i)

Source: Internet
Author: User

The project is finished and summed up. The project is implemented using SSH technology. Here's how spring manages Struts2 and hibernate.

One:

Let's talk about how spring works. When Tomcat starts, it will load two words in Web. XML,

Web. XML code
    1. <context-param>
    2. <param-name>contextConfigLocation</param-name>
    3. <param-value>classpath:applicationContext.xml</param-value>
    4. </context-param>

Java code
    1. <listener>
    2. <listener-class>
    3. Org.springframework.web.context.ContextLoaderListner
    4. </listener-class>
    5. </listener>

Here, Contextloaderlistener inherits the Servletcontextlistener. As a result, spring will Contextconfiglocation refers to the applicationcontext.xml automatically assemble the Bean object used in the XML file.

In Applicationcontext.xml, the bean is defined as follows

Applicationcontext.xml Code
    1. <bean id="user" class="Com.test.User" abstract="false" parent="People" scope="prototype" >
    2. <property name="dept" ref="dept" ></property>
    3. </bean>

When a special character such as a Slash "/" is required in the ID, transfer the ID to the equivalent of name. Another special note is that the value of the ID and name here must be unique.

Two: How spring manages Hibernate. And spring-managed transactions.

After spring loads, the instance is assembled according to the configuration in the Applicationcontext.xml file.

Java code
    1. <bean name="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    2. <property name="configlocation" value="Classpath:hibernate.cfg.xml" ></property>
    3. </bean>

He will load hibernate configuration information according to Hibernate.cfg.xml configuration. and instantiate sessionfactory. And if DAO is to be managed by spring, then this DAO class needs to inherit Hibernatedaosupport.

Spring provides transaction management in two ways, programmatic and statement-type. Programming cumbersome, I generally do not like to use. One of the favorites is that it's convenient to use AOP to manage transactions using the spring-encapsulated ASPECTJ.

First create a transaction manager

Java code
    1. <bean id="TransactionManager" class=" Org.springframework.orm.hibernate3.HibernateTransactionManager ">
    2. <property name="sessionfactory" ref="sessionfactory" >
    3. </property>
    4. </bean>

Declares a notification and declares that this notification is managed by one of the transaction managers

Java code
    1. <tx:advice id="Txadvice" transaction-manager="TransactionManager" >
    2. <tx:attributes>
    3. <tx:method name="save*"/>
    4. <tx:method name="*" readonly="true"/>
    5. </tx:attributes>
    6. </tx:advice>

The above paragraph illustrates the way Txadvice is managed by the TransactionManager transaction manager and manages the beginning of save. The rest does not use transaction management.

Finally, the advisor is used to configure the advice entry point.

Java code
    1. <aop:config>
    2. <aop:advisor advice-ref="Txadvice" pointcut="Execution (pulic * *). *.service.*.* (..)) " >
    3. </aop:advisor>
    4. </aop:config>

In this way, transaction management is entered as long as the service layer is passed.

How spring is managing Hibernate and struts (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.