@value将外部的值动态注入到Bean中
Bird
public class Bird {
@Value("bider") //,注入普通字符串
private String name;
@Value("#{20-2}")//SpringEL 表达式
private Integer age;
@Value("${bird.color}")//读取配置文件
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Bird [name=" + name + ", age=" + age + ", color=" + color + "]";
}
}
配置类
@Configuration
@PropertySource("classpath:bootstrap.properties")
public class MainConfig {
@Bean
public Bird Bird() {
return new Bird();
}
}
测试类
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext anno = new AnnotationConfigApplicationContext(MainConfig.class);
for (String string : anno.getBeanDefinitionNames()) {
System.out.println(string);
}
System.out.println("容器加载完成");
Bird bird = (Bird) anno.getBean("Bird");
System.out.println(bird.toString());
//关闭容器
anno.close();
}
}
运行结果
:49:56.563 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
Bird
容器加载完成
21:49:56.566 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'Bird'
Bird [name=bider, age=18, color=red]
21:49:56.566 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6ea6d14e: startup date [Sun Sep 29 21:49:56 CST 2019]; root of context hierarchy
21:49:56.566 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:49:56.566 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2833cc44: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,mainConfig,Bird]; root of factory hierarchy
网友评论