Jpetstore研究(二)配置文件分析

来源:互联网 发布:成绩统计软件 编辑:程序博客网 时间:2024/06/05 19:37

1> web.xml

配置log4j:

       <!--- Key of the system property that should specify the root directory of this- web app. Applied by WebAppRootListener or Log4jConfigListener.--><context-param><param-name>webAppRootKey</param-name><param-value>petstore.root</param-value></context-param><!--- Location of the Log4J config file, for initialization and refresh checks.- Applied by Log4jConfigListener.--><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param>    <!-- Leave the listener commented-out if using JBoss -->    <!--    <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    </listener>    -->

配置好webAppRootKey可以在log4j.properties中引用它作为应用的根目录,如log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log

配置需要加载的配置文件,默认加载的是/WEB-INF/applicationContext.xml

<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/dataAccessContext-local.xml /WEB-INF/applicationContext.xml</param-value></context-param><listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

配置dispatcherservlet,其对应的配置文件为:petstore-servlet.xml

<!--- Spring web MVC servlet that dispatches requests to registered handlers.- Has its own application context, by default defined in "{servlet-name}-servlet.xml",- i.e. "petstore-servlet.xml" in this case.-- A web app can contain any number of such servlets.- Note that this web app has a shared root application context, serving as parent- of all DispatcherServlet contexts.--><servlet><servlet-name>petstore</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>2</load-on-startup></servlet>

配置匹配的URL:

<servlet-mapping><servlet-name>petstore</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>

配置欢迎页面:

<welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>


2>  petstore-servlet.xml

配置视图:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/WEB-INF/jsp/spring/"/><property name="suffix" value=".jsp"/></bean>

配置controller:

<!-- ========================= DEFINITIONS OF PUBLIC CONTROLLERS ========================= --><bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean name="/shop/addItemToCart.do" class="org.springframework.samples.jpetstore.web.spring.AddItemToCartController"><property name="petStore" ref="petStore"/></bean>        <!-- 注入视图名称,实现参数化,而不是硬编码 --><bean name="/shop/checkout.do" class="org.springframework.samples.jpetstore.web.spring.ViewCartController"><property name="successView" value="Checkout"/></bean>        <!-- 不需要具体的controller处理逻辑,直接返回视图index.jsp --><bean name="/shop/index.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController"><property name="viewName" value="index"/></bean>        <!-- validator与successView来自父类 --><bean name="/shop/newAccount.do" class="org.springframework.samples.jpetstore.web.spring.AccountFormController"><property name="petStore" ref="petStore"/><property name="validator" ref="accountValidator"/><property name="successView" value="index"/></bean><bean name="/shop/removeItemFromCart.do" class="org.springframework.samples.jpetstore.web.spring.RemoveItemFromCartController"/><bean name="/shop/signoff.do" class="org.springframework.samples.jpetstore.web.spring.SignoffController"/><bean name="/shop/searchProducts.do" class="org.springframework.samples.jpetstore.web.spring.SearchProductsController"><property name="petStore" ref="petStore"/></bean><bean name="/shop/signon.do" class="org.springframework.samples.jpetstore.web.spring.SignonController"><property name="petStore" ref="petStore"/></bean><bean name="/shop/signonForm.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController"><property name="viewName" value="SignonForm"/></bean><bean name="/shop/updateCartQuantities.do" class="org.springframework.samples.jpetstore.web.spring.UpdateCartQuantitiesController"/><bean name="/shop/viewCart.do" class="org.springframework.samples.jpetstore.web.spring.ViewCartController"><property name="successView" value="Cart"/></bean><bean name="/shop/viewCategory.do" class="org.springframework.samples.jpetstore.web.spring.ViewCategoryController"><property name="petStore" ref="petStore"/></bean><bean name="/shop/viewItem.do" class="org.springframework.samples.jpetstore.web.spring.ViewItemController"><property name="petStore" ref="petStore"/></bean><bean name="/shop/viewProduct.do" class="org.springframework.samples.jpetstore.web.spring.ViewProductController"><property name="petStore" ref="petStore"/></bean>

配置安全保护的controller:

<!-- ========================= DEFINITIONS OF PROTECTED CONTROLLERS ========================= --><bean id="secureHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="interceptors"><list><ref bean="signonInterceptor"/></list></property><property name="urlMap"><map><entry key="/shop/editAccount.do" value-ref="secure_editAccount"/><entry key="/shop/listOrders.do" value-ref="secure_listOrders"/><entry key="/shop/newOrder.do" value-ref="secure_newOrder"/><entry key="/shop/viewOrder.do" value-ref="secure_viewOrder"/></map></property></bean><bean id="signonInterceptor" class="org.springframework.samples.jpetstore.web.spring.SignonInterceptor"/><bean id="secure_editAccount" class="org.springframework.samples.jpetstore.web.spring.AccountFormController"><property name="petStore" ref="petStore"/><property name="validator" ref="accountValidator"/><property name="successView" value="index"/></bean><bean id="secure_listOrders" class="org.springframework.samples.jpetstore.web.spring.ListOrdersController"><property name="petStore" ref="petStore"/></bean><bean id="secure_newOrder" class="org.springframework.samples.jpetstore.web.spring.OrderFormController"><property name="petStore" ref="petStore"/><property name="validator" ref="orderValidator"/></bean><bean id="secure_viewOrder" class="org.springframework.samples.jpetstore.web.spring.ViewOrderController"><property name="petStore" ref="petStore"/></bean>

首先增加了一层过滤器
signonInterceptor
实现了preHandle的功能,然后列出需要保护的controller,查看signonInterceptor可知,在用户没有登录的情况下访问上面受保护的.do时会跳到SignonForm,并且登录成功后再跳到这个.do页面

3> applicationContext.xml

<!-- Configurer that replaces ${...} placeholders with values from properties files --><!-- (in this case, mail and JDBC related properties) --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>WEB-INF/mail.properties</value><value>WEB-INF/jdbc.properties</value></list></property></bean>

PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。

这样就可以以比如${jdbc.url}的形式来引用jdbc.properties里的内容,dataAccessContext-local.xml就是这样来配置数据源。

配置validator:

<!-- Generic validator for Account objects, to be used for example by the Spring web tier --><bean id="accountValidator" class="org.springframework.samples.jpetstore.domain.logic.AccountValidator"/><!-- Generic validator for Order objects, to be used for example by the Spring web tier --><bean id="orderValidator" class="org.springframework.samples.jpetstore.domain.logic.OrderValidator"/>

配置依赖注入:

<bean id="petStore" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl"><property name="accountDao" ref="accountDao"/><property name="categoryDao" ref="categoryDao"/><property name="productDao" ref="productDao"/><property name="itemDao" ref="itemDao"/><property name="orderDao" ref="orderDao"/></bean>

各个Dao的bean定义在dataAccessContext-local.xml


4> dataAccessContext-local.xml

配置数据源:

<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><!-- Transaction manager for a single JDBC DataSource -->    <!-- (see dataAccessContext-jta.xml for an alternative) -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>

配置ibatis,定义bean sqlMapClient

<!-- SqlMap setup for iBATIS Database Layer --><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation" value="WEB-INF/sql-map-config.xml"/><property name="dataSource" ref="dataSource"/></bean>

定义DAO:

        <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="categoryDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapCategoryDao"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="productDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapProductDao"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="itemDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapItemDao"><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="orderDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapOrderDao"><property name="sqlMapClient" ref="sqlMapClient"/><property name="sequenceDao" ref="sequenceDao"/></bean><bean id="sequenceDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapSequenceDao"><property name="sqlMapClient" ref="sqlMapClient"/></bean>




原创粉丝点击