saas-export项目-项目搭建-service

  • export_system_service
    (1)service依赖dao
    (2)编写测试
    (3)如何读取另一个工程的spring的配置

classpath: 加载当前maven工程的resources目录下的配置文件
classpath*: 加载当前maven工程及其依赖工程的resources目录下的配置文件
applicationContext-*.xml: 读取所有符合规则的文件

TestCompanyService

src\test\java\com\lfy\service\company\TestCompanyService.java

//2:添加spring的单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")   //3:创建 spring/applicationContext-tx.xml
public class TestCompanyService {
    @Autowired
    ICompanyService service;
    @Test
    public void test01(){
        //1:编写了业务逻辑的测试
        //等号 左边是接口 右边是实现类
        //ICompanyService  service = new CompanyServiceImpl();
        List<Company> list=service.findAllCompanies();
        System.out.println(list);
    }
}

spring/applicationContext-tx.xml

src\main\resources\spring\applicationContext-tx.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描Service实现类-->
    <context:component-scan base-package="com.lfy.service"/>


    <!--Spring声明式事务(底层就是AOP): 三步曲-->

    <!--1.配置事务管理器:管理事务:DataSource.Connection.commit() rollback()方法  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--2.配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager" >
        <!--配置事务细节特征-->
        <tx:attributes>
            <!--查询方法,使用默认的隔离级别 及 SUPPORTS传播行为-->
            <tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS"/>
            <tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS"/>
            <tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS"/>
            <tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS"/>
            <!--增删改方法,使用默认的隔离级别 及 REQUIRED传播行为-->
            <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--3.配置事务切面: 切面=通知+切入点-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="pt" expression="execution(* com.lfy.service.*.impl.*.*(..))"/>

        <!--切面=通知+切入点-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值