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); } } }
五丶测试
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/15096785.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步