4. 属性赋值
4.1. @Value
赋值
有一个Person类:
package com.hutu.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
}
还有一个配置类:MainConfigOfPropertyValues
package com.hutu.config;
import com.hutu.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person() {
return new Person();
}
}
测试方法
@Test
public void test01() {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
System.out.println("======================");
Person person = context.getBean(Person.class);
System.out.println(person);
printBeans(context);
}
private void printBeans(ApplicationContext context) {
String[] definitionNames = context.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println("IOC容器中已注册的bean:" + name);
}
}
测试结果
======================
Person(name=null, age=null)
IOC容器中已注册的bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalCommonAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerFactory
IOC容器中已注册的bean:mainConfigOfPropertyValues
IOC容器中已注册的bean:person
可以看容器中的Person{name=‘null’, age=null}值都会默认值,
现在,我们就可以这样来写:
package com.hutu.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
//使用@Value赋值
//1.基本的数值
//2.可以写SpEL: #{}
//3.可以写${}:取出配置文件中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
}
再来运行以上的测试方法,测试结果如下:这个时候,Person的属性就是可以获取到值了
======================
Person(name=张三, age=18)
IOC容器中已注册的bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalCommonAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerFactory
IOC容器中已注册的bean:mainConfigOfPropertyValues
IOC容器中已注册的bean:person
4.2. @PropertySource
加载外部配置文件
我们为Person这个类再添加一个属性nickName:
package com.hutu.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
//使用@Value赋值
//1.基本的数值
//2.可以写SpEL: #{}
//3.可以写${}:取出配置文件中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
private String nickName;
}
再来写上一个person.properties文件:
person.nickName=小张三
在用xml文件配置的时候,我们是这样做的:
<context:property-placeholder location="classpath:person.properties"/>
现在,我们用注解的方式就可以这样来做: 我们要添加这样的一个注解:@PropertySource,查看源码的时候,我们可以发现,这个注解是一个可重复标注的注解,可多次标注,也可以在一个注解内添加外部配置文件位置的数组,我们也可以用PropertySources内部包含多个PropertySource
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.io.support.PropertySourceFactory;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
String name() default "";
String[] value();
boolean ignoreResourceNotFound() default false;
String encoding() default "";
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
@PropertySources :内部可以指定多个@PropertySource
类似于xml文件配置的这一步:<context:property-placeholder location="classpath:person.properties"/> 我们要用@PropertySource这个注解来指定外部文件的位置:@PropertySource(value = {"classpath:/person.properties"})
package com.hutu.config;
import com.hutu.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//使用@PropertySource读取外部配置文件中的key/value保存到运行的环境变量中
//加载完外部配置文件以后使用${}取出配置文件的值
@PropertySource(value = {"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person() {
return new Person();
}
}
然后,我们就可以用@Value
,里面用${}就可以引用配置文件中的值了:
package com.hutu.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
//使用@Value赋值
//1.基本的数值
//2.可以写SpEL: #{}
//3.可以写${}:取出配置文件中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
}
再来运行测试方法,运行结果如下:这个时候,nickName就有值了:
======================
Person(name=张三, age=18, nickName=Tom)
IOC容器中已注册的bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalCommonAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerFactory
IOC容器中已注册的bean:mainConfigOfPropertyValues
IOC容器中已注册的bean:person
还可以用Environment里面的getProperty()方法来获取
@Test
public void test01() {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
System.out.println("======================");
Person person = context.getBean(Person.class);
System.out.println(person);
System.out.println("======================");
Environment environment = context.getEnvironment();
String property = environment.getProperty("person.nickName");
System.out.println(property);
System.out.println("======================");
printBeans(context);
}
private void printBeans(ApplicationContext context) {
String[] definitionNames = context.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println("IOC容器中已注册的bean:" + name);
}
}
结果
======================
Person(name=张三, age=18, nickName=Tom)
======================
Tom
======================
IOC容器中已注册的bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.annotation.internalCommonAnnotationProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerProcessor
IOC容器中已注册的bean:org.springframework.context.event.internalEventListenerFactory
IOC容器中已注册的bean:mainConfigOfPropertyValues
IOC容器中已注册的bean:person