spring系列-注解驱动原理及源码-属性赋值
生活随笔
收集整理的這篇文章主要介紹了
spring系列-注解驱动原理及源码-属性赋值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
一、bean屬性賦值
1、bean屬性使用@Value賦值
2、bean屬性通過配置文件賦值
一、bean屬性賦值
1、bean屬性使用@Value賦值
(1)編寫bean類
package com.xiang.spring.bean;import org.springframework.beans.factory.annotation.Value;public class Person {/*** 使用@Value賦值:* ① 、基本數(shù)值* ② 、可以寫SpEL、#{}* ③ 、可以寫${} 取出配置文件中的值(在運行環(huán)境變量里面的值)*/@Value("zhangsan")private String name;@Value("#{20-2}")private Integer age;...get set toString... }(2)配置類
package com.xiang.spring.config;import com.xiang.spring.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class MainConfigOfPropertyValues {@Beanpublic Person person() {return new Person();} }(3)測試方法測試結(jié)果
package com.xiang.spring.test;import com.xiang.spring.bean.Person; import com.xiang.spring.config.MainConfigOfPropertyValues; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class IOCTest_PropertyValues {@Testpublic void test01() {// 創(chuàng)建ioc容器,容器創(chuàng)建時,默認(rèn)會將單例的bean都創(chuàng)建出來AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);System.out.println("容器創(chuàng)建完成");String[] definitionNames = applicationContext.getBeanDefinitionNames();for (String name : definitionNames) {System.out.println(name);}Person person = applicationContext.getBean("person", Person.class);System.out.println(person);} }// 運行結(jié)果 容器創(chuàng)建完成 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 mainConfigOfPropertyValues person Person{name='zhangsan', age=18}2、bean屬性通過配置文件賦值
(1)bean類
package com.xiang.spring.bean;import org.springframework.beans.factory.annotation.Value;public class Person {/*** 使用@Value賦值:* ① 、基本數(shù)值* ② 、可以寫SpEL、#{}* ③ 、可以寫${} 取出配置文件中的值(在運行環(huán)境變量里面的值)*/@Value("zhangsan")private String name;@Value("#{20-2}")private Integer age;@Value("${person.trueName}")private String trueName;...get set toString... }(2)在resources目錄下新增配置文件person.properties
person.trueName=zhangsanfeng(3)配置類
package com.xiang.spring.config;import com.xiang.spring.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;/** * 使用@PropertySource讀取外部配置文件中k/v保存到運行環(huán)境變量中,加載完外部配置文件之后,使用@Value("${}")獲取 * 也可以使用@PropertySources指定多個@PropertySource */ @PropertySource(value = {"classpath:/person.properties"}) @Configuration public class MainConfigOfPropertyValues {@Beanpublic Person person() {return new Person();} }(4)測試程序查看結(jié)果
@Test public void test01() {// 創(chuàng)建ioc容器,容器創(chuàng)建時,默認(rèn)會將單例的bean都創(chuàng)建出來AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);System.out.println("容器創(chuàng)建完成");String[] definitionNames = applicationContext.getBeanDefinitionNames();for (String name : definitionNames) {System.out.println(name);}Person person = applicationContext.getBean("person", Person.class);System.out.println(person);// 在環(huán)境變量中也可以獲取到配置文件中的值String property = applicationContext.getEnvironment().getProperty("person.trueName");System.out.println(property); }// 運行結(jié)果 容器創(chuàng)建完成 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 mainConfigOfPropertyValues person Person{name='zhangsan', age=18, trueName='zhangsanfeng'} zhangsanfeng總結(jié)
以上是生活随笔為你收集整理的spring系列-注解驱动原理及源码-属性赋值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring系列-注解驱动原理及源码-b
- 下一篇: spring系列-注解驱动原理及源码-自