依赖注入Bean属性




package cn.itcast.spring.di;
//构造方法注入
public class Car {
private String name;
private String color; public Car(String name, String color) {
super();
this.name = name;
this.color = color;
} @Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
} package cn.itcast.spring.di;
//setter方法注入
public class Car2 {
private String name;
private String color; public void setName(String name) {
this.name = name;
} public void setColor(String color) {
this.color = color;
} @Override
public String toString() {
return "Car2 [name=" + name + ", color=" + color + "]";
} } package cn.itcast.spring.di; //employee 引用一个复杂数据类型 Car2
public class Employee {
private String name;
private Car2 car2; public void setCar2(Car2 car2) {
this.car2 = car2;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Employee [name=" + name + ", car2=" + car2 + "]";
} } <!-- 使用构造函数 完成属性注入 -->
<bean id="car" class="cn.itcast.spring.di.Car">
<!-- 构造函数注入 通过constructor-arg -->
<!-- 通过索引和类型,指定注入参数位置 -->
<constructor-arg index="0" type="java.lang.String" value="宝马"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="红色"></constructor-arg>
</bean> <!-- 使用setter 方法注入 -->
<bean id="car2" class="cn.itcast.spring.di.Car2">
<!-- setter 方法注入 ,根据setter 方法编写注入属性 -->
<property name="name" value="保时捷"></property>
<property name="color" value="黑色"></property>
</bean> <!--注入一个复杂类型,通过<property> ref属性 引用其他Bean -->
<bean id="employee" class="cn.itcast.spring.di.Employee">
<property name="name" value="小丽"></property>
<!-- ref 引用另一个Bean -->
<property name="car2" ref="car2"></property>
</bean>
// 测试复杂类型 注入
@Test
public void demo3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Employee employee = (Employee) applicationContext.getBean("employee");
System.out.println(employee);
} // 测试setter 方法注入
@Test
public void demo2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
} // 测试构造函数属性注入
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
3、p名称空间的使用
从spring2.5版本,为了简化 bean属性注入写法,引入p名称空间
p:<属性名>="xxx" 引入常量值
p:<属性名>-ref="xxx" 引入其他bean对象
使用p名称空间之前,先引用p名称空间:"xmlns:p="http://www.springframework.org/schema/p" 加入bean 元素
<bean id="employee" class="cn.itcast.spring.di.Employee">
<property name="name" value="小丽"></property>
<property name="car2" ref="car2"></property>
</bean>
简化为:
1 <!-- 使用p名称空间 -->
<bean id="employee" class="cn.itcast.spring.di.Employee" p:name="小丽" p:car2="#{car2}" />
4、SpEL (Spring Expression Language ) 的使用
Spring 3.0 创建了一种新的方式用以配置对象的注入(set 注入或者构造参数注入),它便是SpEL (Spring Expression Language)
语法格式 : #{...}
用法一: 向一个Bean 引用另一个Bean
<property name="car2" ref="car2" /> ------ > <property name="car2" value="#{car2}" /
用法二: 使用另一个Bean属性 为当前Bean 属性赋值
public class CarInfo {
// 说明CarInfo 有个 name 属性
public String getName() {
return "奇瑞QQ";
}
// 是一个普通方法
public String initColor() {
return "白色";
}
}
<bean id="carinfo" class="cn.itcast.spring.di.CarInfo"></bean>
<bean id="car3" class="cn.itcast.spring.di.Car2">
<property name="name" value="#{carinfo.name}"></property>
</bean>
car3 的name值,调用 carinfo对象 getName() 方法
用法三 : 使用另一个Bean 方法 ,为当前Bean 属性赋值
public class CarInfo {
// 说明CarInfo 有个 name 属性
public String getName() {
return "奇瑞QQ";
}
// 是一个普通方法
public String initColor() {
return "白色";
}
}
<bean id="carinfo" class="cn.itcast.spring.di.CarInfo"></bean>
<bean id="car3" class="cn.itcast.spring.di.Car2">
<property name="name" value="#{carinfo.name}"></property>
<property name="color" value="#{carinfo.initColor()}"></property>
</bean>
car3 的color值,有carinfo对象 initColor方法提供的
用法四: 读取properties 文件的值
5、如何向一个Bean对象 ,注入集合类型的属性
// 集合Bean ,向Bean中注入 List 、 Set 、Map 、Properties 对应数据
public class CollectionBean {
private List<String> hobbies;
private Set<Car2> cars;
private Map<String, Integer> webSiteVisitMap;
private Properties employees;
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCars(Set<Car2> cars) {
this.cars = cars;
}
public void setWebSiteVisitMap(Map<String, Integer> webSiteVisitMap) {
this.webSiteVisitMap = webSiteVisitMap;
}
public void setEmployees(Properties employees) {
this.employees = employees;
}
@Override
public String toString() {
return "CollectionBean [hobbies=" + hobbies + ", cars=" + cars
+ ", webSiteVisitMap=" + webSiteVisitMap + ", employees="
+ employees + "]";
}
}
<!-- 向Bean注入 集合类型属性 -->
<bean id="collectionBean" class="cn.itcast.spring.di.CollectionBean">
<!-- 注入list 基本数据类型 -->
<property name="hobbies">
<list>
<value>体育</value>
<value>音乐</value>
<value>爬山</value>
</list>
</property>
<!-- 注入 Set 复杂数据类型 -->
<property name="cars">
<set>
<ref bean="car2"/>
<ref bean="car3"/>
</set>
</property>
<!-- 注入Map -->
<property name="webSiteVisitMap">
<map>
<entry key="传智播客" value="100"></entry>
<entry key="黑马程序员" value="110"></entry>
</map>
</property>
<!-- 注入property -->
<property name="employees">
<props>
<prop key="张三">传智播客</prop>
<prop key="李四">黑马程序员</prop>
</props>
</property>
</bean>
</beans>
// 测试集合属性赋值
@Test
public void demo5() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CollectionBean collectionBean = (CollectionBean) applicationContext
.getBean("collectionBean");
System.out.println(collectionBean);
}
依赖注入Bean属性的更多相关文章
- 依赖注入Bean属性——手动装配Bean
一.构造方法注入 其中,可以根据不同的参数列表调用不同的重载的构造方法: 其中,基本数据类型没有包,引用类型都有包路径,基本类型对应封装类: 二.通过property标签调用类的set方法注入 三.通 ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired
Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...
- Spring入门(4)-注入Bean属性
Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...
- Spring学习笔记--注入Bean属性
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...
- Spring学习(五)-----注入bean属性的三种方式( 1: 正常的方式 2: 快捷方式 3: “p” 模式)
在Spring中,有三种方式注入值到 bean 属性. 正常的方式 快捷方式 “p” 模式 看到一个简单的Java类,它包含两个属性 - name 和 type.稍后将使用Spring注入值到这个 b ...
- SpringMVC的filter怎么使用Autowired依赖注入bean
有的时候根据我们业务的需要,我们需要在web项目中定义一个自己的filter,并想在这个filter中使用@Autowired注入bean供我们使用.如果直接使用的话是不行的,需要我们在xml文件 ...
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
使用spring-boot作为基础框架,其理念为零配置文件,所有的配置都是基于注解和暴露bean的方式. Quartz的4个核心概念: 1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法v ...
- Spring 源码分析之 bean 依赖注入原理(注入属性)
最近在研究Spring bean 生命周期相关知识点以及源码,所以打算写一篇 Spring bean生命周期相关的文章,但是整理过程中发现涉及的点太多而且又很复杂,很难在一篇文章中把Spri ...
随机推荐
- linux socket 编程(C语言)
转自:http://blog.csdn.net/piaojun_pj/article/details/5920888 最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很 ...
- 关于服务器响应,浏览器请求的理解以及javaWeb项目的编码问题
1.服务器(Server)响应,浏览器(Brower)请求: 对于B/S的软件,数据的传递体现在,用户利用浏览器请求,以获得服务器响应.在JavaWeb项目中,大致包含.java文件的数据处理模块,和 ...
- JAVA 获取web文件的相对路径
转自:http://wwwdd2315.blog.163.com/blog/static/66661889201091953350298/ 在JAVA文件中获取该项目的相对路径1.基本概念的理解 绝对 ...
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- Aqua Data Studio中文乱码
使用Aqua Data Studio 查询数据时,如果表中的数据有中文时,会显示乱码,如下图: 解决方法很简单,只能更改字体即可,步骤如下: 更改字体后,显示的结果如下:
- js:语言精髓笔记1--标识符与基本类型
标识符: 命名: 语法以及类型----语法关键字 //逻辑 值(的存储位置)----变量和常量 ...
- UIBarButtonItem不能获取frame
在使用KxMenu这个厉害的控件做竖直列表的时候,发现UIBarButtonItem不能获取到frame,UIBarButtonItem是NSObject的子类,他不是一个uiresponed或者ui ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Booklet Printing[HDU1117]
Booklet Printing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法
首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...