1.掌握spring的属性注入的方法:

1.1构造方法注入普通值---------<constructor-arg>标签的使用

首先新建一个类

package spring.day1.demo3;

public class car {

    private String name;
private double price; public car(String name, Double price) {
this.name = name;
this.price = price;
} @Override
public String toString() {
return "car [name=" + name + ", price=" + price + "]";
} }

在编写applicationContext2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- =======================引入spring的关于bean的约束 ========================= -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!-- ================spring属性注入=============================== -->
<!-- 构造方法注入 -->
<bean id="car" class="spring.day1.demo3.car">
<constructor-arg name="name" value="宝马"></constructor-arg>
<constructor-arg name="price" value="500000"></constructor-arg>
</bean> </beans>

在编写测试类SpringDemo3

package spring.day1.demo3;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class springDemo3 { @Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
car bean = (car) applicationContext.getBean("car");
System.out.println(bean);
}
}

运行结果如下

1.2set方法注入普通值和对象属性(ref的使用)------<property>标签的使用

新建一个类car2

package spring.day1.demo3;

public class car2 {

    private String name;
private double price; public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "car2 [name=" + name + ", price=" + price + "]";
} }

新建一个Employee类

package spring.day1.demo3;

public class employee {
private String name;
private car2 car; public void setName(String name) {
this.name = name;
} public void setCar(car2 car) {
this.car = car;
} @Override
public String toString() {
return "Employee [name=" + name + ", car=" + car + "]";
}
}

在applicationContext2.xml中加入以下

<!-- set方法对car2注入普通值 -->
<bean id="car2" class="spring.day1.demo3.car2">
<property name="name" value="奔驰"></property>
<property name="price" value="600000"></property>
</bean> <!-- set方法注入对象 -->
<bean id="employee" class="spring.day1.demo3.employee">
<property name="name" value="张三"></property>
<property name="car" ref="car2"></property>
</bean>

在测试类SpringDemo3加入以下

@Test
public void demo2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
employee bean = (employee) applicationContext.getBean("employee");
System.out.println(bean);
}

运行截图如下

1.3SPEL方式注入普通值和对象值(Spring3.0以后)

SpEL:Spring Expression Language,Spring的表达式语言。

语法格式:#{SPEL}

新建一个类carInfo

package spring.day1.demo3;

public class carInfo {
private String name;
private double price; public String getName() {
return "小三轮儿";
} public double getPrice() {
return Math.random()*1000;
} }

修改applicationContext2.xml为如下

<?xml version="1.0" encoding="UTF-8"?>
<!-- =======================引入spring的关于bean的约束 ========================= -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!-- ================spring属性注入=============================== -->
<!-- 构造方法注入 -->
<bean id="car" class="spring.day1.demo3.car">
<constructor-arg name="name" value="宝马"></constructor-arg>
<constructor-arg name="price" value="500000"></constructor-arg>
</bean> <!-- set方法对car2注入普通值 -->
<!-- <bean id="car2" class="spring.day1.demo3.car2">
<property name="name" value="奔驰"></property>
<property name="price" value="600000"></property>
</bean> --> <!-- set方法注入对象 -->
<!-- <bean id="employee" class="spring.day1.demo3.employee">
<property name="name" value="张三"></property>
<property name="car" ref="car2"></property>
</bean> --> <!-- SPEL方式 -->
<bean id="carInfo" class="spring.day1.demo3.carInfo"></bean> <bean id="car2" class="spring.day1.demo3.car2">
<property name="name" value="#{carInfo.name}"></property>
<property name="price" value="#{carInfo.getPrice()}"></property>
</bean> </beans>

在测试类SpringDemo3加入以下

@Test
public void demo3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
car2 bean = (car2) applicationContext.getBean("car2");
System.out.println(bean);
}

运行截图如下

1.4P名称空间方式注入普通值和对象(Spring2.5以后)了解

2.集合属性注入(了解)

1.新建CollectionBean类

2.在applicationContext2.xml中加入如下代码

3.新建测试类测试

