先導,對IOC容器的理解
阿新 • • 發佈:2022-03-25
先導,對IOC容器的理解
- 通俗的講就是把你的class類交給spring的IOC容器去管理
- 需要對該類的屬性注入一些值,就可以通過spring提供的xml檔案或者註解進行注入
- 自己使用時在IOC容器工廠中去獲取就可以了,從而實現控制
1、匯入maven依賴 5.3.15版本
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>pro01-maven-ider-parent</artifactId> <groupId>com.mhy.maven</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pro06-module-spring</artifactId> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.15</version> </dependency> </dependencies> </project>
2、xml配置檔案的常用bean注入
- 實體類
public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private Properties info; private String wife; }
- 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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.mhy.pojo.Address"> <property name="address" value="重慶交通大學"/> </bean> <bean id="student" class="com.mhy.pojo.Student"> <property name="name" value="mhy"/> <property name="address" ref="address"/> <property name="books"> <array> <value>西遊記</value> <value>紅樓夢</value> <value>水滸傳</value> <value>三國演義</value> </array> </property> <property name="hobbys"> <list> <value>數學</value> <value>程式碼</value> </list> </property> <property name="games"> <set> <value>LOL</value> <value>CF</value> <value>DMF</value> </set> </property> <property name="card"> <map> <entry key="身份證" value="5002372222287878787"/> <entry key="學生卡" value="631910040417"/> </map> </property> <property name="info"> <props> <prop key="driver">com.mysql.jdbc.Driver</prop> <prop key="url">url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8</prop> <prop key="username">root</prop> <prop key="password">123456</prop> </props> </property> <property name="wife"> <null/> </property> </bean> </beans>
- 測試的程式碼
@Test
public void testSpring2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
/*結果--->
Student{name='mhy',
address=Address{address='重慶交通大學'},
books=[西遊記, 紅樓夢, 水滸傳, 三國演義],
hobbys=[數學, 程式碼],
card={身份證=5002372222287878787, 學生卡=631910040417},
games=[LOL, CF, DMF],
info={
password=123456,
url=url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8,
driver=com.mysql.jdbc.Driver,
username=root},
wife='null'}
*/
}
- p和c標籤的使用
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.mhy.pojo.Hello">
<property name="str" value="hello spring"/>
</bean>
<bean id="hello4" class="com.mhy.pojo.Hello">
<property name="str" value="hello spring 2"/>
</bean>
<bean id="hello2" class="com.mhy.pojo.Hello" p:str="hello spring p"/>
<bean id="hello3" class="com.mhy.pojo.Hello" c:str="hello spring c"/>
</beans>
注意:
- c標籤是set注入
- p標籤是有引數構造注入