(五)service
(1)service依赖dao(2)编写测试(3)如何读取另一个工程的spring的配置
classpath: 加载当前maven工程的resources目录下的配置文件 classpath*: 加载当前maven工程及其依赖工程的resources目录下的配置文件 applicationContext-*.xml: 读取所有符合规则的文件
TestCompanyService
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class TestCompanyService {
@Autowired
ICompanyService service
;
@Test
public void test01(){
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">
<context:component-scan base-package="com.wzx.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>
<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"/>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<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