1.IOC爲反轉控制,是一種設計思想,能夠直接將其理解爲一個容器,這個容器能夠幫你管理全部的組件,之前建立對象須要手動的new,在Spring中拿對象只須要直接從容器中取出。java
實現方式: private ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");//可理解爲獲取IOC容器 Object bean = ioc.getBean("XXX");根據組件ID從IOC容器中取值。
2.IOC容器中的全部組件都在xml中進行註冊配置。爲對象賦值有三種方式:mysql
1. 用property賦值. <bean id="Student" class="com.Ioctest.Student"> <property name="StudentName" value="小明"> <property name="Age" value="18"> </bean> 2.使用p名稱空間賦值,注意須要導入p名稱空間. <bean id="Student" class="com.Ioctest.Student" p:age="18" p:StudentName="小明" > </bean> 3.parent繼承。等同於子類父類。 <bean id="StudentParent" class="com.Ioctest.Student" abstract="true"> <property name="StudentName" value="小三"></property> <property name="Age" value="20"></property> </bean> <bean id="StudentChild" class="com.Ioctest.Student" parent="StudentParent"> <property name="StudentName" value="小四"></property> //繼承後能夠對任意值進行修改。 </bean> //切記!!!property中的name必定要和Setter方法後的名字相匹配。如該配置的Set方法應該是 public void SetStudentname()
針對實體類中的list和Map集合,Spring有<list></list>和<Map></Map>標籤。例如:假設Student實體類下有Private List<Teacher> teachers。則配置代碼應爲:sql
<bean id="Teacher02" class="com.Ioctest.Teacher"> <property name="Teachername" value="張老師"> </property> <property name="Age" value="44"> </property> <property name="salary" value="#{5000*12}"> </property> </bean> //#{}功能強大,能夠在裏面寫運算符,bean屬性值,調用靜態方法或非靜態方法 <bena id="Student" class="com.Ioctest.Student"> <property name="teachers"> <list> <bean class="com.Ioctest.Teacher"> <property name="Teachername" value="王老師"> <property name="Age" value="32"> </bean> <bean class="com.Ioctest.Teacher" ref="Teacher02">//ref能夠引用外部bean。 <bean class="com.Ioctest.Teacher"value="#{Teahcer02}>//#{}用於引用外部bean </list> </property> </bean>
假設Student實體類中有private Map<String, Object> maps,則配置代碼應爲:spa
<bean id="Student" class="com.Ioctest.Student"> <property name="maps"> <map> <entry key="key1" value="張三"></entry> <entry key="key2"> <bean class="com.Ioctest.Teacher"> <property name="Teachername" value="王老師"> </property> </bean> </entry> <entry key="Name"> <value>蕪湖</value> </entry> //<entry>表明一個鍵值對 </map> </property> </bean>
<bean id="Student2" class="com.Ioctest.Student"> <constructor-arg name="StudentName" value="小明"></constructor-arg> <constructor-arg name="gender" value="男" index="2"></constructor-arg> <constructor-arg name="age" value="18" type="java.lang.Integer"></constructor-arg> </bean> //注意使用構造器賦值時,實體類中必定要有帶參構造器。賦值順序要參照構造器參數位置。若是想不按順序能夠如代碼所示用index=?進行指引。若是但願進行重載,能夠用type="java.lang.Integer"進行重複賦值。
<bean id="Student" class="com.IocTest.Student" autowire="byType/byName/constructor"> //byType/byName/constructor分別對應着經過類型,名字,構造器引入。 </bean>
註解注入配合@AutoWired是最經常使用的注入方式,註解注入只需在xml文件中配置一句須要掃描包package的前綴設計
<context:component-scan base-package="com.IocTest"></context:component-scan>
而後經過@Controller、@Service、@Repository、@Component四種方式進行組件注入。code
引用外部文件最經常使用的就是有關jdbc配置文件的撰寫。
外部文件dbconfig.propertiescomponent
jdbc.username=root jdbc.password=123456 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test jdbc.driverClass=com.mysql.jdbc.Driver
xml代碼xml
<context:property-placeholder location="classpath:dbconfig.properties"/>//引入外部文件 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> </bean> //${}能夠取出文件中對應的值