依赖注入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}

Spring讲解(三)的更多相关文章

  1. Spring第三天

    Spring第三天 整体课程安排(3天+2天): 第一天:Spring框架入门.IoC控制反转的配置管理.Spring Web集成.Spring Junit集成. 第二天:Spring AOP面向切面 ...

  2. Spring中三个重要概念 IOC AOP Bean

    Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...

  3. Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!

    点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...

  4. Spring的三种通过XML实现DataSource注入方式

    Spring的三种通过XML实现DataSource注入方式: 1.使用Spring自带的DriverManagerDataSource 2.使用DBCP连接池 3.使用Tomcat提供的JNDI

  5. spring ioc三种注入方式

    spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...

  6. Redis实战之征服 Redis + Jedis + Spring (三)

    一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 通过spring-data-redis完 ...

  7. 征服 Redis + Jedis + Spring (三)—— 列表操作【转】

    一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Re ...

  8. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  9. spring第三篇

    在昨天下午更新sprin第二篇中,叙述了将对象交给spring创建和管理,今天在spring第三篇中,主要写两个点一是spring的思想 二是spring中bean元素的属性配置. 1 spring思 ...

  10. (转)Spring的三种实例化Bean的方式

    http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...

随机推荐

  1. 一次Linux服务器空间满的随笔解决记录

    昨天突然无法上传文件到服务器上的,FTP工具总是到99%就卡住了.查了一下说可能是服务器满了. 赶紧用 df -h 命令查看空间使用情况.果然100%了. 想想上次查询才不到50%,怎么突然就满了了呢 ...

  2. windows之cmd常用命令

    一.简单介绍 CMD全称command,即命令提示符,是内置在windows图形操作系统内的磁盘操作系统,通过CMD可以方便用户查询比较复杂的信息或快速查找实现某些功能等,比如说打开文件.系统设置等操 ...

  3. 多线程模拟生产者消费者示例之Lock

    public class Test { public static void main(String[] args) { List<String> list = new ArrayList ...

  4. tab切换中的滚动条下拉分页带来的问题

    相信做过tab切换中滚动条下拉分页的童鞋都知道,我们在用scroll方法来做滚动条下拉分页的时候,都是有bug,切换中间的内容会互相影响,为了解决这个问题,我总结了2种方法: 1.方法一: <! ...

  5. BZOJ 3294: [Cqoi2011]放棋子(计数dp)

    传送门 解题思路 设\(f[i][j][k]\)表示前\(k\)个颜色的棋子占领了\(i\)行\(j\)列的方案数,那么转移时可以枚举上一个颜色时占领的位置,\(f[i][j][k]=\sum\lim ...

  6. RHEL/CentOS通用性能优化、安全配置参考

    RHEL/CentOS通用性能优化.安全配置参考 本文的配置参数是笔者在实际生产环境中反复实践总结的结果,完全适用绝大多数通用的高负载.安全性要求的网络服务器环境.故可以放心使用. 若有异议,欢迎联系 ...

  7. python装饰器参数那些事_接受参数的装饰器

    # -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...

  8. (66) c# async await

    1.使用 async await 2.返回值 static void Main(string[] args) { Program p = new Program(); Console.WriteLin ...

  9. ReplicatorLayer 复制图层

    使用文档介绍: #import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AVAILABLE (10.6, 3.0, ...

  10. ORM模型类介绍,

    所有的软件开发过程中,都会涉及到对象和关系型数据库,在用户层面和业务逻辑层面,程序员编写代码都是面向对象的,当我们对象的信息发生变化的时候,都需要将对应的信息,传到关系型数据库中.而在此之前,需要我们 ...