我在学习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在修改之后会标红,知道的朋友希望能给我解惑。
大家共勉~~
武汉加油