Summary of sping and SSH configuration __web development in Java Web development

Source: Internet
Author: User
Tags access properties aop java web log4j
1. The birth of spring

In order to solve the complex structure of large enterprise application, it involves many external resources, transaction density, large data scale, many users, strong security considerations and higher performance requirements. ------Rod Jonson (author of Spring)

2. Spring optimization

Spring is part of the integration framework, whose core is based on control reversals (inverse, the IOC), and Spring is a comprehensive solution: it advocates not reinventing the wheel.

3, the module that spring contains

Core, AOP, DAO, ORM, JEE, WEB.

4. Spring's core mechanism----dependency Injection

The so-called dependency injection is that when a role requires assistance from another role, it is usually the caller who creates the instance of the callee. In spring, the work of the creator invoking the instance is often done by the spring container and then injected to the caller.

For example, an object that requires service in action, we can use injection to implement the object's reference.

Private UserService UserService;

Public void setuserservice (UserService userservice) {

this. UserService = UserService;

}

5. log4j Logging-----log4j.properties

"Norms:

#ootLogger是所有日志的根志, modifying this log property will work on all logs

#有日志的输出级别info, Output source console

#log4j. Rootlogger=info,console

#定义输出位置是控制台

Log4j.appender.console=org.apache.log4j.consoleappender

#日志布局采用的类

Log4j.appender.console.layout=org.apache.log4j.patternlayout

#日志输出布局

log4j.appender.console.conversionpattern= [%p]%d{yyyy-mm-dd

HH:MM:SS}%m%l

"" Practical:

Log4j.rootlogger=debug,console,file

Log4j.appender.console=org.apache.log4j.consoleappender

Log4j.appender.console.layout=org.apache.log4j.patternlayout

log4j.appender.console.conversionpattern= [%p]%d{yyyy-mm-dd HH:mm:ss}%m%l

Log4j.appender.file=org.apache.log4j.fileappender

Log4j.appender.file.file=${log4j_root}/logs.file

Log4j.appender.file.layout=org.apache.log4j.patternlayout

Log4j.appender.file.layout.conversionpattern=[%p]%d{yyyy-mm-dd HH:mm:ss}%m%r%n

6, SSH integration

(1) Construction of SSH framework

Steps:

1, add all the integrated jar package.

2, add order struts2,hibernate3,spring2.5 or Spring3.0.

3, respectively, the configuration of Web.xml, Hibernate, Dao.xml, Srvice.xml, Action.xml and Applicationcontent.xml

Main configuration under Web.xml file:

<!--configuration Spring-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<listener-class>com.boonya.jboa.utils.Log4jListener</listener-class>

</listener>

<!--Configure character filter-->

<filter>

<filter-name>characterFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<!--configuration Osiv filter-->

<filter>

<filter-name>OSIVFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

</filter>

<!--configuration Struts2-->

<filter>

<filter-name>struts2</filter-name>

<filter-class>

Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>

</filter>

Hibernate.cfg.xml configuration:

<session-factory>

<property name= "Myeclipse.connection.profile" >JBOA</property>

<property name= "dialect" >

Org.hibernate.dialect.Oracle9Dialect

</property>

<property name= "Show_sql" >true</property>

<mapping resource= "Com/boonya/jboa/entity/claimvoucher.hbm.xml"/>

<mapping resource= "Com/boonya/jboa/entity/checkresult.hbm.xml"/>

<mapping resource= "Com/boonya/jboa/entity/position.hbm.xml"/>

<mapping resource= "Com/boonya/jboa/entity/department.hbm.xml"/>

<mapping resource= "Com/boonya/jboa/entity/claimvoucherdetail.hbm.xml"/>

<mapping resource= "Com/boonya/jboa/entity/employee.hbm.xml"/>

</session-factory>

Database access Properties File: Db.properties

Driver=oracle.jdbc.driver.oracledriver

Url=jdbc:oracle:thin: @localhost: 1521:ORCL

User=jboa

Password=jboa

Dao.xml configuration under Config folder:

