1. 程式人生 > >java開發中常用的xml,properties配置

java開發中常用的xml,properties配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 監聽器:自動裝配xml檔案 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- xml檔案的位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml</param-value>
	</context-param>

	<!-- springMVC的入口-->
	<servlet>  
		<servlet-name>dispatcher</servlet-name>  
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
		<!-- 配置檔案的位置-->
		<init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:web/applicationContext-dispatcherServlet.xml</param-value>  
    </init-param> 
		<load-on-startup>1</load-on-startup>  
	</servlet>  
           
	<servlet-mapping>  
		<servlet-name>dispatcher</servlet-name>  
		<url-pattern>/**</url-pattern>  
	</servlet-mapping> 
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

  <!-- 該filter的作用:開啟session直到呼叫jsp頁面結束才關閉session 
  		注意:在配置時,此filter應該在struts入口(下面struts2的filter)的前面-->
	<filter>
		<filter-name>openSession</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<!-- 這個filter會預設找一個name為sessionFactory的bean,如果不叫這名字,如下配置個引數 -->
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>mySessionFactory</param-value>
		</init-param>
		
	</filter>
	
	<filter-mapping>
		<filter-name>openSession</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- struts2的filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>


==============================

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/jd1107db</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">create</property> -->

        <mapping class="com.cx.regist.entity.User" />

    </session-factory>

</hibernate-configuration>


 ===============================================================

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-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">


	<!-- 元件由註解註釋 如:@entity-->			
	<context:annotation-config />
	<context:component-scan base-package="com.cx.regist"></context:component-scan>




	<!--
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
		<property name="url" value="jdbc:mysql://localhost:3306/jd1107db" /> 
		<property name="username" value="root" /> 
		<property name="password" value="1234"/> 
		</bean>
	-->

	<!--佔位符配置  -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<!-- 資料連線池 -->
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<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>


	<!--SessionFactory配置  -->
	<bean id="mySessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--  
		<property name="annotatedClasses">
			<list>
				<value>entity.User</value>
			</list>
		</property>
		-->
		
		<property name="packagesToScan">
			<list>
				<value>com.cx.regist.entity</value>
			</list>
		</property>
		
		<!-- 一些hibernate的配置-->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>

	<!-- 事務管理 基於註解 :@transaction-->
	<tx:annotation-driven transaction-manager="txManager"/>


	<!-- 事務管理 基於xml-->

	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="mySessionFactory" />
	</bean>

	<aop:config>
		<aop:pointcut expression="execution(public * service..*.*(..))" id="serviceOperation"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="add"/>
		</tx:attributes>
	</tx:advice>

<!-- Template -->
	<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="mySessionFactory"></property>
	</bean>
</beans>

================================

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="regist" extends="struts-default" namespace="/user">
		<action name="toRegist" class="#">
			<result name="success">/user/success.jsp</result>
			<result name = "fail">/user/fail.jsp</result>
			<result name = "list">/user/userList.jsp</result>
			<result name = "user">/user/loadUser.jsp</result>
		</action>
	</package>
	
</struts>

==================================================
jdbc.properties

#資料庫的配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jd1107db
jdbc.username=root
jdbc.password=1234

===========================
log4j.properties

### direct log messages to stdout ###

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss} %5p %c{1}:%L - %m%n


log4j.rootLogger=warn, console

log4j.logger.com.tarena.tts=debug

log4j.logger.org.hibernate=error