package cn.synny.common.carts.dao; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.synny.common.carts.po.ShoppingPO; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring-mvc.xml","classpath:spring-service.xml"}) public class ShoppingDaoTest extends AbstractJUnit4SpringContextTests{ @Resource private ShoppingDao shoppingDao; @Test public void getAllShopping(){ List<ShoppingPO> shoppingPOs = shoppingDao.findAll(); System.out.println(shoppingPOs.size()); } } //报错的日志 015-03-31 13:36:14,682 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@227d4e] to prepare test instance [cn.synny.common.carts.dao.ShoppingDaoTest@50a492] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [junit-4.11.jar:na] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na] at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) [spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) [.cp/:na] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) [.cp/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BossOrderController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.ServletContext cn.synny.common.order.service.impl.OrderServiceImpl.servletContext; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) ~[spring-test-3.1.3.RELEASE.jar:3.1.3.RELEASE] ... 24 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.ServletContext cn.synny.common.order.service.impl.OrderServiceImpl.servletContext; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:872) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:814) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:438) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:554) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:155) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] ... 39 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.ServletContext cn.synny.common.order.service.impl.OrderServiceImpl.servletContext; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] ... 55 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:948) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:817) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] ... 57 common frames omitted //加载的配置文件 spring-mvc.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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <!-- 指定系统寻找controller路径 --> <mvc:annotation-driven validator="validator" conversion-service="conversionService"> <!-- 对bean 的特定返回值进行处理 <mvc:return-value-handlers> <bean class="cn.vulcan.controller.handler.OrgReturnValueHandlers"></bean> </mvc:return-value-handlers> --> <!-- 参数转换 <mvc:argument-resolvers> <bean class="cn.vulcan.controller.handler.OrgWebArgumentHandler"></bean> </mvc:argument-resolvers> --> <mvc:message-converters> <bean class="cn.synny.common.core.handler.JsonAbstractHttpMessageConverter"> <property name="serializerFeature"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 自定义日期转换器 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="cn.synny.common.core.conver.StringToDateConverter"> </bean> </list> </property> </bean> <!-- spel 表达式 <bean id="numberGuess" class="cn.vulcan.boss.core.remoting.NumberGuess"> <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/> </bean> --> <!-- 添加AOP注解 <aop:aspectj-autoproxy proxy-target-class="true"> </aop:aspectj-autoproxy> --> <!-- 搜索的包路径 --> <context:component-scan base-package="cn.synny.*.*.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 放过导入的JS CSS 图片等文件 --> <mvc:resources location="/static/" mapping="/static/**" cache-period="31556926" /> <!-- 数据效验--> <bean id="validatorAspect" class="cn.synny.common.core.aop.AopValidate"> <property name="validator" ref="validator" /> </bean> <aop:config> <aop:aspect ref="validatorAspect"> <aop:around method="doBasicProfiling" pointcut="@annotation(org.springframework.web.bind.annotation.RequestMapping)" /> </aop:aspect> </aop:config> <!-- 自定义异常拦截 --> <bean id="exceptionHandler" class="cn.synny.common.core.aop.AopException"> <property name="messageSource" ref="messageSource"></property> </bean> <!-- 配置注解用的本地化文件 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" p:validationMessageSource-ref="messageSource"> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="cn.synny.common.core.handler.SystemInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="cn.synny.common.core.interceptors.AuthInterceptor"> <property name="excludeUrls"> <list> <value>/web/login/doLogin</value> <value>/web/login/</value> <value>/boss/login/doLogin</value> <value>/boss/login/</value> <value>loginController.do?checkuser</value> <value>systemController.do?saveFiles</value> <value>systemController.do?saveNews</value> <value>iconController.do?saveIcon</value> <value>userController.do?savesign</value> </list> </property> <property name="includeUrls"> <list> <value>/web/mbr/</value> <value>/web/mbr/detail</value> <value>/order/listn</value> <value>/order/listp</value> </list> </property> </bean> </mvc:interceptor> </mvc:interceptors> <bean id="systemMonitor" class="cn.synny.common.core.handler.SystemMonitor"> <!-- 超时警告时间 --> <property name="methodWarningThreshold" value="10000" /> </bean> <!-- --> <aop:config> <aop:pointcut id="allControllerMethod" expression="execution(* cn.synny.*.*.controller.*.*(..))" /> <aop:advisor advice-ref="systemMonitor" pointcut-ref="allControllerMethod" /> </aop:config> <!-- 本地化文件配置 --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:fallbackToSystemLocale="true" p:useCodeAsDefaultMessage="false" p:defaultEncoding="UTF-8"> <property name="basenames"> <list> <value>classpath:validation</value> <value>classpath:exceptions</value> </list> </property> </bean> <!-- 文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8"> </bean> <!-- 视图解析类,基于jsp和jstl基础之上解析 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 解析器 --> <bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="order" value="3" /> <property name="location" value="classpath:views.xml" /> </bean> <!-- junit 测试用 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> </beans> //配置文件 <?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <!--细粒度的包扫描 devloper--> <context:component-scan base-package="cn.synny.*" use-default-filters="false"> <context:include-filter type="regex" expression="cn.synny.*.*.dao.*.*" /> <context:include-filter type="regex" expression="cn.synny.*.*.service.*.*" /> <context:include-filter type="regex" expression="cn.synny.*.*.exception.*.*" /> <context:include-filter type="regex" expression="cn.synny.*.*.aop.*.*" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加载属性配置文件 --> <context:property-placeholder location="classpath:service.properties" /> <!-- <jee:jndi-lookup id="dataSource" jndi-name="product/itkt" /> --> <!-- dataSource --> <!--数据源 --> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <!--检查数据库连接池中空闲连接的间隔时间--> <property name="idleConnectionTestPeriodInMinutes" value="60" /> <!--连接池中未使用的链接最大存活时间--> <property name="idleMaxAgeInMinutes" value="300" /> <!--设置每个分区含有connection最大个数--> <property name="maxConnectionsPerPartition" value="50" /> <!--设置每个分区含有connection最小个数--> <property name="minConnectionsPerPartition" value="2" /> <!--设置每个分区数--> <property name="partitionCount" value="3" /> <!--设置分区中的connection增长数量--> <property name="acquireIncrement" value="5" /> <!--设置连接池阀值--> <property name="poolAvailabilityThreshold" value="10"></property> <!--连接时间--> <property name="connectionTimeoutInMs" value="1000"></property> <!--连接时间--> <property name="acquireRetryAttempts" value="2"></property> </bean> <!-- iBatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlmapconfig.xml" /> <property name="mapperLocations" value="classpath:cn/synny/*/*/dao/**/*.xml" /> </bean> <!-- 执行sql 语句是会调用这个配置 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解事物控制 <tx:annotation-driven transaction-manager="txManager" /> --> <!-- 事务控制 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="upda*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="sav*" propagation="REQUIRED" /> <tx:method name="sub*" propagation="REQUIRED" /> <tx:method name="count*" propagation="REQUIRED" /> <tx:method name="batch*" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution( * cn.synny.*.*.service.*.*.*(..))" id="ServiceOpertation" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceOpertation" /> </aop:config> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.synny.*.*.dao" /> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> </bean> <bean id="startupListener" class="cn.synny.common.core.listener.StartupListener"> </bean> </beans>
额,你要用spring的测试框架去进行单元测试,他会注入你以来的bean
回复<aclass='referer'target='_blank'>@李伊子:这个也很简单也是注解配置的,网上随便找找资料很简单的.我之前没用过,有资料没?测试的时候都没有加载spring的配置文件?你这有两个问题:1junit测试框架你没搞明白,他是通过test注解的方式让junit反射调用相应的测试方法。所以不可能传参数进去,只能是在测试方法中自己构造。具体你可以上网搜一下junit的使用教程。2spring有自己的测试框架SpringJunit4ClassRunner,可以用来加载相应配置,当然你也完全可以自己构造相应的上下文环境,上网搜一堆答案。而你new一个ctrl出来然后调用方法的方式完全和spring没半点关系,如果接口上有更复杂的spring注解就完全没意义了的确呢!谢谢你的解答!单元测试的方法应该是无参类型的,并且返回值也应该是void
ShoppingControllerTest
这个测试类有问题:shoppingController这个属性不能是new出来的,应该是从spring容器中获得。
而spring容器倒是可以new出来或者使用注解
new的方式:
<preclass="programlisting"> public static voidmain(String[]args) throwsIOException{ApplicationContextappContext= newClassPathXmlApplicationContext( "applicationContext.xml");Applicationapplication=(Application)appContext.getBean( "application"); }注解的方式:
<preclass="programlisting"> import staticorg.springframework.test.web.servlet.request.MockMvcRequestBuilders. ;importstaticorg.springframework.test.web.servlet.result.MockMvcResultMatchers.; <spanstyle="color:gray;">@RunWith(SpringJUnit4ClassRunner.class) <spanstyle="color:gray;">@WebAppConfiguration <spanstyle="color:gray;">@ContextConfiguration("test-servlet-context.xml") public classExampleTests{ <spanstyle="color:gray;">@Autowired privateWebApplicationContextwac; privateMockMvcmockMvc; <spanstyle="color:gray;"> @Before public voidsetup(){ this.mockMvc=MockMvcBuilders.webAppContextSetup( this.wac).build();} <spanstyle="color:gray;"> @Test public voidgetAccount() throwsException{ this.mockMvc.perform(get( "/accounts/1"). accept(MediaType.parseMediaType( "application/json;charset=UTF-8"))).andExpect(status().isOk()).andExpect(content().contentType( "application/json")).andExpect(jsonPath( "$.name").value( "Lee"));}}官网文档去看吧:
http://docs.spring.io/spring/docs/3.2.13.RELEASE/spring-framework-reference/htmlsingle/#spring-mvc-test-framework
<divclass='ref'>这个测试类有问题:shoppingController这个属性不能是new出来的,应该是从spring容器中获得。
而spring容器倒是可以new出来或者使用注解
new的方式:
<preclass="programlisting"> public static voidmain(String[]args) throwsIOException{ApplicationContextappContext= newClassPathXmlApplicationContext( "applicationContext.xml");Applicationapplication=(Application)appContext.getBean( "application"); }注解的方式:
<preclass="programlisting"> import staticorg.springframework.test.web.servlet.request.MockMvcRequestBuilders. ;importstaticorg.springframework.test.web.servlet.result.MockMvcResultMatchers.; <spanstyle="color:gray;">@RunWith(SpringJUnit4ClassRunner.class) <spanstyle="color:gray;">@WebAppConfiguration <spanstyle="color:gray;">@ContextConfiguration("test-servlet-context.xml") public classExampleTests{ <spanstyle="color:gray;">@Autowired privateWebApplicationContextwac; privateMockMvcmockMvc; <spanstyle="color:gray;"> @Before public voidsetup(){ this.mockMvc=MockMvcBuilders.webAppContextSetup( this.wac).build();} <spanstyle="color:gray;"> @Test public voidgetAccount() throwsException{ this.mockMvc.perform(get( "/accounts/1"). accept(MediaType.parseMediaType( "application/json;charset=UTF-8"))).andExpect(status().isOk()).andExpect(content().contentType( "application/json")).andExpect(jsonPath( "$.name").value( "Lee"));}}官网文档去看吧:
http://docs.spring.io/spring/docs/3.2.13.RELEASE/spring-framework-reference/htmlsingle/#spring-mvc-test-framework
回复<aclass='referer'target='_blank'>@JacarriChan:我从新编辑一下我的问题你再进来看就行回复<aclass='referer'target='_blank'>@李伊子:在这个页面的最下面把报错日志完整的贴出来,然后圈我回复<aclass='referer'target='_blank'>@JacarriChan:java.lang.IllegalStateException:FailedtoloadApplicationContext总是抱着个错误加载失败注入也失败回复<aclass='referer'target='_blank'>@李伊子:看你的要求。如果不在意引入后启动变慢,那就全部加载。我想请教一下@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext.xml")这里面加载的配置文件应该是和数据库连接的配置文件还是总的配置文件版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。