测试spring test,测试模块是单独的执行模块,所以要指明加jar才能用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml") //实例化一个session工厂 test是独立于其他模块的,所有要想使用工厂必须读取到工厂先
public class BrandServiceTest {
@Resource //在service接口中没有注册到工厂,
private BrandService brandService; //必须用接口类,面向接口编程,涉及底层动态代理,不予深究
@Test
public void getAll(){
List<Brand> brandList = brandService.getAll();
for (Brand List:brandList
) {
System.out.println(List);
}
}
<?
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"
xsi :schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context :component-scan base-package ="com.babasport" >
< context :exclude-filter type ="annotation"
expression ="org.springframework.stereotype.Controller" ></ context :exclude-filter>
</ context :component-scan>
<!-- dataSource -->
<bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource"
init-method ="init" destroy-method ="close" >
<property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
<property name ="url" value ="jdbc:mysql://localhost:3306/babasport" />
<property name ="username" value ="root" />
<property name ="password" value ="root" />
<property name ="filters" value ="stat" />
<property name ="maxActive" value ="20" />
<property name ="initialSize" value ="1" />
<property name ="maxWait" value ="60000" />
<property name ="minIdle" value ="1" />
<property name ="timeBetweenEvictionRunsMillis" value ="3000" />
<property name ="minEvictableIdleTimeMillis" value ="300000" />
<property name ="validationQuery" value ="SELECT 'x'" />
<property name ="testWhileIdle" value ="true" />
<property name ="testOnBorrow" value ="false" />
<property name ="testOnReturn" value ="false" />
</bean>
<bean id ="sessionFactory"
class ="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name ="dataSource" ref ="dataSource" />
<property name ="packagesToScan" >
<list>
<value>com.babasport.entity </value>
</list>
</property>
<property name ="hibernateProperties" >
<props>
<prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</prop>
<prop key ="hibernate.hbm2ddl.auto" >update </prop>
<prop key ="hibernate.show_sql" >true </prop>
<prop key ="hibernate.format_sql" >true </prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
<prop key ="hibernate.connection.url" >jdbc:mysql://localhost:3306/babasport </prop>
<prop key ="hibernate.connection.driver_class" >com.mysql.jdbc.Driver </prop>
</props>
</property>
</bean>
<!-- 事务管理器 -->
<bean id ="txManager"
class ="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name ="sessionFactory" ref ="sessionFactory" />
</bean>
< aop :config>
< aop :pointcut id ="service" expression ="execution(public * com.babasport..*.*(..))" />
< aop :advisor pointcut-ref ="service" advice-ref ="txAdvice" />
</ aop :config>
<!--没有spring,下边必须配,表示事物管理由hibernate管理,用spring管理事物,则不能配-->
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
< tx :advice id ="txAdvice" transaction-manager ="txManager" >
< tx :attributes>
< tx :method name ="get*" read-only ="true" />
< tx :method name ="save*" propagation ="REQUIRED" />
< tx :method name ="del*" propagation ="REQUIRED" />
< tx :method name ="update*" propagation ="REQUIRED" />
</ tx :attributes>
</ tx :advice>
<!-- 注解方式的事务管理 -->
<!--<bean id="txManager"-->
<!--class="org.springframework.orm.hibernate4.HibernateTransactionManager">-->
<!--<property name="sessionFactory" ref="sessionFactory" />-->
<!--</bean>-->
<!--<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>-->
</beans>
<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"
xsi :schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context :component-scan base-package ="com.babasport" >
< context :exclude-filter type ="annotation"
expression ="org.springframework.stereotype.Controller" ></ context :exclude-filter>
</ context :component-scan>
<!-- dataSource -->
<bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource"
init-method ="init" destroy-method ="close" >
<property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
<property name ="url" value ="jdbc:mysql://localhost:3306/babasport" />
<property name ="username" value ="root" />
<property name ="password" value ="root" />
<property name ="filters" value ="stat" />
<property name ="maxActive" value ="20" />
<property name ="initialSize" value ="1" />
<property name ="maxWait" value ="60000" />
<property name ="minIdle" value ="1" />
<property name ="timeBetweenEvictionRunsMillis" value ="3000" />
<property name ="minEvictableIdleTimeMillis" value ="300000" />
<property name ="validationQuery" value ="SELECT 'x'" />
<property name ="testWhileIdle" value ="true" />
<property name ="testOnBorrow" value ="false" />
<property name ="testOnReturn" value ="false" />
</bean>
<bean id ="sessionFactory"
class ="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name ="dataSource" ref ="dataSource" />
<property name ="packagesToScan" >
<list>
<value>com.babasport.entity </value>
</list>
</property>
<property name ="hibernateProperties" >
<props>
<prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</prop>
<prop key ="hibernate.hbm2ddl.auto" >update </prop>
<prop key ="hibernate.show_sql" >true </prop>
<prop key ="hibernate.format_sql" >true </prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
<prop key ="hibernate.connection.url" >jdbc:mysql://localhost:3306/babasport </prop>
<prop key ="hibernate.connection.driver_class" >com.mysql.jdbc.Driver </prop>
</props>
</property>
</bean>
<!-- 事务管理器 -->
<bean id ="txManager"
class ="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name ="sessionFactory" ref ="sessionFactory" />
</bean>
< aop :config>
< aop :pointcut id ="service" expression ="execution(public * com.babasport..*.*(..))" />
< aop :advisor pointcut-ref ="service" advice-ref ="txAdvice" />
</ aop :config>
<!--没有spring,下边必须配,表示事物管理由hibernate管理,用spring管理事物,则不能配-->
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
< tx :advice id ="txAdvice" transaction-manager ="txManager" >
< tx :attributes>
< tx :method name ="get*" read-only ="true" />
< tx :method name ="save*" propagation ="REQUIRED" />
< tx :method name ="del*" propagation ="REQUIRED" />
< tx :method name ="update*" propagation ="REQUIRED" />
</ tx :attributes>
</ tx :advice>
<!-- 注解方式的事务管理 -->
<!--<bean id="txManager"-->
<!--class="org.springframework.orm.hibernate4.HibernateTransactionManager">-->
<!--<property name="sessionFactory" ref="sessionFactory" />-->
<!--</bean>-->
<!--<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>-->
</beans>