<!-- =======================spring集合属性的注入============================ -->
<bean id="collectionBean" class="zcc.spring.demo5.CollectionBean">
<!-- 数组类型 -->
<property name="arrs">
<list>
<value>张三</value>
<value>李四</value>
<value>王麻子</value>
</list>
</property>
<!-- List集合类型 -->
<property name="list">
<list>
<value>一号</value>
<value>二号</value>
<value>三号</value>
</list>
</property>
<!-- Set集合类型 -->
<property name="set">
<set>
<value>a</value>
<value>b</value>
<value>c</value>
</set>
</property>
<!-- Map集合类型 -->
<property name="map">
<map>
<entry key="数字" value="1"></entry>
<entry key="性别" value="男"></entry>
</map>
</property>
</bean>
package zcc.spring.demo5;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set; /*
* 集合属性的注入
*/
public class CollectionBean {
private String []arrs;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
+ "]";
} }
package zcc.spring.demo5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
* 集合类型的属性注入
*/
public class SpringDemo5 { @Test
public void demo1() {
//创建spring的工厂来实例化xml中所有的bean对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(bean);
} }

spring的基于XML方式的属性注入的更多相关文章

  1. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  2. Spring 中IOC(控制反转)&& 通过SET方式为属性注入值 && Spring表达式

    ### 1. Spring IoC IoC:Inversion of control:控制反转:在传统开发模式下,对象的创建过程和管理过程都是由开发者通过Java程序来实现的,操作权在开发者的Java ...

  3. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  4. Spring-注入方式(基于xml方式)

    1.基于xml方式创建对象 <!--配置User类对象的创建 --> <bean id="user" class="com.at.spring5.Use ...

  5. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  6. 表达式SpEL方式的属性注入

    -----------------------siwuxie095 表达式 SpEL 方式的属性注入 表达式 SpEL 方式的属性注入是 Spring 3.x 版本后提供的方式 1.编写一个普通类 B ...

  7. 命名空间p方式的属性注入

    ---------------------siwuxie095 命名空间 p 方式的属性注入 命名空间 p 方式的属性注入是 Spring 2.x 版本后提供的方式 1.编写一个普通类 Book.ja ...

  8. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  9. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

随机推荐

  1. C# 委托 事件

    一:什么叫委托 通过反射发现,委托其实是一个类,继承自System.MulticastDelegate,但是System.MulticastDelegate这个类是特殊类,不能被继承 二:委托的声明 ...

  2. 【转载】C#工具类:Json操作帮助类

    Json序列化和反序列化在程序开发中时常会遇到,在C#中可以使用很多种方法实现对数据的Json序列化和反序列化,封装一个Json操作工具类来简化相应的操作,该工具类中包含以下功能:对象转JSON.数据 ...

  3. C#开发微信公众平台-就这么简单(附Demo)转载

    C#开发微信公众平台-就这么简单(附Demo)  来源:https://www.cnblogs.com/xishuai/p/3625859.html#!comments 写在前面 阅读目录: 服务号和 ...

  4. C#窗体越界时鼠标还能回到初始坐标位置

    对窗体加越界限制后,鼠标拖动窗体越界时,窗体不能动,鼠标位置可动,但窗体不再越界时,鼠标位置还能回到鼠标按下时相对窗体的坐标:1.首先创建一个窗体Form1,然后在窗体上拖一个button1按钮(主要 ...

  5. 【问题】vs IIS破除文件上传限制最全版

    今天在测试一下上传文件的时候发现iis和配置存在上传文件大小限制(IIS默认大小30M,最大运行为2g:2147483647),百度了一部分资料有些发布到IIS好使,但是在VS调试中不好使.于是自己不 ...

  6. intellij error updating changes svn解决办法

    乌龟检出的svn版本为1.8,而1.8在IntelliJ 上跑起来貌似有问题, 经过多次尝试,当Format改为1.7后,问题被解决.

  7. js正则表达式之人民币匹配

    人民币格式匹配 小写格式:¥ 符号 和 整数值 与小数3部分组成. (0)代码与运行结果 { // 匹配人民币 let [reg, info, rmb, result] = [ /^(¥)(-?[0- ...

  8. crontab架构和格式

    crontab架构图 分时日月周*****my command(可以是一个linux命令,也可以是一个脚本文件,可以是shell格式也可以是python格式,也可是java格式.....) 按照格式编 ...

  9. Spring学习之旅(六)Spring AOP工作原理初探

    AOP(Aspect-Oriented  Programming,面向切面编程)是Spring提供的关键技术之一. AOP基于IoC,是对OOP(Object-Oriented Programming ...

  10. C# Json.Net解析实例

    本文以一个简单的小例子,简述Json.Net的相关知识,仅供学习分享使用,如有不足之处,还请指正. 概述 Json.Net is a Popular high-performance JSON fra ...