Spring學習(二)資源調用 + class path resource cannot be opened because it does not exist

我在學習spring的資源調用時

我運行王雲飛老師的代碼遇到了class path resource cannot be opened because it does not exist這個報錯,我將演示我如何解決。

代碼復現

目錄結構
在這裏插入圖片描述
conf類:

package day0125.SpringResourceScheduling;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * @Author Braylon
 * @Date 2020/1/25 21:15
 */
@Configuration
@ComponentScan("day0125.SpringResourceScheduling")
@PropertySource(value = {"classpath:day0125/SpringResourceScheduling/test.properties"})
public class conf {
    @Value("str injection")
    private String normalAttribute;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    @Value("#{T(java.lang.Math).random() * 100.0}")
    private double randonNum;

    @Value("#{service01.normalAttribute}")
    private String normalAttributeFromOutClass;

    @Value("classpath=test.properties")
    private Resource file;

    @Value("http://www.baidu.com")
    private Resource url;

    @Value("${classpath:day0125/SpringResourceScheduling/test.properties}")
    private String author;

    @Autowired
    private Environment environment;

    public void output() {
        System.out.println("Inject normal str:" + normalAttribute);
        System.out.println("inject os attributes:" + osName);
        System.out.println("inject expression:" + randonNum);
        System.out.println("inject other class' attr:" + normalAttributeFromOutClass);
        try {
            System.out.println(IOUtils.toString(file.getInputStream()));
            System.out.println(IOUtils.toString(url.getInputStream()));
            System.out.println("author:" + author);
            System.out.println(environment.getProperty("resourceScheduling.version"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

service類

package day0125.SpringResourceScheduling;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author Braylon
 * @Date 2020/1/25 21:07
 */
@Getter
@Setter
@Service
public class service01 {
    @Value("this is a str injection")
    private String normalAttribute;
}

test類

package day0125.SpringResourceScheduling;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author Braylon
 * @Date 2020/1/25 21:27
 */

public class test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(conf.class);
        conf bean = context.getBean(conf.class);
        bean.output();
        System.out.println("done");
    }
}

報錯

class path resource cannot be opened because it does not exist
就是顯示找不到test.proterties文件。

debug

這個其實沒怎麼困擾我,肯定我覺得是應該把properties文件放到resources目錄下面,
結果我滿懷信心的改了路徑,變成了value = {“classpath:test.properties”},結果又是報錯。
再仔細接茬發現,原來的是conf類中的兩個路徑都報錯,現在@propertiessource註解目錄已經正常了,但是@Value中的路徑爆紅,我修改爲了"classpath=test.properties"於是正常運行了。

總結

其實classpath這個是比較基礎的知識點,很簡單,沒什麼好說的,但是我不理解的是問什麼第二個@Value在修改之後會標紅,知道的朋友希望能給我解惑。

大家共勉~~
武漢加油

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章