Spring (iv) Manually consolidating Web projects (SSH)

Source: Internet
Author: User

Clear Spring's IOC and AOP, the last one to integrate the SSH framework, recorded, should be used later.

--wh

How do I use Spring in a Web project?

When Tomcat starts, it should load the spring configuration file instead of manually using new Classpathxmlapplicationcontext to load the XML every time. So, Spring provides a contextloaderlistener. with it, when Tomcat starts, the configuration file is loaded.

Import Spring.web....jar

          

1. Add listener in Web. xml

          

View Code

This configuration of the listener, the default load is the XML file in the Web-inf directory, and our configuration file under SRC, so we need to configure the next step.

2. Configure Applicationcontext.xml location in Web. xml

          

View Code

3. Get the spring container from ServletContext, and the container created is already in ServletContext.

            

View Code

          

Second, the SSH integration of the jar package used

2.1. STRUTS2 Required Jar

Version: 2.3.15.1

Location: Struts-2.3.15.1\apps\struts2-blank\web-inf\lib

            

Total: 13 jar Packages

2.2. Hibernate

Version: 3.6.10

The required jar

1. Core Jar:hibernate3.jar

                  

2, must: lib/required all

                  

3, JPA:LIB\JPA all

                  

4, C3p0 \lib\optional\c3p0

                  

5. log4j Integrated jar Slf4j-log4j12-1.7.2.jar

                  

6. mysql Driver pack Mysql-connector-java-5.1.22-bin.jar

                   

7, two level cache Ehcache-1.5.0.jar, Backport-util-concurrent.jar originally to lead 3, logging package in front of the struts package has, so do not need to import

                                  

Hibernate total: 13 jar Packages

2.3. Spring

Version: 3.2.0

Jar Package:

Core: 4 (core, beans, expression, context) + commons-logging. Jar (This jar package has been imported in many places before, Struts2, Hibernate's Level two cache, spring all need, only need to guide one copy)

                  

AOP:AOP Alliance, Spring AOP, AspectJ, Spring-aspect

AOP Alliance: spring-framework-3.0.2.release-dependencies\org.aopalliance\com.springsource.org.aopalliance\1.0.0

Spring AOP and Spring-aspect: in the core package

aspectj:spring-framework-3.0.2.release-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8. RELEASE

Jdbc:2 a SPRING-JDBC, spring-tx

Web:1 a Spring-web

Test:1 a spring-test (integration test)

Total: 12

2.4. Integrated jar

SPRING3 Integrated hibernate3,spring provides integrated Jar:spring-orm...jar

STRUTS2 Integrated SPRING3, struts provides integration Jar:struts2-spring-plugin-2.3.15.1.jar

Location: Struts-2.3.15.1\lib

Total: 2

        

2.5. Tidy jar

Get a total of 39 in front, remove a duplicate, the remaining total 38.

                

          

Iii. Configuration Files

3.1. Hibernate

Hibernate.cfg.xml Core configuration file

*.hbm.xml mapping file (domain)

3.2. Spring

Applicationcontext.xml

Jdbcinfo.properties

Web. XML (Contextloaderlistener)

3.3, Struts2

Struts.xml

Web. XML (front filter strutsprepareandexecutefilter)

      

Iv. Spring Integration Hibernate

4.1. First to understand hibernate's configuration file, and what Hibernate needs, and then how to let spring to manage.

Hibernate.cfg.xml

            

            

With these two graphs, you can see that spring needs to help hibernate get the session, and get the session first to get Sessionfactory, and get sessionfactory to load the configuration file. So spring needs to provide two things, a sessionfactory, a session. How does spring do that?

            

View Code

Use:

             

This way, you can delete the Hibernte.cfg.xml.

There is also a problem with the handling of the transaction.

Simple, the previous article explains how spring manages the transaction, using the JDBC Manager, now in a different way, using the Hibernate transaction manager

            

So when you've finished integrating Hibernate, Spring's configuration file is

View Code

Summary: In fact, it is very simple, the beginning of integration must have no clue, do not know where to start, then the first one of the simplest hibernate operation object to write out, see what Hibernate needs, and then spring to configure what, to what. This is simple and straightforward.

Five, STRUTS2 integration spring

5.1. Struts2 Create action yourself. Instead of spring to help us create

Struts.xml

                     

The creation of the UserService object in action requires spring to help us create

                  

Applicationcontext.xml

              

This way, it will be all right.

      

5.2, Spring help us create action,struts2 nothing to do. ‘

Prerequisites: Import Struts-spring-plugin...jar Required

          

All the applicationcontext.xml after integration

View Code

Vi. Summary

So here, the manual integration of SSH ended, in fact very simple, step by step, from the Guide package to integration of hibernate in the integration of the steps of struts, then you can successfully integrate, above all I did not use interface programming, in order to facilitate, pay attention to this point in the future. Very simple.

But in the integration of hibernate, we have your domain map files are written manually, the actual development is certainly not necessary to manually write, because there is a database, and then write your domain, So then we'll talk about creating domain and mapping files based on the tables in the database by reversing the engine.

        

Spring (iv) Manually consolidating Web projects (SSH)

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.