上週末看了一章之前javaee輕量級的書spring部分,簡單作了一些筆記java
// ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//從類加載路徑搜索配置文件 ApplicationContext ac=new FileSystemXmlApplicationContext("src/bean.xml");//從相對路徑或絕對路徑搜索配置文件 設值注入:setter方法,xml的bean裏property 構造注入:構造器,xml的bean裏constructor-arg bean.xml設置<property name="lyx" value="哈哈"></property>,經過setter,例如 public void setLyx(String name) 獲取bean裏面的值 國際化: <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames" > //messageSource 和basenames名字都不能變 <list> <value>message</value> //message、message_zh_CN.properties、message_en_US.properties </list> </property> </bean> 好比hello={0},經過String hi=ac.getMessage("hello", new String[]{"哈哈"},Locale.getDefault(Locale.Category.FORMAT)); ApplicationContextAware接口,Spring容器會在建立該Bean以後, 自動調用該Bean的setApplicationContext()方法,調用該方法時, 會將容器自己做爲參數傳給該方法 applicationcontext事件機制:ApplicationListener監聽ApplicationEvent事件類 spring的做用域有: singleton:springioc容器中,做用的bean只生成一個實例 prototype:每次獲取該prototype做用域bean都生成新的一個實例 request:一次http對話,request做用域的bean只生成一個bean實例 session:一次http對話,session做用域的bean只生成一個bean實例 global session:每一個全局的httpsession對應一個bean實例 自動裝配:autowire,default-autowire no:必須經過ref元素定義 byName:經過bean的id進行setter該方法名字 byType:包含不要Name,直接setter就能夠了 constructor:和byTpye類似,找不到與構造器參數類型匹配的bean就拋異常 autodetect:根據bean內部決定使用constructor或byType 嵌套Bean:防止某個bean被訪問,把該bean放在有name的bean <property name="" >內部 <bean id="chinese" class="com.ij34.service.impl.Chinese" > <property name="dog" ><bean class="com.ij34.service.impl.DaHuang"/></property> </bean> 註解配置Annotation: @Configuration:修飾一個java類 @Bean:修飾一個方法,有返回值,構成了一個bean @Value:修飾一個Field,至關於爲配置一個變量 @Import:導入其餘java配置類 @Scope:修飾一個方法,指定對應的Bean的生命域 @Lazy:修飾一個方法,指定對應的Bran是否須要好延遲初始化 @DependsOn:初始化對應的Bean以前初始化指定的Bean @ImportResource:加載xml配置文件 spring建立bean的三種方式:調用構造器,調用靜態工廠方法,調用實例工廠方法 靜態工廠方法: <bean id="dog" class="靜態工廠類" factory-method="靜態工廠類裏面的方法"> <!-- 配置靜態工廠方法的參數 --> <constructor-arg value="一個參數,若是相同於靜態工廠類裏面的方法的參數,就返回該參數類的方法"/> <!-- 驅動Spring以"我是狗"爲參數來執行dog的setMsg()方法 --> <property name="msg" value="我是狗"/> </bean> 實例工廠方法: <bean id="chinese" factory-bean="工廠Bean名字" factory-method="工廠Bean裏面的方法名"> <!-- 配置實例工廠方法的參數 --> <constructor-arg value="chin"/> </bean> 抽象bean不能被實例化。抽象bean的價值在作父bean被繼承,配置設置成abstract="true" spring裏bean繼承與java繼承區別:前者是對象與對象之間的關係,後者是類與類之間的關係;前者能夠是不一樣類型,後者子類可看作一個特殊的父類;前者子bean不能做父bean使用,後者子類能夠徹底看成父類實例使用。 Spring中bean容器的生命週期 注入依賴關係以後的行爲有:使用init-method屬性、實現InitializingBean接口 bean銷燬以前的行爲有:使用destroy-method屬性,實現DisposableBean接口 lookup-method方法:bean定義爲抽象類裏面有抽象方法名與lookup-method的方法名相同。解決singleton與prototype做用域不一樣步 獲取其餘bean的屬性值 <bean id="son2" class="org.crazyit.app.service.Son"> <property name="age"> <!-- 使用嵌套Bean爲調用setAge()方法指定參數值 --> <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/> </property></bean> 或者<bean id="son2" class="org.crazyit.app.service.Son"> <property name="age"> <util:property-path path="person.son.age"/> </property></bean> 獲取Field(targetClass,targetField,targetObject)的值 <bean id="theAge1"class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <!-- targetClass指定訪問哪一個目標類 --> <property name="targetClass" value="java.sql.Connection"/> <!-- targetField指定要訪問的Field名 --> <property name="targetField" value="TRANSACTION_SERIALIZABLE"/> </bean> 簡化配置<util:constant id="theAge1"static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> 獲取方法的返回值MethodInvokingFactoryBean xml的bean配置才用p:和c:和util:命名簡化配置