代码:
test.properties
book.name=java
book.author=jolie
ELConfig.java
package com.yubai.EL;
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.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
//@Configuration
@PropertySource("classpath:com/yubai/EL/test.properties")
public class ELConfig {
@Autowired
private Environment environment;
public void output(){
try{
System.out.println(environment.getProperty("book.author"));
}catch (Exception e){
e.printStackTrace();
}
}
}
Main.java
package com.yubai.EL;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainEl {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class);
ELConfig resource = context.getBean(ELConfig.class);
resource.output();
context.close();
}
}
当把ELConfig.java中类前的@Configuration注释掉后environment.getProperty(“book.author”)获得的值是null
???
不知道为什么注释@Configuration后从test.properties中获取的值是null,之后抽时间学习debug调试
本文通过一个具体的示例探讨了Spring框架中如何使用@PropertySource注解来加载外部配置文件,并解析了@Configuration注解在这一过程中的作用。当@Configuration被注释掉后,为何无法正确读取属性值的问题得到了详细的解答。
1471

被折叠的 条评论
为什么被折叠?



