测试spring test,测试模块是单独的执行模块,所以要指明加jar才能用

测试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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值