您的当前位置:首页正文

spring项目获取类路径地址值

2024-11-28 来源:个人技术集锦

1.使用 ResourceLoader:

@Autowired
private ResourceLoader resourceLoader;

public void getClassPath() {
    Resource resource = resourceLoader.getResource("classpath:");
    try {
        String classpath = resource.getURL().getPath();
        System.out.println("Classpath: " + classpath);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2.使用 ClassPathResource:

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public void getClassPath() {
    Resource resource = new ClassPathResource("");
    try {
        String classpath = resource.getURL().getPath();
        System.out.println("Classpath: " + classpath);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
显示全文