简体   繁体   English

找不到资源的类路径/ URL

[英]Unable to find classpath/URL for Resource

I am not able to find the file/classpath for my xml file. 我找不到我的xml文件的文件/类路径。 I have tried the absolute path and springs classpath*: 我已经尝试过绝对路径并弹奏classpath*:

ApplicationContext ctx =
            new ClassPathXmlApplicationContext();



private Resource resource = this.ctx.getResource("classpath*:validation.xml");

In the constructor I have: 在构造函数中,我有:

public Validator() {

    if(this.resource.exists()){
        System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
    }else{
        System.out.println("NOT FOUND");
    }

    this.runInputStream();
}

The above prints out out FALSE in the console. 上面在控制台中打印出FALSE 在此处输入图片说明

I have the validation.xml file in two places just under different names. 我在两个地方的validation.xml文件使用不同的名称。 I am trying to figure out where and why it is not loading or being found. 我试图弄清楚没有加载或找不到它的位置和原因。

I posted the project on GIT.... GitHub Link 我在GIT 发布了该项目。

---------------------Update 1--------------------------- ---------------------更新1 ---------------------------

I have added the following to my pom file: 我已将以下内容添加到我的pom文件中:

<build>
 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
  </plugin>
</plugins>
</build>

It appears to have found/been initialized but the class is not and it gives me a null pointer exception just like it was doing before. 它似乎已经找到/被初始化了,但是该类没有被初始化,就像以前一样,它给了我一个空指针异常。

Updated Code: 更新的代码:

@Configuration
public class Validator {

    private InputStream forms;
    private ValidatorResources resources;

    private Resource validationResource;

    ApplicationContext ctx =
            new ClassPathXmlApplicationContext();

    @PostConstruct
    public void init(){
        validationResource = ctx.getResource("classpath*:validation.xml");
    }

    public Validator() {

        this.runInputStream();
    }

    private void runInputStream(){
        try{
            this.forms = this.validationResource.getInputStream();

            this.resources = new ValidatorResources(this.forms);

        }catch (IOException ex) {
            System.out.println("Error in Validator.Class-----runInputStream IOException: "+ex);
        }catch (SAXException e) {
            System.out.println("Error in Validator.Class-----runInputStream SAXException: "+e);
        }catch (Exception e){
            System.out.println("Error in Validator.Class-----------"+e);
        }

    }

}

在此处输入图片说明

The problem is that src/main/resources/validation.xml is not packaged into the war file. 问题是src / main / resources / validation.xml没有打包到war文件中。

Replace the < build> section in the pom with this: 用以下内容替换pom中的<build>部分:

<build>
 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
  </plugin>
</plugins>
</build>

This way the default resource directory for all Maven projects which is src/main/resources will end up in target/classes and in WEB-INF/classes in the WAR. 这样,所有Maven项目的默认资源目录src / main / resources最终将位于target / classes和WAR的WEB-INF / classes中。

First remove the trailing / in resources part. 首先删除尾随/资源部分。 The resource part should be relative to the build directory. 资源部分应相对于构建目录。

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/webapp/WEB-INF/config</directory>
        </resource>
    </resources>
</build>

Next modify your Validator.java 接下来修改您的Validator.java

@Configuration
public class Validator implements InitializingBean {

@Autowired
private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        Resource resource = applicationContext.getResource("validation.xml");
        if(this.resource.exists()){
            System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
        }else{
            System.out.println("NOT FOUND");
        }
    }

See InitializingBean for more details. 有关更多详细信息,请参见InitializingBean The other approach is to use ApplicationContextAware like this: 另一种方法是像这样使用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");
        }
    }

Build the project then deploy the war in tomcat. 构建项目,然后将战争部署在tomcat中。 You should see something like 你应该看到类似

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]

The first thing I noticed is you're trying to acquire resources at the field level. 我注意到的第一件事是您正在尝试在现场一级获取资源。 In other words, this: 换句话说,这是:

public class SomeClass{
   ApplicationContext ctx = new ClassPathXmlApplicationContext();
   private Resource resource = this.ctx.getResource("classpath:validation.xml");
}

Would make me think that application context isn't fully initialized. 让我认为应用程序上下文尚未完全初始化。

After pulling down your project and checking, my assumption was correct. 拉下您的项目并检查后,我的假设是正确的。 ApplicationContext isn't fully initialized until after the classes are initialized. 直到类初始化之后, ApplicationContext才被完全初始化。 To get around this fact, you can use the @PostConstruct annotation. 要解决这个问题,可以使用@PostConstruct批注。 Your class will now look like this: 您的班级现在看起来像这样:

@Configuration
public class Validator {
    @Autowired
    private ApplicationContext applicationContext;
    private Resource validationResource;


    @PostConstruct
    public void init(){
        validationResource = applicationContext.getResource("classpath*:validation.xml");
    }

On a side note, I'd recommend you take some time to properly configure your pom. 附带一提,我建议您花一些时间正确配置pom。 Ideally, someone should be able to pull down your code, build, and run. 理想情况下,某人应该能够删除您的代码,进行构建并运行。 It took me a while to get your project running locally due to manual configuration. 由于手动配置,我花了一些时间才能使您的项目在本地运行。

You could try: 您可以尝试:

URL resource = ClassLoader.getSystemClassLoader().getResource("validation.xml");

If having a URL would be usable to then get a Resource from. 如果可以使用URL ,则可以从中获取Resource It will return null if no resource is found. 如果找不到资源,它将返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM