spring 在web项目中的一些基本配置
1.首先在项目中的web.xml文件中加入如下xml 片段
//这个是加载spring配置文件,下文以为加载classpath下所有spring路径下的xml文件,classpath后跟*意味同时也会加载其他jar中的xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
//配置spring加载类,随webserver启动
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.spring*.xml的一些配置
在web项目中,springbean一般分为三类
service
<beans>
<bean id="baseService" class="com.service.impl.BaseServiceImpl">
<property name="baseDao" ref="baseDao"></property>
</bean>
</beans>
dao
<beans> <bean id="baseDao" class="com.dao.impl.BaseDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
</beans>
controller
controller用的是stucts所以就不再这里举例了
3.web项目中常用的一些实用功能
spring面向切面的事物管理,在这个例子中是管理com包下并且所有名称带有Service的class下的所有方法。
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* com..*Service.*(..))" advice-ref="txAdvice" />
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
spring的用户身份认证框架acegi
<bean id="filterChainProxy"
class="org.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,securityContextHolderAwareRequestFilter,anonymousProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor
</value>
</property>
</bean>
<bean id="httpSessionContextIntegrationFilter"
class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />
<bean id="logoutFilter"
class="org.acegisecurity.ui.logout.LogoutFilter">
<constructor-arg value="/pages/jsp/loginH.jsp" />
<constructor-arg>
<list>
<bean
class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler" />
</list>
</constructor-arg>
<property name="filterProcessesUrl" value="/j_acegi_logout"/>
</bean>
<bean id="authenticationProcessingFilter"
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationFailureUrl" value="/pages/jsp/loginH.jsp?loginerror=1" />
<property name="defaultTargetUrl" value="/index.do" />
<property name="filterProcessesUrl" value="/j_acegi_security_check" />
</bean>
<bean id="securityContextHolderAwareRequestFilter"
class="org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter" />
<bean id="anonymousProcessingFilter"
class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
<property name="key" value="changeThis" />
<property name="userAttribute"
value="anonymousUser,ROLE_ANONYMOUS" />
</bean>
<bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
<property name="key" value="changeThis"/>
</bean>
<bean id="exceptionTranslationFilter"
class="org.acegisecurity.ui.ExceptionTranslationFilter">
<property name="authenticationEntryPoint">
<bean
class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl" value="/pages/jsp/loginH.jsp" />
</bean>
</property>
<property name="accessDeniedHandler">
<bean
class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
<property name="errorPage" value="/pages/jsp/accessDenied.jsp" />
</bean>
</property>
</bean>
<bean id="filterInvocationInterceptor"
class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
<property name="authenticationManager"
ref="authenticationManager" />
<property name="accessDecisionManager"
ref="accessDecisionManager" />
<!--property name="objectDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=ROLE_ANONYMOUS,ROLE_1
</value>
</property-->
<property name="objectDefinitionSource"
ref="filterDefinitionSource" />
</bean>
<bean id="authenticationManager"
class="org.acegisecurity.providers.ProviderManager">
<property name="providers">
<list> <!-- 一般用户认证 -->
<ref local="anonymousAuthenticationProvider"/>
<ref local="daoAuthenticationProvider" />
</list>
</property>
</bean>
<bean id="accessDecisionManager"
class="org.acegisecurity.vote.AffirmativeBased">
<property name="allowIfAllAbstainDecisions" value="false" />
<property name="decisionVoters">
<list>
<bean class="org.acegisecurity.vote.RoleVoter" />
<bean class="org.acegisecurity.vote.AuthenticatedVoter" />
</list>
</property>
</bean>
<bean id="daoAuthenticationProvider"
class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService" />
<!-- UserCache property will activate the cache, it is not
mandatory but increases performance by cacheing the user
details retrieved from user-base -->
<property name="userCache" ref="userCache" />
<property name="passwordEncoder" ref="passwordEncoder" />
</bean>
<!-- 使用Md5算法加密 -->
<bean id="passwordEncoder"
class="org.acegisecurity.providers.encoding.Md5PasswordEncoder" />
<bean id="userDetailsService"
class="com.arvato.mynissan.service.impl.CustomUserDetailsServiceImpl">
<!-- property name="userIdIncluded" value="true" /-->
<property name="dataSource" ref="mynissanDataSource" />
<property name="usersByUsernameQuery">
<value>
SELECT USER_LOGINNAME,USER_LOGINPWD,1 FROM dfl_user WHERE USER_LOGINNAME=?
AND USER_STATUS='1'
</value>
</property>
<property name="authoritiesByUsernameQuery">
<value>
SELECT u.USER_LOGINNAME,r.CODE from dfl_user u, SYS_ROLES r,
SYS_USER_ROLE ur WHERE u.user_id=ur.USER_ID and r.id=ur.ROLE_ID
and u.USER_LOGINNAME = ?
</value>
</property>
</bean>
<bean id="userCache"
class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
<property name="cache">
<bean
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
</property>
<property name="cacheName" value="userCache" />
</bean>
</property>
</bean>
<bean id="resouceDetailsService"
class="com.arvato.core.acegisecurity.resouce.jdbc.ResouceDetailsServiceImpl">
<property name="dataSource" ref="mynissanDataSource" />
<property name="resouceQuery">
<value>
SELECT ID,RES_STRING,RES_TYPE FROM SYS_PERMISSIONS
</value>
</property>
<property name="authoritiesByRoleQuery">
<value>
SELECT ro.code from SYS_ROLES ro, SYS_PERMISSIONS p,SYS_ROLE_PERM pp
WHERE ro.id=pp.ROLE_ID and p.ID=pp.PERM_ID and p.ID= ?
</value>
</property>
<property name="resourceCache" ref="resourceCache"></property>
</bean>
<bean id="resourceCache"
class="com.arvato.core.acegisecurity.resouce.dao.cache.EhCacheBasedResourceCache">
<property name="cache">
<bean
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<!-- property name="cacheManager">
<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
</property-->
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="rdCache" />
</bean>
</property>
<property name="keyConverter">
<bean class="com.arvato.core.cache.QueryStringConverter" />
</property>
</bean>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>
classpath:config/ehcache.xml
</value>
</property>
</bean>
<bean id="filterDefinitionSource"
class="com.arvato.core.acegisecurity.intercept.web.DbBasedFilterInvocationDefinitionSource">
<!-- property name="convertUrlToLowercaseBeforeComparison" value="true" / -->
<property name="useAntPath" value="true" />
<property name="resouceDetailsManager" ref="resouceDetailsService" />
<property name="loginFormUrl" value="/pages/jsp/loginH.jsp*" />
<property name="defaultAuthority" value="ROLE_1" />
</bean>
<!-- This bean is optional; it isn't used by any other bean as it only listens and logs -->
<bean id="loggerListener" class="org.acegisecurity.event.authentication.LoggerListener" />
</beans>
项目数据源配置
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction">
<ref local="jotm" />
</property>
</bean>
<bean id="DataSource"
class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager">
<ref local="jotm" />
</property>
<property name="driverName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${lms.hibernate.connection.url}</value>
</property>
<property name="preparedStmtCacheSize"><value>0</value></property>
</bean>
</property>
<property name="user">
<value>${lms.hibernate.connection.username}</value>
</property>
<property name="password">
<value>${lms.hibernate.connection.password}</value>
</property>
<property name="maxSize"><value>150</value></property>
<property name="minSize"><value>1</value></property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource">
<ref local="DataSource" />
</property>
<property name="mappingLocations">
<list>
<value>
classpath*:com/models/*.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- prop key="hibernate.hbm2ddl.auto">update</prop -->
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">15</prop>
</props>
</property>
</bean>