spring5基礎學習之ioc
阿新 • • 發佈:2022-04-08
接上昨天的隨筆,繼續ioc操作練習
今天主要繼續xml配置中的內部bean,外包bean,級聯賦值操作
1.注入外部bean(將外部的物件注入userservice)
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"View Code> <!-- service和dao物件建立--> <bean id="userService" class="service.UserService"> <!-- 注入userDaodui物件--> <!-- name屬性值:類裡面的屬性名稱--> <property name="userdao" ref="userDao_1"></property> </bean> <bean id="userDao_1" class="dao.Userdaoimpl"> </bean> </beans>
類:
package dao; public interface Userdao { public void updata(); } package dao; public class Userdaoimpl implements Userdao{ @Override public void updata(){ System.out.println("dao updata......."); } }View Code
packageView Codeservice; import dao.Userdao; public class UserService { private Userdao userdao; public void setUserdao(Userdao userdao) { this.userdao = userdao; } //建立userdao型別屬性,生成set方法 public void add(){ System.out.println("service add......"); userdao.updata(); } }
測試:
@Test public void testadd() { ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); //獲取配置的建立物件 UserService userService = context.getBean("userService", UserService.class); userService.add(); }View Code
2.注入屬性-內部 bean(將內部的dept物件注入emp物件中)
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--> <bean id="emp" class="bean.emp"> <!-- 設定兩個普通的屬性--> <property name="ename" value="chenghaixiang"></property> <property name="gender" value="男"></property> <!-- 設定物件型別屬性--> <property name="dept"> <bean id="dept" class="bean.Dept"> <property name="dname" value="財務"></property> </bean> </property> </bean> </beans>View Code
類:
package bean; public class Dept { private String dname; public void setDname(String dname) { this.dname = dname; } @Override public String toString() { return "Dept{" + "dname='" + dname + '\'' + '}'; } } package bean; public class emp { private String ename; private String gender; //員工屬於某個部門,使用物件形式表示 private Dept dept; public void setEname(String ename) { this.ename = ename; } public void setGender(String gender) { this.gender = gender; } public void setDept(Dept dept) { this.dept = dept; } public void add(){ System.out.println(ename+","+gender+","+dept); } }View Code
測試就不放上去了
3.級聯賦值(與內部bean都一樣是講內部物件注入,但方法不一樣)
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="emp" class="bean.emp"> <!-- 設定兩個普通的屬性--> <property name="ename" value="chenghaixiang"></property> <property name="gender" value="男"></property> <!-- 設定物件型別屬性--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="bean.Dept"> <property name="dname" value="財務"></property> </bean> </beans>View Code