Spring如何通过配置文件与注解相结合完成属性值的注入?
- 内容介绍
- 文章标签
- 相关推荐
本文共计607个文字,预计阅读时间需要3分钟。
前言:在实际开发中,Spring中bean的属性直接赋值用得并不多,整理一下这方面的资料,做一个小结,以备后续深入学习。
通过配置文件的方式启动Spring容器时,可以使用以下方式为bean属性赋值:
1. XML配置文件方式:
xml
2. 注解配置方式:
java@Configurationpublic class AppConfig { @Bean public ExampleBean exampleBean() { ExampleBean bean=new ExampleBean(); bean.setProperty1(value1); bean.setProperty2(bean2()); return bean; }
@Bean public Bean2 bean2() { return new Bean2(); }}
总结:通过配置文件或注解的方式,我们可以为Spring容器中的bean属性赋值。在实际开发中,直接赋值的方式较少使用,更多情况下会选择通过构造函数、setter方法或依赖注入等方式进行。
前言
在实际开发当中,Spring中bean的属性直接赋值用的不是太多,整理这方面的资料,做一个小结,以备后续更深入的学习。
通过配置文件的方式
以配置文件的方式启动spring容器时,可以使用property标签的value给bean的属性赋值,赋值的形式有以下几种:
<--通过context:property-placeholder将properties文件中的值加载的环境变量中(properties中的属性值最终是以环境变量的形式存储的)> <context:property-placeholder location="classpath:person.properties"/> <bean id="person" class="com.atneusoft.bean.Person" > <--①通过基本数值直接赋值--> <property name="name" value="zhangsan"></property> <--②通过${}取出配置文件中的值--> <property name="age" value="${person.age}"></property> <--③通过Spring的El表达式--> <--<property name="age" value="10*2"></property>--> </bean>
classpath下的properties文件内容
person.age=\u5C0F\u674E\u56DB
通过注解的方式
使用properties的value对应的注解给属性赋值
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值 @PropertySource(value={"classpath:/person.properties"}) @Configuration public class MainConfigOfPropertyValues { @Bean public Person person(){ return new Person(); } }
public class Person { //使用@Value赋值; //1、基本数值 //2、可以写SpEL; #{} //3、可以写${};取出配置文件中的值(在运行环境变量里面的值) @Value("张三") private String name; @Value("#{20-2}") private Integer age; /* @Value("${person.age}") private Integer age;*/ }
注:
外部配置文件中的k/v保存到运行的环境变量中,可以直接在环境变量中取出对应的值
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("person.age");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计607个文字,预计阅读时间需要3分钟。
前言:在实际开发中,Spring中bean的属性直接赋值用得并不多,整理一下这方面的资料,做一个小结,以备后续深入学习。
通过配置文件的方式启动Spring容器时,可以使用以下方式为bean属性赋值:
1. XML配置文件方式:
xml
2. 注解配置方式:
java@Configurationpublic class AppConfig { @Bean public ExampleBean exampleBean() { ExampleBean bean=new ExampleBean(); bean.setProperty1(value1); bean.setProperty2(bean2()); return bean; }
@Bean public Bean2 bean2() { return new Bean2(); }}
总结:通过配置文件或注解的方式,我们可以为Spring容器中的bean属性赋值。在实际开发中,直接赋值的方式较少使用,更多情况下会选择通过构造函数、setter方法或依赖注入等方式进行。
前言
在实际开发当中,Spring中bean的属性直接赋值用的不是太多,整理这方面的资料,做一个小结,以备后续更深入的学习。
通过配置文件的方式
以配置文件的方式启动spring容器时,可以使用property标签的value给bean的属性赋值,赋值的形式有以下几种:
<--通过context:property-placeholder将properties文件中的值加载的环境变量中(properties中的属性值最终是以环境变量的形式存储的)> <context:property-placeholder location="classpath:person.properties"/> <bean id="person" class="com.atneusoft.bean.Person" > <--①通过基本数值直接赋值--> <property name="name" value="zhangsan"></property> <--②通过${}取出配置文件中的值--> <property name="age" value="${person.age}"></property> <--③通过Spring的El表达式--> <--<property name="age" value="10*2"></property>--> </bean>
classpath下的properties文件内容
person.age=\u5C0F\u674E\u56DB
通过注解的方式
使用properties的value对应的注解给属性赋值
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值 @PropertySource(value={"classpath:/person.properties"}) @Configuration public class MainConfigOfPropertyValues { @Bean public Person person(){ return new Person(); } }
public class Person { //使用@Value赋值; //1、基本数值 //2、可以写SpEL; #{} //3、可以写${};取出配置文件中的值(在运行环境变量里面的值) @Value("张三") private String name; @Value("#{20-2}") private Integer age; /* @Value("${person.age}") private Integer age;*/ }
注:
外部配置文件中的k/v保存到运行的环境变量中,可以直接在环境变量中取出对应的值
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("person.age");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

