Spring 的属性注入
对于类成员变量,注入有三种方式
1、构造函数注入
2、属性 setter 方法注入
3、接口注入
构造方法注入
通过构造方法注入 Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
构造器注入在
实例演示
新建一个 demo4,创建一个 User 类。
package com.jikewenku.ioc.demo4; public class User { private String name; private Integer age; public User(String name,Integer age){ this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
接下来对它进行测试,先将这个类配置到Spring中去
<bean id="user" class="User"> <constructor-arg name="name" value="张三" /> <constructor-arg name="age" value="23"/> </bean>
再新建一个测试类SpringDemo4,写一个测试方法
@Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User)applicationContext.getBean("user"); System.out.println(user); }
运行程序,可以看到值已经显示出来了。
set 方法注入
使用 set 方法注入,在Spring配置文件中,通过
新建一个 Person 类
package com.jikewenku.ioc.demo4; public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
配置文件中进行配置
<bean id="person" class="Person"> <property name="name" value="李四"/> <property name="age" value="32"/> </bean>
SpringDemo4 中编写测试类
@Test public void demo2(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person)applicationContext.getBean("person"); System.out.println(person); }
运行程序,结果如下
p 名称空间
使用 p 命名空间:为了简化 XML 文件配置,Spring从 2.5 开始引入一个新的 p 名称空间
p:<属性名>=”xxx”引入常量值
p:<属性名>-ref=”xxx”引用其他 Bean 对象
编写配置文件
<bean id="person" class="com.jikewenku.ioc.demo4.Person" p:name="大黄" p:age="34"/>
剩下的操作和前面类似,就不详细介绍了。
SpEL 注入
SpELl:spring expression language,spring 表达式语言,对依赖注入进行简化
语法:#{表达式}
SpEL 表达式语言
语法:#{}
#{‘hello’}:使用字符串
#{topicId3}:使用另一个 bean
#{topicId4.content.toUpperCase()}:使用指定名属性,并使用方法
#{T{java.lang.Math}.PI}:使用静态字段或方法
代码演示
新建一个类 Product
package com.jikewenku.ioc.demo4; public class Product { private String name; private Double price; private Category category; 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; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Override public String toString() { return "Product{" + "name='" + name + '\'' + ", price=" + price + ", category=" + category + '}'; } }
新建一个类 Category
package com.jikewenku.ioc.demo4; public class Category { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Category{" + "name='" + name + '\'' + '}'; } }
在配置文件中编写
<!--Bean 的 SpEL 的属性注入==============================--> <bean id="category" class="com.jikewenku.ioc.demo4.Category"> <property name="name" value="#{'服装'}"/> </bean> <bean id="product" class="com.jikewenku.ioc.demo4.Product"> <property name="name" value="#{'男装'}"/> </bean>
SpringDemo4 中编写测试类
@Test public void demo3(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Product product = (Product)applicationContext.getBean("product"); System.out.println(product); }
运行程序,观察结果
假设此时想调用另外一个 Bean 的方法,这时候应该怎么写呢?
新建一个类 ProductInfo
package com.jikewenku.ioc.demo4; public class ProductInfo { public Double calculatePrice(){ return Math.random() * 199; } }
编写配置文件
<bean id="productInfo" class="com.jikewenku.ioc.demo4.ProductInfo"/> <bean id="product" class="com.jikewenku.ioc.demo4.Product"> <property name="name" value="#{'男装'}"/> <property name="price" value="#{productInfo.calculatePrice()}"/> <property name="category" value="#{category}"/> </bean>
再运行一下刚才的测试类 Demo3
复杂类型的属性注入
数组类型的属性注入
List 集合类型的属性注入
Set 集合类型的属性注入
Map 集合类型的属性注入
Propertis 类型的属性注入
代码演示
新建一个包 demo5,新建一个 CollectionBean
package com.jikewenku.ioc.demo5; import java.util.*; public class CollectionBean { private String[] arrs; // 数组类型 private List<String> list;// List 集合类型 private Set<String> set; // Set 集合类型 private Map<String,Integer> map;// Map 集合类型 private Properties properties; // 属性类型 public String[] getArrs() { return arrs; } public void setArrs(String[] arrs) { this.arrs = arrs; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Integer> getMap() { return map; } public void setMap(Map<String, Integer> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "CollectionBean{" + "arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } }
编写配置文件
<!--集合类型的属性注入=================================--> <bean id="collectionBean" class="com.jikewenku.ioc.demo5.CollectionBean"> <!--数组类型--> <property name="arrs"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> <!--List 集合的属性注入--> <property name="list"> <list> <value>111</value> <value>222</value> <value>333</value> </list> </property> <!--Set 集合的属性注入--> <property name="set"> <set> <value>ddd</value> <value>eee</value> <value>fff</value> </set> </property> <!--Map 集合的属性注入--> <property name="map"> <map> <entry key="aaa" value="111"/> <entry key="bbb" value="222"/> <entry key="ccc" value="333"/> </map> </property> <!--Properties 的属性注入--> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">1234</prop> </props> </property> </bean>
新建一个测试类 SpringDemo5
@Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionBean collectionBean = (CollectionBean)applicationContext.getBean("collectionBean"); System.out.println(collectionBean); }
运行程序