依赖注入Bean属性,使用xml配置

1、构造方法注入

案例代码演示

public class User {

    private String username;
private String password;
private Integer age; public User() {} public User(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
} ========================================================================================
通过构造方法注入参数
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!---->
<bean id="user" class="com.example.demo.testservice.User">
<!--相当于调用了有参构造
public User(String username, String password, Integer age) {...}
-->
<constructor-arg name="username" value="zhangsan"/>
<constructor-arg name="password" value="123456"/>
<constructor-arg name="age" value="99"/>
</bean>
</beans> ========================================================================================
测试函数
public class ServiceTest { public static void main(String[] args) { ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context1.getBean("user");
System.out.println(user);
}
}

执行测试函数得到如下结果:

User{username='zhangsan', password='123456', age=99}

上面的bean.xml还可以通过索引注入参数,其他不变,修改 bean.xml 如下
通过索引注入参数
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.example.demo.testservice.User">
<!--相当于调用了有参构造
public User(String username, String password, Integer age) {...}
-->
<constructor-arg index="0" value="李四" type="java.lang.String"/>
<constructor-arg index="1" value="112233" type="java.lang.String"/>
<constructor-arg index="2" value="44" type="java.lang.Integer"/>
</bean>
</beans>

执行测试函数得到如下结果:

User{username='李四', password='112233', age=44}

2、属性setter方法注入(有两种)

上面的 User 和 测试函数 ServiceTest 不变,只需要修改 bean.xml即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.example.demo.testservice.User">
<property name="age" value="11"/>
<property name="password" value="666"/>
<property name="username" value="王麻子"/>
</bean>
</beans> ========================================================================================= <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.example.demo.testservice.User">
<property name="age">
<value>11</value>
</property>
<property name="password">
<value>666</value>
</property>
<property name="username">
<value>王麻子</value>
</property>
</bean>
</beans>

上面 两种配置 bean.xml 都可以,采用第一种配置的比较只管方便。

测试结果都如下:

User{username='王麻子', password='666', age=11}

3、p命名空间注入

上面的 User 和 测试函数 ServiceTest 不变,只需要修改 bean.xml即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p ="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.example.demo.testservice.User" p:age="111" p:password="888" p:username="秦始皇" />
</beans>

这种方法不常用,有兴趣的朋友可以对比下 p 命名空间的 bean.xml 和构造方法注入、setter 方法注入的 bean.xml 区别就是加了一个===》xmlns:p ="http://www.springframework.org/schema/p",就可以使用 p 命名空间了。

运行结果如下:

User{username='秦始皇', password='888', age=111}

4、集合注入

List、Set、Map、Properties

public class Coder {

    private List<String> cars;  // 车

    private Set<String> pats;   // 宠物

    private Map<String,String> information; // 信息

    private Properties mysqlInfo;   // Mysql 连接信息

    public Properties getMysqlInfo() {
return mysqlInfo;
} public void setMysqlInfo(Properties mysqlInfo) {
this.mysqlInfo = mysqlInfo;
} public Map<String, String> getInformation() {
return information;
} public void setInformation(Map<String, String> information) {
this.information = information;
} public Set<String> getPats() {
return pats;
} public void setPats(Set<String> pats) {
this.pats = pats;
} public List<String> getCars() {
return cars;
} public void setCars(List<String> cars) {
this.cars = cars;
} @Override
public String toString() {
return "Coder{" +
"cars=" + cars +
", pats=" + pats +
", information=" + information +
", mysqlInfo=" + mysqlInfo +
'}';
}
} =========================================================================================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="coder" class="com.example.demo.testservice.Coder">
<!--List 数据注入-->
<property name="cars">
<list>
<value>ofo</value>
<value>宝马</value>
<value>奔驰</value>
</list>
</property> <!--Set数据注入-->
<property name="pats">
<set>
<value>猪</value>
<value>马</value>
<value>牛</value>
</set>
</property> <!--Map 数据注入-->
<property name="information">
<map>
<entry key="name" value="王八蛋"/>
<entry key="age" value="99"/>
<entry key="password" value="8888"/>
</map>
</property> <!--Properties 数据注入-->
<property name="mysqlInfo">
<props>
<prop key="url">mysql:jdbc://localhost:3306/sample</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
=========================================================================================测试函数
public class ServiceTest { public static void main(String[] args) { ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
Coder coder = (Coder) context1.getBean("coder");
System.out.println("车:"+coder.getCars());
System.out.println("宠物:"+coder.getPats());
System.out.println("个人信息:"+coder.getInformation());
System.out.println("数据库信息信息:"+coder.getMysqlInfo());
}
}

运行结果如下:

车:[ofo, 宝马, 奔驰]

宠物:[猪, 马, 牛]

个人信息:{name=王八蛋, age=99, password=8888}

数据库信息信息:{password=root, url=mysql:jdbc://localhost:3306/sample, username=root}

最新文章

  1. 利用边框border的属性做小符号
  2. css绝对定位问题
  3. python AES 双向对称加密解密
  4. Maven加依赖包
  5. [译]36 Days of Web Testing(一)
  6. Linux查看硬件信息以及驱动设备的命令
  7. [原]容器学习(二):动手模拟AOP
  8. java提高篇(三)-----理解java的三大特性之多态
  9. SQL Server 索引维护sql语句
  10. 排座椅(seat)
  11. PC网页版、移动客户端、Wap版 有什么不同
  12. [JSOI2008]Blue Mary开公司[李超线段树]
  13. 这可能是最为详细的Docker入门吐血总结
  14. 关于jstl中碰到的Property &#39;username&#39; not found on type java.lang.String异常
  15. (转载)关于usr/bin/ld: cannot find -lxxx问题总结
  16. 第一章 C#入门 (Windows窗体应用程序)(三)
  17. httpd-vhosts.conf配置
  18. 安装python3后使用pip和pip3的区别是什么?
  19. jquery里面的$.each()方法
  20. HTML5 web 存储

热门文章

  1. 洛谷P3374(线段树)(询问区间和,支持单点修改)
  2. 汇编 “error A2031”
  3. EditText设置/隐藏光标位置、选中文本和获取/清除焦点(转)
  4. 用 Flask 来写个轻博客 (31) — 使用 Flask-Admin 实现 FileSystem 管理
  5. python3+django2 个人简单博客实现 -正在施工
  6. A Bite Of React(2) Component, Props and State
  7. python基础--二分查找
  8. LOJ6682 梦中的数论
  9. 分布式ID的雪花算法及坑
  10. python3 requests库学习笔记(MOOC网)