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
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
Java code
- <listener>
- <listener-class>
- Org.springframework.web.context.ContextLoaderListner
- </listener-class>
- </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
- <bean id="user" class="Com.test.User" abstract="false" parent="People" scope="prototype" >
- <property name="dept" ref="dept" ></property>
- </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
- <bean name="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- <property name="configlocation" value="Classpath:hibernate.cfg.xml" ></property>
- </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
- <bean id="TransactionManager" class=" Org.springframework.orm.hibernate3.HibernateTransactionManager ">
- <property name="sessionfactory" ref="sessionfactory" >
- </property>
- </bean>
Declares a notification and declares that this notification is managed by one of the transaction managers
Java code
- <tx:advice id="Txadvice" transaction-manager="TransactionManager" >
- <tx:attributes>
- <tx:method name="save*"/>
- <tx:method name="*" readonly="true"/>
- </tx:attributes>
- </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
- <aop:config>
- <aop:advisor advice-ref="Txadvice" pointcut="Execution (pulic * *). *.service.*.* (..)) " >
- </aop:advisor>
- </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)