Spring属性注入DI
2015-10-20 00:00
513 查看
1.构造方法注入(只需提供一个构造方法)
javabean1 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 + "]" ; } } |
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 > |
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 + "]" ; } } |
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> |
相关文章推荐
- Spring的Bean管理(XML方式)
- Java IO 之 介质流
- Java IO
- Java基础--多线程
- java 优化-单例模式
- java 基础-序列化问题
- javaI/O
- Spring入门到精通<三>Spring访问数据库
- Maven3仓库与配置
- 关于Jersey整合Spring注入出现NullPointException问题
- UVa 1496 - Peach Blossom Spring
- 用myeclipse连接mysql报错的解决方案
- java基础部分:数据类型转换和一些标示符的应用
- Java中使用DOM解析XML
- java数据库连接池实现原理
- java中map的用法
- 安装jdk目录下有jdk 和jre连个文件夹的解析
- Struts2学习笔记
- java io类图(转)
- java中奇偶数的判断