获取 classpath 用于 ClassPathXmlApplicationContext
发布日期:2021-06-30 16:08:05 浏览次数:2 分类:技术文章

本文共 999 字,大约阅读时间需要 3 分钟。

使用 ClassPath 类的 getClassPath() 方法可得到一组路径集合。

其中包括项目中使用的 jar 包路径和项目的 classpath。
代码:

package com.kaven.spring;import org.aspectj.apache.bcel.util.ClassPath;import java.util.Arrays;public class SpringTest {    public static void main(String args[]){        String[] str = ClassPath.getClassPath().split(";");        for(String s : str) System.out.println(s);    }}

得到下图路径信息:

在这里插入图片描述

在这里插入图片描述
在第一张图片中可得到我项目的 classpath。
然后根据自己项目的路径信息将 classpath 用于 ClassPathXmlApplicationContext。
我项目的路径信息:
在这里插入图片描述
代码:

package com.kaven.spring;import org.aspectj.apache.bcel.util.ClassPath;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Arrays;public class SpringTest {    public static void main(String args[]){//        String[] str = ClassPath.getClassPath().split(";");//        for(String s : str) System.out.println(s);        ClassPathXmlApplicationContext context  =                new ClassPathXmlApplicationContext("classpath:com/kaven/spring/SpringTestXML.xml");        context.close();    }}

转载地址:https://kaven.blog.csdn.net/article/details/82793776 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:怎么保存退出 vim 编辑
下一篇:使用 Spring AOP 时报错

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月17日 22时55分34秒