作者:小晴天9927 | 来源:互联网 | 2023-05-19 17:21
这篇文章给大家介绍Spring中如何将值注入到Bean中,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
在Spring中,有三种方式注入值到 bean 属性。
正常的方式
快捷方式
“p” 模式
新建一个User类,它包含username和password两个属性,现在使用spring的IOC注入值到该bean。
package com.example.pojo;
public class User
{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username= username;
}
public String getPassword() {
return type;
}
public void setPassword(String password) {
this.password= password;
}
}
1.正常方式
在一个“value”标签注入值,并附有“property”标签结束。
2.快捷方式
注入值“value”属性。
3. “p” 模式
通过使用“p”模式作为注入值到一个属性。
记住声明 xmlns:p=”http://www.springframework.org/schema/p" 在Spring XML bean配置文件。
关于Spring中如何将值注入到Bean中就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。