依赖注入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 ...
随机推荐
- 【codevs2216】行星序列 线段树 区间两异同修改+区间求和*****
[codevs2216]行星序列 2014年2月22日3501 题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始,他就报名参加了“小小 ...
- js执行顺序
我们知道有个全局的 window对象,js的一切皆window上的属性和方法.window上有个window.document属性,记录了整个html的dom树,document是顶层. body 和 ...
- ls命令
ls(list) 命令可以说是Linux下最常用的命令之一 #ls -l;列出文件的详细信息 #ll 以上两个命令一样,ll是ls -l的简写 #ls -al;列出目录下的所有文件,包括以 . 开头的 ...
- python 自定义排序函数
自定义排序函数 Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 ...
- safari的坑
当将图片src设置为空字符的时候,图片仍然还在:
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- javascript生成n至m的随机整数
摘要: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备. Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包 ...
- BestCoder Round #73
这场比赛打完后可以找何神玩了orz(orz)* T1Rikka with Chess 嘿嘿嘿.输出n/2+m/2即可. 我能说我智商捉鸡想了4min吗? T2Rikka with Graph 由于N个 ...
- ext4 文件系统的一些记录
https://www.kernel.org/doc/Documentation/filesystems/ext4.txt ext4 权威说明 http://computer-forensics.sa ...
- lightning mdb 源代码分析系列(3)
本系列前两章已经描述了系统架构以及系统构建的基础内存映射,本章将详细描述lmdb的核心,外存B+Tree的操作.本文将从基本原理.内存操作方式.外存操作方式以及LMDB中的相关函数等几方面描述LMDB ...