Day30项目saas-export项目-项目搭建(五)service

(五)service

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

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

TestCompanyService

//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.findAll();
        System.out.println(list);
    }
}

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.wzx.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.wzx.service.*.impl.*.*(..))"/>

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

</beans>


如果出现utf-8异常

去父工程的pom.xml

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

如果出现applicationContext异常

@ContextConfiguration("classpath*:spring/applicationContext-*.xml")

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

如果出现Company找不到类异常

<packaging>pom</packaging>
export_parent

<packaging>jar</packaging>
export_domain
export_dao
export_system_service

<packaging>war</packaging>
export_web_manager

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/109223955
今日推荐