标签:name Spring age Value Person org import public 赋值
使用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); } } }
五丶测试
标签:name,Spring,age,Value,Person,org,import,public,赋值 来源: https://www.cnblogs.com/mangoubiubiu/p/15096785.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。