Spring注解驱动开发——属性赋值 @Value
使用value赋值
1丶基本数值
2丶可以写SpEL; #{}
3丶可以写${};取出配置文件中的值(在运行环境变量里面的值)
1丶基本数值&可以写SpEL; #{}
一丶配置类
package com.mongoubiubiu.conf; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import com.mongoubiubiu.bean.Person; @Configurable public class MainConfigOfPropertyValues { @Bean public Person person(){ return new Person(); } }
二丶测试类
package com.mongougbiubiu.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Scope; import com.mongoubiubiu.bean.Person; import com.mongoubiubiu.conf.MainConfigOfLifeCycle; import com.mongoubiubiu.conf.MainConfigOfPropertyValues; public class IOCTest_propertyValues { @Test public void Test01(){ //创建ioc 容器 AnnotationConfigApplicationContext applica= new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class); Person person=applica.getBean(Person.class); System.out.println(person); String [] af=applica.getBeanNamesForType(Person.class); for (String string : af) { System.out.println(string); } } }
三丶实体类
package com.mongoubiubiu.bean; import org.springframework.beans.factory.annotation.Value; public class Person { public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } /** *使用value赋值 *1丶基本数值 *2丶可以写SpEL; #{} *3丶可以写${};取出配置文件中的值(在运行环境变量里面的值) */ @Value("李四") private String name; @Value("#{200-99}") private Integer age; 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
四丶返回

2丶基本数值&可以写SpEL; #{}
一丶person.properties
person.name=mangoubiubiu person.age=18
二丶实体类
package com.mongoubiubiu.bean; import org.springframework.beans.factory.annotation.Value; public class Person { public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } /** *使用value赋值 *1丶基本数值 *2丶可以写SpEL; #{} *3丶可以写${};取出配置文件中的值(在运行环境变量里面的值) */ @Value("${person.name}") private String name; @Value("${person.age}") private Integer age; 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
三丶配置类使用@PropertySource 注解读取 properties 配置信息
package com.mongoubiubiu.conf; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import com.mongoubiubiu.bean.Person; @PropertySource(value={"classpath:/person.properties"}) @Configurable public class MainConfigOfPropertyValues { @Bean public Person person(){ return new Person(); } }
四丶测试类
package com.mongougbiubiu.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Scope; import org.springframework.core.env.ConfigurableEnvironment; import com.mongoubiubiu.bean.Person; import com.mongoubiubiu.conf.MainConfigOfLifeCycle; import com.mongoubiubiu.conf.MainConfigOfPropertyValues; public class IOCTest_propertyValues { @Test public void Test01(){ //创建ioc 容器 AnnotationConfigApplicationContext applica= new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class); Person person=applica.getBean(Person.class); System.out.println(person); System.out.println("环境变量获取在下面哈============"); ConfigurableEnvironment config=applica.getEnvironment(); String s=config.getProperty("person.name"); System.out.println(s); String [] af=applica.getBeanNamesForType(Person.class); for (String string : af) { System.out.println(string); } } }
五丶测试


浙公网安备 33010602011771号