spring之属性注入
来源:互联网 发布:2017淘宝如何投诉盗图 编辑:程序博客网 时间:2024/05/14 19:17
综述:属性注入,意思就是先通过类的无参构造函数构造一个对象,然后通过调用setter方法来设置对象的属性。
所以,使用属性注入时,有两个前提。
(一):类有无参构造器。
(二):类的属性有setter方法。
但是我们知道,类的属性有很多种类型,比如:基本类型(int、double)、string、集合(list、map)、其他对象的引用。
接下来挨个看看这些类型在spring的xml文件中如何配置。
1、属性注入-------基本类型注入。
package com.cmm;public class Person {private int age;private String name;private double high;public Person(){};public void setAge(int age) {this.age = age;}public void setHigh(double high) {this.high = high;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person [age=" + age + ", name=" + name + ", high=" + high + "]";}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"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-4.3.xsd"><!-- id: 表示这个Bean的标识,可以理解为一个对象的变量名class:类全名,表示这个Bean的类型 --><bean id="personA" class="com.cmm.Person"><!-- 使用属性注入 --><!-- name: 表示属性,如name、age、highvalue: 表示属性值,如"Tom",15,155将分别调用setName/setAge/setHigh方法设置对象的属性值 --><property name="name" value="Tom"></property><property name="age" value="15"></property><property name="high" value="155"></property></bean></beans>
package com.cmm;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("AppCtx.xml");Person personA = (Person)context.getBean("personA");System.out.println(personA);}}
value表示值类型。
2、属性注入-------集合类型注入。
首先,定义一个辅助类。
package com.cmm;public class Phone {private String brand;private int price;public Phone() {}public void setBrand(String brand) {this.brand = brand;}public void setPrice(int price) {this.price = price;}@Overridepublic String toString() {return "Phone [brand=" + brand + ", price=" + price + "]";}}
然后定义Person。
package com.cmm;import java.util.List;import java.util.Map;public class Person {private int age;private String name;private double high;private List<Phone> phones;public Person(){};public void setAge(int age) {this.age = age;}public void setHigh(double high) {this.high = high;}public void setName(String name) {this.name = name;}public void setPhones(List<Phone> phones) {this.phones = phones;}@Overridepublic String toString() {return "Person [age=" + age + ", name=" + name + ", high=" + high + ", phones=" + phones + "]";}}
可以看出,添加了一个list属性。
配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"><bean id="hw" class="com.cmm.Phone"><property name="brand" value="HW"></property><property name="price" value="3000"></property></bean><bean id="oppo" class="com.cmm.Phone"><property name="brand" value="oppo"></property><property name="price" value="2000"></property></bean><!-- id: 表示这个Bean的标识,可以理解为一个对象的变量名class:类全名,表示这个Bean的类型 --><bean id="personA" class="com.cmm.Person"><!-- 使用属性注入 --><!-- name: 表示属性,如name、age、highvalue: 表示属性值,如"Tom",15,155将分别调用setName/setAge/setHigh方法设置对象的属性值 --><property name="name" value="Tom"></property><property name="age" value="15"></property><property name="high" value="155"></property><!-- 配置list类型的属性 --><property name="phones"><list><!-- 可以使用 ctrl + / 快捷键来查询list的子节点 --><ref bean="hw"/><ref bean="oppo"/></list></property></bean></beans>
在看看map类型的属性注入:
person类添加属性map
package com.cmm;import java.util.List;import java.util.Map;public class Person {private int age;private String name;private double high;private List<Phone> phones;private Map<Integer, String>address;public Person(){};public void setAge(int age) {this.age = age;}public void setHigh(double high) {this.high = high;}public void setName(String name) {this.name = name;}public void setPhones(List<Phone> phones) {this.phones = phones;}public void setAddress(Map<Integer, String> address) {this.address = address;}@Overridepublic String toString() {return "Person [age=" + age + ", name=" + name + ", high=" + high + ", phones=" + phones + ", address="+ address + "]";}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"><bean id="hw" class="com.cmm.Phone"><property name="brand" value="HW"></property><property name="price" value="3000"></property></bean><bean id="oppo" class="com.cmm.Phone"><property name="brand" value="oppo"></property><property name="price" value="2000"></property></bean><!-- id: 表示这个Bean的标识,可以理解为一个对象的变量名class:类全名,表示这个Bean的类型 --><bean id="personA" class="com.cmm.Person"><!-- 使用属性注入 --><!-- name: 表示属性,如name、age、highvalue: 表示属性值,如"Tom",15,155将分别调用setName/setAge/setHigh方法设置对象的属性值 --><property name="name" value="Tom"></property><property name="age" value="15"></property><property name="high" value="155"></property><!-- 配置list类型的属性 --><property name="phones"><list><!-- 可以使用 ctrl + / 快捷键来查询list的子节点 --><ref bean="hw"/><ref bean="oppo"/></list></property><!-- 配置map属性 --><property name="address"><map><!-- 使用entry子节点来配饰map子元素 --><entry key="1" value="xian"></entry><entry key="2" value="hangzhou"></entry></map></property></bean></beans>
3、属性注入-------引用类型注入。
重写person类。
package com.cmm;public class Person {private Phone phone;public void setPhone(Phone phone) {this.phone = phone;}public Person() {// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "Person [phone=" + phone + "]";}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"><bean id="hw" class="com.cmm.Phone"><property name="brand" value="HW"></property><property name="price" value="3000"></property></bean><bean id="personA" class="com.cmm.Person"><property name="phone" ref="hw"></property></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"><bean id="personA" class="com.cmm.Person"><property name="phone" ref="hw"></property></bean><bean id="hw" class="com.cmm.Phone"><property name="brand" value="HW"></property><property name="price" value="3000"></property></bean></beans>
这里使用的set方法,对属性进行注入。配置起来还是有点麻烦。下一节,看看使用p空间进行属性注入。这个比较方便。
0 0
- Spring之属性注入
- spring之属性注入
- spring学习之属性注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- 【Spring学习】之 属性注入与对象属性注入
- spring学习之集合属性注入
- Spring 之 Bean的属性注入
- Spring基础之属性注入总结
- Spring入门之bean属性注入集合
- spring基础之属性注入总结
- Spring之ioc操作与属性注入
- spring 属性注入
- spring公共属性注入
- Spring中的属性注入
- Spring属性注入
- spring 注入属性
- CSU 1899: Yuelu Scenes
- 前端学HTTP之重定向和负载均衡
- 2017/4/24 阿里前端笔试题
- JSP中的taglib标签如何让JSP页面使用标签
- Linux命令行与shell脚本(3)--环境变量
- spring之属性注入
- //自己封装的几个函数方便记忆
- Android 接收无序广播
- POJ 3304 Segments(线段相交)
- C#的补充数据类型System.Numerics.dll
- JS过滤HTML标签
- 双bfs求树的直径
- CUDA卷积操作—使用constant memory实现高斯滤波
- Java表达式的陷阱——正则表达式的陷阱