<!--configuration Data source-->

<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >

<property name= "Driverclassname" value= "${driver}" ></property>

<property name= "url" value= "${url}" ></property>

<property name= "username" value= "${user}" ></property>

<property name= "Password" value= "${password}" ></property>

</bean>

<!--configuration Session factory-->

<bean id= "Sessionfactory"

class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

<property name= "DataSource" ref= "DataSource" ></property>

<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" >

</property>

</bean>

<!--Configure DAO base class-->

<bean id= "Basedao" abstract= "true" class= "Com.boonya.jboa.dao.BaseDao" >

<property name= "Sessionfactory" ref= "Sessionfactory" ></property>

</bean>

<!--******************** below for specific ******************-->

<bean id= "Claimvoucherdao" class= "Com.boonya.jboa.dao.ClaimVoucherDao" parent= "Basedao" ></bean>

<bean id= "Userdao" class= "Com.boonya.jboa.dao.UserDao" parent= "Basedao" ></bean>

Service.xml configuration under Config folder:

xmlns:p= "http://www.springframework.org/schema/p"

xmlns:tx= "Http://www.springframework.org/schema/tx"

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

Xsi:schemalocation= "

Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

Http://www.springframework.org/schema/tx

Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

">

<!--transaction configuration-->

<bean id= "Txmanager"

class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >

<property name= "Sessionfactory" ref= "Sessionfactory" ></property>

</bean>

<!--Advice-->

<tx:advice transaction-manager= "Txmanager" id= "Serviceadvice" >

<tx:attributes>

<tx:method name= "*" propagation= "REQUIRED"/>

<tx:method name= "get*" read-only= "true"/>

<tx:method name= "find*" read-only= "true"/>

</tx:attributes>

</tx:advice>

<!--AOP-->

<aop:config>

<aop:pointcut expression= "Execution (* com.boonya.jboa.service. *.*(..))"

Id= "Servicemethod"/>

<aop:advisor advice-ref= "Serviceadvice" pointcut-ref= "Servicemethod"/>

</aop:config>

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<bean id= "Baseservice" class= "Com.boonya.jboa.service.BaseService" abstract= "true" ></bean>

<bean id= "Claimvoucherservice" class= "Com.boonya.jboa.service.ClaimVoucherService" parent= "Baseservice" >

<property name= "Claimvoucherdao" ref= "Claimvoucherdao" ></property>

</bean>

<bean id= "UserService" class= "Com.boonya.jboa.service.UserService" parent= "Baseservice" >

<property name= "Userdao" ref= "Userdao" ></property>

</bean>

Action.xml configuration under Config folder:

<!--paging Bean-->

<bean id= "Pagebean" class= "Com.boonya.jboa.utils.PaginationSupport"

Scope= "Prototype" ></bean>

<!--Foundation Action-->

<bean id= "baseaction" class= "Com.boonya.jboa.action.BaseAction" abstract= "true" scope= "prototype" >

<property name= "Pagebean" ref= "Pagebean" ></property>

</bean>

<bean id= "claimvoucheraction" class= "com.boonya.jboa.action.ClaimVoucherAction" parent= "Baseaction" Prototype ">

<property name= "Claimvoucherservice" ref= "Claimvoucherservice" ></property>

</bean>

Write the appropriate action under Struts.xml:

<package name= "Default" namespace= "/" extends= "Struts-default" >

<action name= "user" class= "useraction" >

<result name= "Input" >/login.jsp</result>

<result name= "Success" >/success.jsp</result>

</action>

In the Applicationcontext.xml configuration, the injection object is introduced:

<!--configuration Properties file Read-->

<bean

class= "Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" >

<property name= "Locations" >

<list>

<value>classpath:com/boonya/jboa/config/db-config.properties</value>

</list>

</property>

</bean>

<!--Import sub-configuration file-->

<import resource= "Com/boonya/jboa/config/dao.xml"/>

<import resource= "Com/boonya/jboa/config/service.xml"/>

<import resource= "Com/boonya/jboa/config/action.xml"/>

Related Article

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.