Java 找不到资源的类路径/URL
我找不到xml文件的文件/类路径。我已经尝试了绝对路径和springsJava 找不到资源的类路径/URL,java,spring,spring-mvc,Java,Spring,Spring Mvc,我找不到xml文件的文件/类路径。我已经尝试了绝对路径和springsclasspath*: ApplicationContext ctx = new ClassPathXmlApplicationContext(); private Resource resource = this.ctx.getResource("classpath*:validation.xml"); 在构造函数中,我有: public Validator() { if(this.
classpath*:
ApplicationContext ctx =
new ClassPathXmlApplicationContext();
private Resource resource = this.ctx.getResource("classpath*:validation.xml");
在构造函数中,我有:
public Validator() {
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
this.runInputStream();
}
上述内容在控制台中打印出FALSE
。
我将validation.xml
文件放在两个不同名称的地方。我试图弄清楚它在哪里,为什么没有加载或被找到
我在GIT上发布了这个项目
---------------------更新1---------------------------
我已将以下内容添加到我的pom
文件中:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
问题在于src/main/resources/validation.xml没有打包到war文件中 将pom中的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
org.apache.maven.plugins
maven战争插件
3.1.0
这样,所有Maven项目的默认资源目录src/main/resources将在WAR中以target/classes和WEB-INF/classes结尾。问题在于src/main/resources/validation.xml没有打包到WAR文件中 将pom中的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
org.apache.maven.plugins
maven战争插件
3.1.0
这样,在WAR中,所有Maven项目的默认资源目录src/main/resources将以target/classes和WEB-INF/classes结尾。首先删除trailing/in resources部分。资源部分应与生成目录相对
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF/config</directory>
</resource>
</resources>
</build>
有关更多详细信息,请参阅。另一种方法是使用ApplicationContextAware,如下所示:
@Configuration
public class Validator implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Resource resource = applicationContext.getResource("classpath:validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
@Configuration
public class Validator {
@Autowired
private ApplicationContext applicationContext;
private Resource validationResource;
@PostConstruct
public void init(){
validationResource = applicationContext.getResource("classpath*:validation.xml");
}
构建项目,然后在tomcat中部署war。你应该看到类似的东西
22-May-2017 10:57:38.658 INFO [localhost-startStop-1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/config/application-context.xml]
FOUND!!!!!!!!!!!!!!!!!!!!class path resource [validation.xml]
2017年5月22日10:57:38.658信息[localhost-startStop-1]org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源加载xml bean定义[/WEB-INF/config/application context.xml]
建立类路径资源[validation.xml]
首先删除尾随/输入资源部分。资源部分应与生成目录相对
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF/config</directory>
</resource>
</resources>
</build>
有关更多详细信息,请参阅。另一种方法是使用ApplicationContextAware,如下所示:
@Configuration
public class Validator implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Resource resource = applicationContext.getResource("classpath:validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
@Configuration
public class Validator {
@Autowired
private ApplicationContext applicationContext;
private Resource validationResource;
@PostConstruct
public void init(){
validationResource = applicationContext.getResource("classpath*:validation.xml");
}
构建项目,然后在tomcat中部署war。你应该看到类似的东西
22-May-2017 10:57:38.658 INFO [localhost-startStop-1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/config/application-context.xml]
FOUND!!!!!!!!!!!!!!!!!!!!class path resource [validation.xml]
2017年5月22日10:57:38.658信息[localhost-startStop-1]org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源加载xml bean定义[/WEB-INF/config/application context.xml]
建立类路径资源[validation.xml]
我注意到的第一件事是你试图在实地获取资源。换言之,这:
public class SomeClass{
ApplicationContext ctx = new ClassPathXmlApplicationContext();
private Resource resource = this.ctx.getResource("classpath:validation.xml");
}
会让我觉得应用程序上下文没有完全初始化
在完成你的项目并检查之后,我的假设是正确的<代码>应用程序上下文在初始化类之前不会完全初始化。要绕过这一事实,可以使用@PostConstruct
注释。现在,您的类将如下所示:
@Configuration
public class Validator implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Resource resource = applicationContext.getResource("classpath:validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
@Configuration
public class Validator {
@Autowired
private ApplicationContext applicationContext;
private Resource validationResource;
@PostConstruct
public void init(){
validationResource = applicationContext.getResource("classpath*:validation.xml");
}
另外,我建议您花一些时间来正确配置pom。理想情况下,应该有人能够下载您的代码、构建和运行。由于手动配置,我花了一段时间才让您的项目在本地运行。我注意到的第一件事是您正在尝试获取现场级别的资源。换言之,这:
public class SomeClass{
ApplicationContext ctx = new ClassPathXmlApplicationContext();
private Resource resource = this.ctx.getResource("classpath:validation.xml");
}
会让我觉得应用程序上下文没有完全初始化
在完成你的项目并检查之后,我的假设是正确的<代码>应用程序上下文在初始化类之前不会完全初始化。要绕过这一事实,可以使用@PostConstruct
注释。现在,您的类将如下所示:
@Configuration
public class Validator implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Resource resource = applicationContext.getResource("classpath:validation.xml");
if(this.resource.exists()){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
}
@Configuration
public class Validator {
@Autowired
private ApplicationContext applicationContext;
private Resource validationResource;
@PostConstruct
public void init(){
validationResource = applicationContext.getResource("classpath*:validation.xml");
}
另外,我建议您花一些时间来正确配置pom。理想情况下,应该有人能够下载您的代码、构建和运行。由于手动配置,我花了一些时间让您的项目在本地运行。您可以尝试:
URL resource = ClassLoader.getSystemClassLoader().getResource("validation.xml");
如果有一个URL
可以从中获取资源。如果找不到资源,它将返回null。您可以尝试:
URL resource = ClassLoader.getSystemClassLoader().getResource("validation.xml");
如果有一个URL
可以从中获取资源。如果找不到资源,它将返回null。很难说。为了帮助调试,您可以查看正在解析的完全限定路径。尝试appContext.getResource(“classpath*:validation.xml”).getURI().getPath()
查看正在解析的完整路径。@ChristopherSchneider在调试器中失败,因为它无法解析URL。我试过绝对的道路和方法。您提到wayappContext
,我也知道您所指的类。我猜应用程序上下文没有初始化,因为您正在使用new
检索它。当您@Autowired
ApplicationContext
时会发生什么?@christorschneiderSystem.out.println(ApplicationContext.getResource(“classpath:validation.xml”).getURI().getPath())代码>返回空指针异常。而@PropertySource(“classpath*:validation.xml”)
将返回不存在的此类文件。我不明白@ChristopherSchneider我把它贴在GIT上很难说。为了帮助调试,您可以查看正在解析的完全限定路径。尝试appContext.getResource(“classpath*:validation.xml”).getURI().getPath()
查看正在解析的完整路径。@ChristopherSchneider在调试器中失败,因为它无法解析URL。我试过绝对的道路和方法。您提到wayappContext
,我也知道您所指的类。我猜应用程序上下文没有初始化,因为您正在使用new
检索它。当您@Autowired
ApplicationContext
时会发生什么情况?@C