spring框架IOC配置
第一步:匯入包 commons-logging-1.2.jar spring-beans-3.2.13.RELEASE.jar spring-context-3.2.13.RELEASE.jar spring-core-3.2.13.RELEASE.jar spring-expression-3.2.13.RELEASE.jar spring開頭的 4個包自帶的…… commons-log4j需要https://repo.spring.io 下載
第二步:配置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" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean元素宣告spring建立的物件例項,id屬性指定物件唯一識別符號,可以設定多個別名,用name屬性替換id屬性指定,用逗號隔開。 class屬性指定被宣告的例項對應類 --> <bean id="helloSpring" class="com.big3.dao.impl.HelloSpring" > <!-- 指定物件被賦值的屬性名賦值--> <property name="who" value="Spring" /> </bean> <bean id="greyInk" class="com.big3.dao.impl.GreyInk" ></bean> <bean id="colorink" class="com.big3.dao.impl.ColorInk" ></bean> <bean id="a4paper" class="com.big3.dao.impl.TextPaper" > <property name="charPerLine" value="8" /> <property name="linePerPage" value="6" /> </bean> <!-- value給常量和變數賦值 --> <bean id="b5paper" class="com.big3.dao.impl.TextPaper" > <property name="charPerLine" value="6" /> <property name="linePerPage" value="5" /> </bean> <!-- ref賦物件值 --> <bean id="printer" class="com.big3.dao.impl.Printer" > <property name="ink" ref="colorink" /> <property name="paper" ref="a4paper" /> </bean> <bean id="usLogger" class="com.big3.aop.UserServiceLogger"/> <!--通過構造方法注入引數,也可以用C:標籤--> <bean id="use" class="com.big3.pojo.User" c:_0="ssss" c:_1="1000"> <!--<constructor-arg index="0" > <value>zsc</value> </constructor-arg> <constructor-arg index="1" > <value>18</value> </constructor-arg>--> </bean> <bean id="userServiceImpl" class="com.big3.service.impl.UserServiceImpl"> <!-- 使用CDATA處理特殊字元 --> <property name="specialCharacter1"> <value><![CDATA[P&G]]></value> </property> <!-- 將特殊字元轉為實體引用 --> <property name="specialCharacter2"> <value>P&G</value> </property> <!-- JAVABEN賦值 --> <property name="innerBean"> <bean class="com.big3.pojo.User"> <property name="name"><value>zsc</value></property> <property name="age"><value>18</value></property> </bean> </property> <!-- List賦值,裡面還可以加<bean>和<ref>標籤 --> <property name="list"> <list> <value>000</value> <value>111</value> </list> </property> <!-- 陣列賦值,裡面也可以用list換掉array標籤 --> <property name="array"> <array> <value>aaaa</value> <value>bbbb</value> </array> </property> <!-- set賦值 --> <property name="set"> <set> <value>9999</value> <value>8888</value> </set> </property> <!-- Map賦值 --> <property name="map"> <map> <entry> <key><value>user</value></key> <!--<value>100</value>--> <bean class="com.big3.pojo.User" p:name="123456" p:age="100"/> </entry> </map> </property> <!-- Properties賦值 --> <property name="props"> <props> <prop key="name">wwww</prop> <prop key="age">50</prop> </props> </property> <!-- 空字串賦值 --> <property name="emptyValue"> <value></value> </property> <!-- null賦值 --> <property name="nullValue"> <null/> </property> </bean> </beans>
可以通過註解方法,就不用在xml檔案裡配置bean了,只需加入如下程式碼就可
<!--指定掃描註解定義的包-->
<context:component-scan base-package="cn.kgc.service,cn.kgc.pojo"></context:component-scan>
只需在類里加上註解即可: @PropertySource(“zscbbs.properties”) //在類前面加入,引入配置檔案 @Value("${user}") //給屬性賦配置檔案裡的值
@ImportResource 的作用是匯入Spring的配置檔案,讓配置檔案裡面的內容生效。
@Configuration 配置類,類似於配置檔案 @Bean
@Component 實現Bean元件的定義 @Repository 標註DAO類 @Service 標註業務類 @Controller 標註控制器類,也就是servlet類
@Resource 對類的成員變數和方法進行標註,預設按 byName自動注入,JSR-250規範定義的註解,單獨使用
@Autowired 對類的成員變數和方法進行標註,按byType自動注入,屬於spring框架,和@Qualifier一起使用 @Qualifier(“userDao”)
1、 @Autowired與@Resource都可以用來裝配bean. 都可以寫在欄位上,或寫在setter方法上。 2、 @Autowired預設按型別裝配(這個註解是屬業spring的),預設情況下必須要求依賴物件必須存在,如果要允許null值,可以設定它的required屬性為false,如:@Autowired(required=false) ,如果我們想使用名稱裝配可以結合@Qualifier註解進行使用 3、@Resource(這個註解屬於J2EE的),預設按照名稱進行裝配,名稱可以通過name屬性進行指定,如果沒有指定name屬性,當註解寫在欄位上時,預設取欄位名進行安裝名稱查詢,如果註解寫在setter方法上預設取屬性名進行裝配。當找不到與名稱匹配的bean時才按照型別進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。 4.一個類的成員變數是List<>或者Map<>時,如果型別用介面型別,用@Autowired或者@Resource註解,自動會把此介面的實現類物件儲存在集合裡。