Spring属性注入DI
1.构造方法注入(只需提供一个构造方法)
javabean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.mickeymouse.ioc;
public class Car {
private String name;
private Double price;
//提供构造方法
public Car(String name, Double price) {
super ();
this .name = name;
this .price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]" ;
}
}
|
Xml配置
1
2
3
4
5
|
<!-- Bean的属性注入:构造方法注入 -->
<bean id= "car" class = "com.mickeymouse.ioc.Car" >
<constructor-arg name= "name" value= "宝马" ></constructor-arg>
<constructor-arg name= "price" value= "4343434.0" ></constructor-arg>
</bean>
|
测试类
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 属性注入之构造方法注入
*/
@Test
public void test5(){
//获取配置文件
String path = "applicationContext.xml" ;
//加载配置文件
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
Car car = (Car) applicationContext.getBean( "car" );
System.out.println(car);
}
|
结果:
2 . set方法注入(只需提供set方法)
javabean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.mickeymouse.ioc;
public class Car {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this .price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]" ;
}
}
|
配置文件
1
2
3
4
5
|
<!-- Bean的属性注入:Set方法注入 -->
<bean id= "car" class = "com.mickeymouse.ioc.Car" >
<property name= "name" value= "兰博基尼" />
<property name= "price" value= "3423432.0" />
</bean>
|
测试类:
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 属性注入之set方法注入
*/
@Test
public void test6(){
//获取配置文件
String path = "applicationContext.xml" ;
//加载配置文件
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
Car car = (Car) applicationContext.getBean( "car" );
System.out.println(car);
}
|
结果图
3 . P名称空间的注入----->Spring的2.5版本才开始
一 . 引入P名称空间:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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">
<!-- bean definitions here -->
<!-- Bean的属性注入:p名称空间注入 -->
< bean id = "car" class = "com.mickeymouse.ioc.Car" p:name = "长安宝马" p:price = "432423.5" ></ bean >
</ beans >
|
二 . javabean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.mickeymouse.ioc;
public class Car {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this .price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]" ;
}
}
|
测试类
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 属性注入之P名称空间注入
*/
@Test
public void test7(){
//获取配置文件
String path = "applicationContext.xml" ;
//加载配置文件
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
Car car = (Car) applicationContext.getBean( "car" );
System.out.println(car);
}
|
结果图:
4 . Bean属性注入之 SPEL表达式方式--->sping3.0以后才开始出现
javabean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.mickeymouse.ioc;
public class Car {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this .price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]" ;
}
}
|
XML表达式:
1
2
3
4
5
|
<!-- Bean的属性注入:SPEL表达式方式 -->
< bean id = "car" class = "com.mickeymouse.ioc.Car" >
< property name = "name" value = "#{'劳斯莱斯'}" />
< property name = "price" value = "#{3434.00}" />
</ bean >
|
测试类
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 属性注入之SPEL表达式注入
*/
@Test
public void test8(){
//获取配置文件
String path = "applicationContext.xml" ;
//加载配置文件
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
Car car = (Car) applicationContext.getBean( "car" );
System.out.println(car);
}
|
结果
XML关于数组,集合(list map set properties)的属性赋值配置写法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<bean id= "collectionBean" class = "com.itheima.spring.demo6.CollectionBean" >
<property name= "arrs" >
<list>
<value>老王</value>
<value>凤姐</value>
<value>如花</value>
</list>
</property>
<property name= "list" >
<list>
<value>豆豆</value>
<value>奶茶</value>
<value>绿茶</value>
</list>
</property>
<property name= "set" >
<set>
<value>王尧</value>
<value>刘健</value>
<value>周玉</value>
</set>
</property>
<property name= "map" >
<map>
<entry key= "老王2" value= "38" />
<entry key= "凤姐" value= "38" />
<entry key= "如花" value= "29" />
</map>
</property>
<property name= "properties" >
<props>
<prop key= "username" >root</prop>
<prop key= "password" > 123 </prop>
</props>
</property>
</bean>
|
转载于:https://my.oschina.net/mickeymouse/blog/519123
Spring属性注入DI相关推荐
- Spring属性注入的三种方式(超详细)
属性注入的三种方式 使用set方法进行注入 使用有参构造函数进行注入 使用p名称空间注入 首先了解下面两个名词的含义: IOC:控制反转(Inversion of Control,缩写为IoC),是面 ...
- 2.3.2 spring属性注入-注解注入-半注解注入-后序
代码: spring属性注入-注解注入-半注解后序.zip - 蓝奏云文件大小:15.2 K|https://www.lanzouw.com/iCjbVvpvxaf 上一个博客的半注解是,一个类在xm ...
- 2.3.3 spring属性注入-注解注入-全注解-配置类扫描
代码: spring2-属性注入-全注解-配置类扫描.zip - 蓝奏云文件大小:12.3 K|https://www.lanzouw.com/iPK5vvq21uf 这个博客,我们使用全注解的方式来 ...
- 2.3.1 spring属性注入-注解注入-半注解方式-前序
注解注入自由度高,可以对部分类使用注解,也可以对所有类都使用注解. 下面代码是的Category使用xml定义java Bean, Product使用注解来定义java Bean 1.定义Catogo ...
- 2.2 Spring属性注入-构造方法
代码: spring2.2-构造方法注入.zip - 蓝奏云文件大小:14.9 K|https://www.lanzouw.com/iLbBBvpug4b 1.定义Category和Product类, ...
- 2.1、spring属性注入-Set方法注入
代码地址:spring2.1.zip - 蓝奏云文件大小:15.1 K|https://www.lanzouw.com/imlEwvptwre Set方法注入的原理是spring先通过指定id的类的无 ...
- Spring依赖注入(DI)
2019独角兽企业重金招聘Python工程师标准>>> 1.面向接口编程 什么是面向接口编程呢?我个人认为,就是在我们的系统分析和架构中,首先,分清层次和依赖关系,每个层次不直接向上 ...
- Spring依赖注入DI
DI是Dependency Injection的缩写,依赖注入的意思,依赖了Spring容器,进行set注入,这里还是以一个例子进行阐述 首先我们创建两个实体类 package com.zhiying ...
- spring属性注入
注入方式 set方法注入 构造函数注入 p名称空间注入 spel注入(#表示理解为引用) 练习代码: <!-- 第二天 set注入 index:索引 ref:引用对象 type:参数类型--&g ...
最新文章
- 牛客练习赛61 E 相似的子串(二分+哈希)难度⭐⭐⭐
- Leetcode1700. 无法吃午餐的学生数量[C++题解]:模拟题简单,用queue
- MFC的SendMessage函数详解
- android phpmyadmin,从android studio向phpmyadmin添加数据时出现问题
- php无法完成文件上传,php – Fine Uploader无法上传文件
- ORACLE:Health Monitor
- base64核心原理
- prove, verify, bear out, demonstrate, confirm, validate, testify, certify 的区别
- mysql 优化之 is null ,is not null 索引使用测试
- 蚂蚁中间件团队Java面试题:Netty+Redis+Kafka+MongoDB+分布式
- 多mysql实例下开发需要注意主从同步延迟
- 相对熵与交叉熵_熵、KL散度、交叉熵
- js获取下月时间_js 获取 本周、下周、本月、下月、本季度、下季度的开始结束日期...
- 到2030年丰田将斥资135亿美元开发电动汽车电池技术及供应系统
- 封条格式用word怎么打_汽车密封条保养膏怎么用?大师来教你正确方法
- IOS开发-表视图LV3导航控制器
- mysqldump备份所有数据库,恢复单个库的场景预演
- JQuery左右切换实现
- DSP开发,使用CCS软件建立工程以及烧录
- MyBatis源码阅读指南
热门文章
- linux怎么配置gvim教程,Vim配置详解_Linux教程_Linux公社-Linux系统门户网站
- nc提示java过期_用友U8 软件经常出现“超时已过期”的提示
- Foxmail7.2导入账户信息
- 学海无涯苦作舟?做梦
- 三七出品--自制国内无人机航拍视角下车辆检测数据集
- 2019年互联网行业从业前景如何?
- html 打印出word文档,JS如何实现获取word文档内容并输出显示到html页面
- taro 小程序转h5之后报错_使用taro框架将手百小程序转成h5总结
- HCIP-H12-223多选题库
- 光影魔术手可以切片吗_实体框架魔术独角兽(还有更多!)现已开放,并且可以收回