第一種:構造方法注入
● 通過構造方法注入Bean的屬性值或依賴的對象,它保證了Bean實例在實例化後就可以使用
● 構造器注入在 《constructor-arg》 元素裏聲明的屬性
代碼演示:
1.編寫一個javaBean
package com.zhou.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 +
'}';
}
}
2.編寫applicationContext.xml配置文件
<?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.xsd">
<bean id="user" class="com.zhou.ioc.demo4.User">
<constructor-arg name="name" value="小明"/>
<constructor-arg name="age" value="18"/>
</bean>
</beans>
3.編寫測試類
package com.zhou.ioc.demo4;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class springDemo1 {
@Test
public void demo1(){
//獲得工廠
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
}
結果:
User{name='小明', age=18}
.第二種方式 :Set 方法注入
使用set方法注入,在Spring配置文件種,通過 《property》設置注入的屬性
案例演示:
- 創建一個javaBean , 裏面不僅包含普通屬性 還包含對象屬性
——
package com.zhou.ioc.demo4;
public class Person {
private String name;
private Integer age;
private Cat cat;
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;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", cat=" + cat +
'}';
}
}
2.編寫Cat 對象屬性
package com.zhou.ioc.demo4;
public class Cat {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}
3.編寫 applicationContext.xml配置文件
<?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.xsd">
<bean id="person" class="com.zhou.ioc.demo4.Person">
<property name="name" value="黃大炮"/>
<property name="age" value="250"/>
<property name="cat" ref="cat"/>
</bean>
<bean id="cat" class="com.zhou.ioc.demo4.Cat">
<property name="name" value="胖橘"/>
</bean>
</beans>
小提示 : ref:是用來引入其他bean對象的id或name的
4 編寫測試類
package com.zhou.ioc.demo4;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class springDemo1 {
@Test
public void demo(){
//獲得工廠
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
}
}
Person{name='黃大炮', age=250, cat=Cat{name='胖橘'}}
第3種方式:p名稱空間注入
首先得引入p名稱空間,這跟beans差不多 我們可以複製一下 然後對其修改就行了
xmlns:p=“http://www.springframework.org/schema/p”
接着我們就可以直接去調用 直接在 標籤裏面填寫 格式如下:
p:<屬性名>=“xxx” 引入常量值
p:<屬性名>-ref=“xxx” 引入其他Bean對象
案例演示:
我們就直接用上面set的案例對其就行修改一下
1.修改applicationContext.xml 配置文件
<?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 id="person" class="com.zhou.ioc.demo4.Person" p:name="大黃" p:age="18" p:cat-ref="cat"/>
<bean id="cat" class="com.zhou.ioc.demo4.Cat" p:name="小黃" />
</beans>
這裏我們添加了一個p名稱空間 :xmlns:p=“http://www.springframework.org/schema/p”
然後對<bean>標籤進行了修改
2.直接執行測試類得到結果
Person{name='大黃', age=
Person{name='大黃', age=18, cat=Cat{name='小黃'}}
第4種: SpEL注入
即spring表達式語言,對依賴注入進行簡化
語法:#{表達式}
<bean id = " " value = “#{表達式}” 》
SpEL表達式語言:
#{ ‘hello’} :使用字符串
#{topicId}:使用另一個bean
#{topicld.add()}: 使用指定名屬性, 並使用屬性裏的方法
#{ T (java.lang.Math).PI} : 使用靜態字段或方法
代碼演示:
1.創建一個Product 產品類
package com.zhou.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 +
'}';
}
}
2、創建 Category 商品 類
package com.zhou.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 + '\'' +
'}';
}
}
3.編寫 一個計算商品價格的ProductInfo 類
package com.zhou.ioc.demo4;
public class ProductInfo {
public Double calculatePrice(){
return Math.random() * 200;
}
}
4.編寫 applicationContext.xml
<?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 id="product" class="com.zhou.ioc.demo4.Product">
<property name="name" value="#{'服裝'}"/>
<!--productInfo.calculatePrice() 調用productInfo中的calculatePrice()方法-->
<property name="price" value="#{productInfo.calculatePrice()}"/>
<property name="category" value="#{category}"/>
</bean>
<bean id="productInfo" class="com.zhou.ioc.demo4.ProductInfo"/>
<bean id="category" class="com.zhou.ioc.demo4.Category">
<property name="name" value="#{'女裝'}"/>
</bean>
</beans>
5.結果
Product{name='服裝', price=69.97578331596061, category=Category{name='女裝'}}
第5種:複雜類型注入
數組類型的屬性注入
List集合類型的屬性注入
Set集合類型的屬性注入
Map集合類型的屬性注入
Properties類型的屬性注入
1.編寫一個demo
package com.zhou.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 +
'}';
}
}
2、編寫applicationContext.xml
<?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 id="collectionBean" class="com.zhou.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>
</beans>
3.編寫測試類
package com.zhou.ioc.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo5 {
@Test
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean = (CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
}
4.結果
CollectionBean{arrs=[aaa, bbb, ccc], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}
————————————————
版權聲明:本文爲CSDN博主「逃不掉的熱愛」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_42137956/article/details/90112478