整合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>
<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.jsu.service.*.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
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的整合了
…