Spring中如何使用@Value注解实现给Bean属性赋值

网友投稿 294 2022-12-18

Spring中如何使用@Value注解实现给Bean属性赋值

目录属性赋值@Value注解的定义:测试1.在添加了Spring依赖的Maven项目中创建2.在resources目录下创建一个配置文件person.properties3.创建配置类4.创建测试类进行测试5.测试结果:如何给Bean的属性赋值(注入)1.通过构造方法设置值。2.设置注入(通过set方法)

属性赋值

只用Spring注解开发的时候,可以使用@Value搭配@PropertySource注解进行给Bean的属性进行赋值。

@Value

@Value注解的定义:

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Value {

/**

* The actuXXBbEDiAnal value expression: for example {@code #{systemProperties.myProp}}.

*/

String value();

}

可以看到@Value注解接受的参数只能是字符串,所以使用时参数都需要带上双引号。并且可以在属性,方法,参数等位置上标注。value属性支持以下三种类型:

基本数据类型,包括String,int,boolean等类型。

SpEL,#{}的形式即表示使用SpEL表达式,一般用于获取环境中其他bean的属性,当使用Elivis运算符“表达式1?:表达式2”,比如#{ “obj.property? :default_value” }形式时,这里的obj是取当前容器中的对象,default_value就是前面的值为空时的默认值。

在主配置类中通过@PropertySource注解加载配置文件,然后通过${ property : default_value}的形式取配置文件中的值,其中default_value表示前面的值为空时的默认值。

测试

1.在添加了Spring依赖的Maven项目中创建

以下两个实体类Student类和Teacher类

public class Teacher {

public int id;

public String name;

public Teacher() {

// TODO Auto-generated constructor stub

System.out.println("teacher-----创建");

}

public Teacher(int id, String name) {

super();

this.id = id;

this.name = name;

}

//省略getter和setter方法

}

在Student类中使用${}取配置文件的值,#{}取容器中的teacher对象的name属性

public class Student{

@Value("1")

private int id;

@Value("${student.name}")

private String name;

@Value("${student.address:北京}")

private String address;

@Value("#{teacher.name?:'老子'}")

private String teacherName;

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", address=" + address + ", teacherName=" + teacherName + "]";

}

//省略getter和setter方法

}

2.在resources目录下创建一个配置文件person.properties

添加以下值:

student.name=张三

person.address=广州市

3.创建配置类

使用@PropertySource注解读取外部配置文件person.properties中的key/value保存到运行的环境变量中。

@Configuration

@PropertySource("person.properties")

public class SpringConfig2 {

@Bean

public Student student() {

return new Student();

}

@Bean

public Teacher teacher() {

return new Teacher(2,"老李");

}

}

4.创建测试类进行测试

编写代码获取容器中的id为student的bean和直接获取配置环境的属性值。

public class IoCTest {

@Test

public void test() {

//获取Spring的IOC容器

AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfig2.class);

//从容器中获取bean

Student stu = (Student) apphttp://licationContext.getBean("student");

System.out.println(stu);

//获取环境变量

ConfigurableEnvironment environment = applicationContext.getEnvironment();

String property = environment.getProperty("person.address");

System.out.println(property);

//关闭容器

applicationContext.close();

}

}

5.测试结果:

如何给Bean的属性赋值(注入)

1.通过构造方法设置值。

2.设置注入(通过set方法)

通过property标签(内部是通过调用类的set方法将值注入)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用Spring自定义实现IOC和依赖注入(注解方式)
下一篇:聊聊Spring——AOP详解(AOP概览)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~