ZK+spring+hibernate+log4j的web.xml配置:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>root</param-value>
</context-param>
<!--Log4jConfigListener要在ContextLoaderListener前 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<description>Used to clean up when a session is destroyed</description>
<display-name>ZK Session Cleaner</display-name>
<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.mypro.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.zul</url-pattern>
</filter-mapping>
<servlet>
<description>ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet>
<description>The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/*</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.zul</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.zul</location>
</error-page>
<!-- MIME mapping -->
……
如果在web.xml中未指明spring的配置配置文件名,默认在WEB-INF目录下查找applicationContext.xml。下面包含property、spring、hibernate、cxf的配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--IoC自动扫描带@Controller、@Service、@Repository、@Resource、@Transactional等注释的类-->
<context:component-scan base-package="com.pro.control,com.pro.service.impl,com.pro.dao.hibernate.impl" />
<!--配置属性文件的存放地址-->
<context:property-placeholder location="WEB-INF/config.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" destroy-method="close">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.validate">true</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.timeout">120</prop>
<prop key="hibernate.c3p0.max_statements">100</prop>
<prop key="hibernate.c3p0.idle_test_period">120</prop>
<prop key="hibernate.c3p0.acquire_increment">2</prop>
<prop key="connection.useUnicode">true</prop>
<prop key="connection.characterEncoding">UTF-8</prop>
</props>
</property>
<!--自动扫描hibernate实体:带@Entity注释-->
<property name="packagesToScan" value="com.pro.dao.hibernate.vo" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置cxf服务器的访问地址:http://192.168.1.120:8000/ -->
<bean id="endpoint" class="com.pro.CxfInterface"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.pro.CxfInterface" />
<property name="address" value="http://192.168.1.120:8000/" />
</bean>
</beans>
Ioc控制的Bean默认是单例,如果要设置非单例在类前注明如下:
@Controller("employmentCtrl")
@Scope("prototype")
public class EmploymentCtrl{
@Resource
private EmploymentService employService;
......
}
业务类:
@Service("employService")
public class EmploymentServiceImpl implements EmploymentService {
@Resource
private EmploymentDao employDao;
@Override
@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class, timeout = 30)
public void addOrEditEmployment{......}
hibernate实体类例子如下(oracle为例,也可设置为native方便移植,属性与表的字段一致即可):
@Entity
@Table(name = "employment")
public class Employment implements Serializable {
private static final long serialVersionUID = 6678401113364404864L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
@SequenceGenerator(name = "emp_seq", sequenceName = "seq_emp", allocationSize = 1)
private long id;
......
log4j有两种文件配置方式:log4j.xml和.property配置文件,默认放置在WEB-INF/下面,以log4j.xml为例配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d [%c.%M()] - %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${root}logs/log.log" />
<param name="Threshold" value="DEBUG" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d [%c.%M] - %m%n"/>
</layout>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<logger name="org.springframework">
<level value="WARN"></level>
</logger>
<logger name="java.sql">
<level value="INFO"/>
</logger>
<logger name="com.mypro">
<level value="DEBUG"/>
</logger>
<root>
<level value="WARN"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
log4j的日志格式(Layout)说明如下:
符号 描述
-------------------------------------------
%d 当前时间
%r 自程序开始后消耗的毫秒数
%t 表示日志记录请求生成的线程
%p 表示日志语句的优先级
%r 与日志请求相关的类别名称
%c 日志信息所在的类名
%M 日志信息所在类的方法
%L 日志信息所在方法的行数
%m%n 表示日志信息的内容