spring环境测试

比如有个service类:(再建个接口类)

package com.tydic.jtcrm.batch.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tydic.jtcrm.batch.service.BatchRealNameService;
import com.tydic.jtcrm.common.service.ParentService;


@Service
public class BatchRealNameServiceImpl extends ParentService implements  BatchRealNameService{
    
    private final static Logger logger = LoggerFactory.getLogger(BatchRealNameServiceImpl.class);

    //根据待处理文件id 查询对应数据记录数量
    @Override
    public Map queryOneByHandleStatus(String handleStatus){
        Map provMap = this.selectOne("ds1", "batchReal.queryOneByHandleStatus",handleStatus);
        return provMap;
    }
    
}

【需要注意的是:要加@service注解】

然后在其他随便一个类里写个main方法即可测试:

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config/disconf.beans.xml"); 
        BatchRealNameService service = (BatchRealNameService)context.getBean("batchRealNameServiceImpl");
        Map map = service.queryOneByHandleStatus("1100");
        System.out.println(map);
    }

这里需要注意的是:

1、ApplicationContext  对象创建时后面参数要写 spring 配置文件的路径。

2、getBean后面的参数要写对应类的首字母小写。 即实际类是 BatchRealNameServiceImpl  而加载bean时,要写成   batchRealNameServiceImpl

猜你喜欢

转载自www.cnblogs.com/libin6505/p/10573709.html