Preliminary understanding of Spring framework construction in SSH framework (i)

Source: Internet
Author: User

It has been over one months since we took over the SSH framework-based Web project. Have heard Javaweb three big frame, first contact, first to feel.

I feel the most obvious advantages of the SSH framework are as follows:

    1. With the MVC pattern, the hierarchy is clear and the programmer only needs to focus on the implementation of the business logic.
    2. Through the configuration file, you can master the relationship between the various parts of the system.
    3. With AOP, transaction management and log management can be implemented.

The spring framework allows you to solve the problem in the simplest possible way, which is very efficient. But it's a little more complicated to build, especially for beginners like me, so write this document about the Spring framework:

    • Create a Web project, import the SSH base jar package, and add the following code to XML

  

1    <listener>  2         class>org.springframework.web.context.contextloaderlistener </listener-class>  3     </listener>

With this listener, the spring framework can be loaded, and struts2 is also managed by spring.

    • Open the Applicationcontext.xml text for extended configuration and management configuration, so we recommend adding code in the following ways
1     <import resource= "Config/applicationcontext.xml"/>23     <import Resource= "Servicebean/applicationcontext*.xml"/>  4     5     <import resource= "Databasebean/applicationcontext*.xml"/>67     <import Resource= "Actionbean/applicationcontext*.xml"/>

The above code is not difficult to understand, is the configuration, service, database, action management, note under Web-inf to create the corresponding folder and the corresponding configuration file can be, below we look at

The combination of spring and hibernate.

    • The _applicationcontext-database.xml configuration is as follows:
  1  <bean id= "DataSource" class< /span>= "Org.apache.commons.dbcp.BasicDataSource" 2  destroy-method= "Close" >3  <property name= "Driverclassname" value= "${XIANRUI.DATABASE.DRIVERCL Assname} "/>4  <property name=" url "value=" Jdbc:${xianrui.database.type} ://${xianrui.database.address}:${xianrui.database.port}/${xianrui.database.name}?${xianrui.database.encode} "/ >5  <property name= "username" value= "${xianrui.database.username}"/> 6  <property name= "password" value= "${xianrui.database.password}"/>
     
      7 
      8  </bean>  

Main configuration Four: driver name, URL, user name and password value with the company configuration values, according to the actual situation to modify into their own good ~

    • The Applicationcontext-hibernate.xml configuration is as follows:
1<bean id= "Sessionfactory"2         class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >3<property name= "DataSource" >4<ref bean= "DataSource"/>5</property>6<property name= "Mappinglocations" >7<list>8<value>classpath:com/xsq/model/**/*.hbm.xml</value>9 </list>Ten </property> One <property name= "Hibernateproperties" > A <props> - <prop key= "Hibernate.dialect" >${xianrui.hibernate.dialect}</prop> - </props> the </property> - </bean> -  - <!--Configure the transaction manager Bean, using the Hibernatetransactionmanager transaction Manager-- + <bean id= "TransactionManager" - class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" > + <property name= "Sessionfactory" > A <ref bean= "Sessionfactory"/> at </property> - <property name= "DataSource" > - <ref bean= "DataSource"/> - </property> - </bean> -  in <!--use annotation to define transactions -- - <tx:annotation-driven transaction-manager= "TransactionManager" to proxy-target-class= "true"/> +  -  the <bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" > * <property name= "DataSource" > $ <ref bean= "DataSource"/>Panax Notoginseng </property> - </bean> the  + <bean id= "hibernatetemplate" class= "Com.xsq.core.extend.HibernateTemplate" > A <property name= "Sessionfactory" > the <ref bean= "Sessionfactory"/> + </property> - <property name= "Cachequeries" value= "false" ></property> $ <!--set Hibernate to automatically refresh update data Flush_eager $ <property name= "Flushmode" value= "2" ></property> - </bean> -  the <bean id= "Hibernatetemplatecache" class= "Com.xsq.core.extend.HibernateTemplate" > - <property name= "Sessionfactory" >Wuyi <ref bean= "Sessionfactory"/> the </property> - <property name= "Cachequeries" value= "true" ></property> Wu <!--set Hibernate to automatically refresh update data Flush_eager - <property name= "Flushmode" value= "2" ></property> About </bean> $  - <!--Configuring the transaction Interceptor Bean -- - <bean id= "Transactioninterceptor" - class= "Org.springframework.transaction.interceptor.TransactionInterceptor" > A <!--inject a thing manager into a transaction interceptor bean-- + <property name= "TransactionManager" ref= "TransactionManager" ></property> the <property name= "Transactionattributes" > - <!--define transaction propagation properties -- $ <props> the <prop key= "add*" >PROPAGATION_REQUIRED</prop> the <prop key= "save*" >PROPAGATION_REQUIRED</prop> the <prop key= "insert*" >PROPAGATION_REQUIRED</prop> the  - <prop key= "remove*" >PROPAGATION_REQUIRED</prop> in <prop key= "delete*" >PROPAGATION_REQUIRED</prop> the  the  About </props> the </property> the </bean>
View Code

Look closely at the code, with a lot of wildcard * configuration, so in the development process of the name of the class method to pay special attention to, there is actually the model class configuration file: Class name. hbm.xml.

By the way, the configuration of Applicationco_service and Applicationcontext-dao.xml is as follows:

    class= "Com.xsq.service.impl.AccountManagerServiceImpl" >        <property name= "Accountmanagerdao" ref= "Accountmanagerdao" ></property>    </bean>
     class= "Com.xsq.dao.AccountManagerDao" >        <property name= "hibernatetemplate" ref= " Hibernatetemplate "></property>    </bean>

Very simple, a look will understand = =!

    • Finally, how does the spring framework combine with STRUTS2, where the main thing is to delegate the struts ' action management to the spring framework

The configuration of the Applicationcontext-action.xml is as follows:

   class= "Com.xsq.web.sysmgr.AnnounceAction" scope= "prototype" >        <property name= "Announceservice" ref= "Announceservice" ></property>    </bean>

Changing the class in the Struts.xml configuration to the bean ID name is good ~

At this point, a basic SSH framework is built, here only shows the spring framework, the other two framework of the basic configuration is still to be built, in this is not elaborate!

Preliminary understanding of Spring framework construction in SSH framework (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.