Spring配置文件
1.alias:設置別名,為bean設置別名,並且可以設置多個別名;
<!-- 設置別名 --> <alias name="user" alias="user1"/>
2.bean的配置;
<!--id是bean的標識符,要唯一,如果沒有配置id,name默認為標識符 如果配置了id,又配置了name,那么name就是別名 name可以設置多個別名並且別名可以是空格 逗號 分號 class是bean的全限定名=包名+類名 如果不配置id和name,則可以根據applicationContext.getBean(Class)獲取對象 --> <bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello"> <property name="name" value="張三"></property> </bean>
3.團隊協作開發,通過import來實現;
在我們的beans.xml中就可以引入和使用entity.xml文件中的bean配置(代碼中config/spring為我們新建的包config.spring)
<import resource="config/spring/entity.xml"/>
而entity.xml中進行bean的詳細配置:
<bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello"> <property name="name" value="張三"></property> </bean>
Bean的作用域
scope指bean的作用域,在配置bean時,有scope屬性來配置bean的作用
注意:在整合struts和spring時,需要將action設為scope="prototype";
<bean id="addr" class="cn.sxt.vo.Address" scope="singleton"> <!-- scope:bean的作用域 (默認是singleton): singleton:單例(用計數器記錄),整個容器中只有一個對象的實例; prototype原型:每次獲取bean都產生一個新的對象; request:每次請求時,創建一個新的對象; session:在回話的范圍內是一個對象(servlet的session); global session: 只在portlet下有用,表示是application application:在應用范圍內,只有一個對象; -->
Bean的自動裝配(spring4)
是用來簡化spring的配置文件,在配置bean時,可以配置bean的autowire屬性,用於指定裝配類型
<bean id="userDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"></bean> <!-- autowire:自動裝配,用於簡化spring的配置 no 不使用自動裝配 byname 根據名稱(set方法名)來查找相應的bean,如果有則裝配上去 使用形式:xml頭文件最后面加上default-autowire="byName" byType 根據類型進行裝配,不同去管bean的id(bean的id可以寫任意) 但是同一種類型的bean只能有一個(盡量慎用byType) constructor 當使用構造器實例化bean時,適用byType的方式裝配構造方法 --> <bean id="service" class="cn.sxt.service.impl.UserServiceImpl" autowire="constructor"></bean>
可以配置全局的自動裝配類型,在xml文件的頭部:default-autowire="byname"
【注意】推薦不使用自動裝配,而使用annotation
依賴注入 DI
1.依賴注入--dependency injection;
依賴:指bean對象創建依賴於容器,Bean對象的依賴資源
注入:指bean對象依賴的資源由容器來設置和裝配
2.spring注入--構造器注入
見IOC創建對象,也就是上一篇筆記中IOC構造方法創建對象的內容(有參和無參)
3.spring注入--setter注入(重點)
要求被注入的屬性必須有set方法。set方法的方法名由set+屬性(屬性首字母大寫),如果屬性是boolean類型,沒有get方法(是is);
3.1 常量注入
Student.java:
package cn.sxt.vo;
public class Student {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
beans.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="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> </bean> </beans>
Test.java:
package cn.sxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.sxt.vo.Student;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
Student stu=(Student)ac.getBean("student");
stu.show();
}
}
3.2 Bean注入
新建一個類Address:
package cn.sxt.vo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student類中引用Address類的對象(Student的實例變量是另外一個類Address的對象):
private Address addr;
public void setAddr(Address addr) {
this.addr = addr;
}
beans.xml:
<bean id="addr" class="cn.sxt.vo.Address"> <property name="address" value="北京優衣庫"/> </bean> <bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> </bean>
3.3 數組注入
Student類繼續添加一個實例變量books以及books的set方法:
private String[] books;
public void setBooks(String[] books) {
this.books = books;
}
beans.xml中做以下修改:
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> </bean>
3.4 List注入
Student類繼續添加實例變量及其set方法:
private List<String> hobbies;
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
beans.xml做以下配置:
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>排球</value> </list> </property> </bean>
3.5 Map注入
Student繼續添加實例變量Map<String,String> cards;
private Map<String, String> cards;
public void setCards(Map<String, String> cards) {
this.cards = cards;
}
beans.xml做以下配置:
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>排球</value> </list> </property> <property name="cards"> <map> <entry key="中國銀行" value="1545615345415"></entry> <entry> <key><value>農業銀行</value></key> <value>54654861231543</value> </entry> </map> </property> </bean>
3.6 Set注入
Student類添加實例變量Set<String>games及其set方法;
private Set<String> games;
public void setGames(Set<String> games) {
this.games = games;
}
beans.xml做以下配置(都是些重復的步驟):
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>排球</value> </list> </property> <property name="cards"> <map> <entry key="中國銀行" value="1545615345415"></entry> <entry> <key><value>農業銀行</value></key> <value>54654861231543</value> </entry> </map> </property> <property name="games"> <set> <value>LOL</value> <value>dota</value> <value>cs</value> <value>dnf</value> <value>cf</value> </set> </property> </bean>
3.7 Null注入
Student類添加實例變量String wife及其set方法;
private String wife;
public void setWife(String wife) {
this.wife = wife;
}
beans.xml做以下修改:
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>排球</value> </list> </property> <property name="cards"> <map> <entry key="中國銀行" value="1545615345415"></entry> <entry> <key><value>農業銀行</value></key> <value>54654861231543</value> </entry> </map> </property> <property name="games"> <set> <value>LOL</value> <value>dota</value> <value>cs</value> <value>dnf</value> <value>cf</value> </set> </property> <property name="wife"><null/></property> </bean>
3.8 Properties 注入
Student繼續添加實例變量properties info;
private Properties info;
public void setInfo(Properties info) {
this.info = info;
}
beans.xml做以下修改:
<bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="張三豐"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>傲慢與偏見</value> <value>仲夏夜之夢</value> <value>霧都孤兒</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>排球</value> </list> </property> <property name="cards"> <map> <entry key="中國銀行" value="1545615345415"></entry> <entry> <key><value>農業銀行</value></key> <value>54654861231543</value> </entry> </map> </property> <property name="games"> <set> <value>LOL</value> <value>dota</value> <value>cs</value> <value>dnf</value> <value>cf</value> </set> </property> <property name="wife"><null/></property> <property name="info"> <props> <prop key="學號">2015534001</prop> <prop key="sex">男</prop> <prop key="name">張三</prop> </props> </property> </bean>
3.9 p命名空間注入
beans.xml頭文件添加以下內容:
我們新建了一個User類:包含name和age屬性
beans.xml配置形式如下所示:
<bean id="user" class="cn.sxt.vo.User" p:name="風清揚" p:age="230"></bean>
3.10 c注命名空間入(構造函數注入)
beans.xml頭文件添加以下內容:
c命名空間注入要求有對應參數的構造方法:
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
beans.xml做以下配置:
<bean id="u1" class="cn.sxt.vo.User" c:name="coco" c:age="16"></bean>
總結我們之前學習的內容:
spring--橋梁
spring--輕量級,易學,ioc,aop,事務,整合框架等
spring--ioc:控制反轉(權限的反轉)
spring--Di(和ioc一個概念,只不過是站在不同的角度)
