Spring测试的常用方法

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringContextInit {
    private static SpringContextInit instance;

    private SpringContextInit() {
        instance = this;
    }

    public static SpringContextInit getInstance() {
        if (instance == null) {
            instance = new SpringContextInit();
        }
        return instance;
    }

    public void init() {
        String path = System.getProperty("user.dir");
        path = path + "/resource/bean";
        File file = new File(path);
        if (file != null && file.isDirectory()) {
            File[] files = file.listFiles();
            Map<Integer, String> map = new HashMap<Integer, String>();
            int i = 0;
            for (File f : files) {
                if (f.isFile()) {
                    map.put(i++, f.getName());
                }
            }
            String[] filesName = new String[i];
            if (!map.isEmpty()) {
                int k = 0;
                for (Integer ii : map.keySet()) {
                    filesName[k++] = "bean/" + map.get(ii);
                }
            }
            new ClassPathXmlApplicationContext(filesName);
        }
    }

    public void load() {
        // 1.加载方式classpathResource
		/*
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean/*.xml");
		ManagerGlobalProps globalProperties = (ManagerGlobalProps)ctx.getBean("globalProperties", ManagerGlobalProps.class);
		String zoneId = globalProperties.getZoneId();
		System.out.println("zoneId = " + zoneId);*/

        //String[] arr
        String path = System.getProperty("user.dir");
        System.out.println("path = " + path);
        String absolutePath = path + "/resource/bean";
        System.out.println("absolutePath = " + absolutePath);

        File file = new File(absolutePath);
        //2 .绝对路径
        /*String[] configs = null;
        if (file != null && file.isDirectory()) {
            File[] files = file.listFiles();
            configs = new String[files.length];
            int i = 0;
            for (File f : files) {
                configs[i++] = f.getAbsolutePath();
                System.out.println("config = " + f.getAbsolutePath());
            }
        }
        //绝对路径
        new FileSystemXmlApplicationContext(configs);*/

        //3.相对路径
        String[] relativePath = null;
        if (file != null && file.isDirectory()) {
            File[] files = file.listFiles();
            relativePath = new String[files.length];
            for (int i = 0; i < files.length; i++) {
                relativePath[i] = "bean/"+files[i].getName();
                System.out.println("relativePath = " + relativePath[i]);
            }
        }
        //相对路径
        new ClassPathXmlApplicationContext(relativePath);

        //4.springtest+junit
    }


    public static void main(String[] args) {
        SpringContextInit.getInstance().load();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值