<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>
<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>*.action</url-pattern>
</filter-mapping>
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<context:annotation-config />
<context:component-scan base-package="" />
<task:annotation-driven />
<import resource="classpath:/applicationContext_datasource.xml" />
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:global.properties</value>
<value>classpath:query.properties</value>
</list>
</property>
</bean>
</beans>
and maybe there are smoe other jars not know because they are in project already
applicationContext.xml
dataSource
<bean id="orcaleDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="${xxx.url}"></property>
<property name="username" value="${xxx.user}"></property>
<property name="password" value="${xxx.DB.password}"></property>
</bean>
sessionFactory
packagesToScan 需要spring3,2.5不支持
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="orcaleDS" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.xxx.pojo</value>
</list>
</property>
</bean>
transactionManager
@Transcation 需要annotation-driven才会生效
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
Junit4 和 Spring整合,用Spring IOC来管理Test Case,从而实现Test Case可利用Spring强大的事物管理等功能,实现Test Case对数据库操作回滚的功能,保持数据库的完整性。
Mockito能有效的mock那些测试类中的不可控的对象,比如Request和Response
@ContextConfiguration(locations={
"config/applicationContext-datasource-test.xml",
"config/applicationContext-Freedom-test.xml",
"classpath:applicationContext-grid.xml",
"classpath:applicationContext-Freedom-legacy.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
protected HttpServletResponse getReponseWithPrintWriter() throws IOException {
PrintWriter writer = org.mockito.Mockito.mock(PrintWriter.class);
HttpServletResponse response = org.mockito.Mockito.mock(HttpServletResponse.class);
org.mockito.Mockito.when(response.getWriter()).thenReturn(writer);
return response;
}
protected HttpServletResponse getReponseWithOutputStream() throws IOException {
ServletOutputStream outputStream = org.mockito.Mockito.mock(ServletOutputStream.class);
HttpServletResponse response = org.mockito.Mockito.mock(HttpServletResponse.class);
org.mockito.Mockito.when(response.getOutputStream()).thenReturn(outputStream);
return response;
}
protected HttpServletRequest getUserIdFromPortalWithMockedRequest() {
HttpServletRequest request = org.mockito.Mockito.mock(HttpServletRequest.class);
org.mockito.Mockito.when(request.getHeader("user")).thenReturn("sxq");
return request;
}
@Scheduled默认单线程,可以通过设置线程池的个数来达到多现成的需求
@Configuration
public class SchedulerConfiguration implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
taskScheduler.initialize();
taskRegistrar.setTaskScheduler(taskScheduler);
}
}
原文:https://www.cnblogs.com/judesheng/p/10622033.html