使用 Maven 管理项目

Maven模块化开发与依赖问题解析
本文探讨了使用Maven进行模块化项目管理时遇到的问题,包括Junit测试包传递依赖失效的原因及解决方案,以及如何在多模块环境下正确读取Spring配置文件。文章通过具体案例展示了如何在service模块中正确引入dao模块的测试依赖,并介绍了@ContextConfiguration注解在模块间配置文件读取中的应用。

最近的练手项目使用的是 Maven 在管理项目,在使用 Maven 管理项目时,三层的开发时分模块开发的,parent-dao-service-web,所有的spring+struts + Hibernate的依赖都是加在 parent 上,dao-service-web都是作为子模块,在模块之间的关系处理的时候出现了几个问题:

  1. junit测试包的传递依赖失效了
  2. 多个配置文件的读取问题

       我在 parent 工程没有添加 Junit 的依赖,在编写 dao 模块是添加了 Junit 的 jar 包,理所当然的在 scope 中写了 test 范围,但是在 service 模块中进行  Junit 测试时,显示没有依赖上 Junit 包,那是为什么呢?百度了才想通,原来是 service 依赖的 dao 模块的 install 之后的 jar 包,当 dao 模块 install 时,scope 为 test 的 Junit包当然没有被发布出来,service中也就不能传递依赖到 Junit了,这样的解决办法只能在 service 中添加 Junit 包的依赖。

      因为采取的是模块式的开发,spring的配置文件就被分布在各个模块中了,在测试项目时需要读取多个模块中的 spring 配置文件时,使用到了之前没有使用到的一个注解:

   @ContextConfiguration(locations={"classpath*:applicationContext-*.xml"}) 这个注解中的*号通配符表示会加载本模块和依赖的jar包中的类路径下的applicationContext-开头的配置文件(只有spring配置文件才会这样命名)

//@ContextConfiguration(locations={"classpath*:applicationContext-*.xml"})
@ContextConfiguration(locations={"classpath:applicationContext-dao.xml","classpath:applicationContext-service.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerServiceImplTest {

    @Autowired
    private CustomerService customerService;
    
    @Test
    public void test() {
        Customer customer = customerService.findById(1L);
        System.out.println("********************"+customer.getCustName());
    }

}

转载于:https://www.cnblogs.com/daimajun/p/7074835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值