依赖注入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 ...
随机推荐
- org.springframework.beans包
beans包中最核心的两个类:DefaultListableBeanFactory&XmlBeanDefinitionReader DefaultListableBeanFactory Xml ...
- nodeAPI--FS
fs是唯一一个同时提供同步和异步API的模块: 读取文件夹文件名,数组形式返回: var fs = require('fs'); //async fs.readdir('./',function(er ...
- HDU2841 Visible Trees(容斥原理)
题目..大概就是有个m*n个点的矩形从(1,1)到(m,n),问从(0,0)出发直线看过去最多能看到几个点. 如果(0,0)->(x,y)和(0,0)->(x',y')两个向量平行,那后面 ...
- Android WebView常见问题解决方案汇总
问题目录: 1.为WebView自定义错误显示界面: 2.WebView cookies清理 3.清理cache 和历史记录 4.判断WebView是否已经滚动到页面底端 5.URL拦截 6.处理We ...
- CC150 - 11.1
Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...
- BZOJ3924 [Zjoi2015]幻想乡战略游戏
Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...
- MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...
- iOS开发Swift篇—简单介绍
iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...
- ArcEngine 异常 :The index passed was not within the valid range.
pRowBuffer.set_Value(pFds.FindField("W_Mean"), Re_mean[3]); 此句代码弹出异常:The index passed was ...
- PHP有两个不同的版本:4.x系列版本和5.x系列版本
在为用户提供动态内容方面,PHP和MySQL是一个强大的组合.这些年来,这两项产品已经跨越了它们最初的应用舞台,现在,一些世界上最繁忙的网站也在应用它们.虽然它们当初都是开源软件,只能在UNIX/Li ...