分布式项目搭建之业务模块的整合

整合service子模块

  • 1、配置事务和aop
    创建一个applicationContext-service.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--配置包扫描-->
    <context:component-scan base-package="com.jsu.service"/>


    <!--配置事务-->
    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事务通知-->
    <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>

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

        <!--配置切面-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

  • 2、创建一个事务类接口
public interface ICompanyService {

    List<Company> findAll();
}


  • 3、实现接口,调用dao子模块的方法
    这里在加载spring配置文件的时候需要注意
@Service
public class CompanyServiceImpl  implements ICompanyService {

    @Autowired
    private ICompany iCompany;

    @Override
    public List<Company> findAll() {
        return iCompany.findAll();
    }
}

  • 4、测试
    因为数据访问层在dao子模块,所以在加载spring的配置文件时,需要使用
    classpath*:applicationContext-*.xml加载整个父工程的所有的spring配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class ICompanyServiceTest {


    @Autowired
    private ICompanyService iCompanyService;


    @Test
    public void test01  ()  {
        List<Company> companyList =  iCompanyService.findAll();
        System.out.println(companyList);
    }


}

  • 5、如果上面的步骤没有问题就可以继续整合springmvc的整合了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值