## 3.1、創建module #### 3.1.1、右擊project,創建新module ![image](https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081202352-22924479.png) ### 3. ...
3.1、創建module
3.1.1、右擊project,創建新module
3.1.2、選擇maven
3.1.3、設置module名稱和路徑
3.1.4、module初始狀態
3.1.5、配置打包方式和依賴
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.rain</groupId>
<artifactId>spring_helloword</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- 基於Maven依賴傳遞性,導入spring-context依賴即可導入當前所需所有jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- junit測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.2、示例
3.2.1、創建組件類
package org.rain.spring.component;
/**
* @author liaojy
* @date 2023/7/25 - 19:17
*/
public class HelloWord {
public void sayHello(){
System.out.println("Hello,Spring!");
}
}
3.2.2、創建Sprig配置文件
++++++++++++++++++++++++++++++++++++分割線++++++++++++++++++++++++++++++++++++
註意:因為會通過自定義代碼指定Spring配置文件,所以Spring配置文件名可以是任意的;
但當整合ssm後,就不能通過通過自定義代碼指定Spring配置文件,因此文件名有硬性要求。
<?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標簽:配置交給Spring的IOC容器管理的對象
id屬性:設置bean的唯一標識
class屬性:設置bean所對應類型的全類名
-->
<bean id="helloWord" class="org.rain.spring.component.HelloWord"></bean>
</beans>
3.2.3、創建測試類
如圖所示,獲取到了IOC容器和容器中對應的bean組件,併成功調用了該bean組件的方法
package org.rain.spring.test;
import org.junit.Test;
import org.rain.spring.component.HelloWord;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liaojy
* @date 2023/7/25 - 19:39
*/
public class HelloWordTest {
@Test
public void testHelloWord(){
//獲取IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//獲取IOC容器中的bean
HelloWord helloWord = (HelloWord)applicationContext.getBean("helloWord");
helloWord.sayHello();
}
}
3.3、獲取bean的三種方式
3.3.1、根據id獲取
由於 id 屬性是 bean 的唯一標識,所以根據 bean 標簽的 id 屬性可以精確獲取到一個組件對象;但也存在類型轉換問題,具體見上一小節。
3.3.2、根據類型獲取(最常用)
如圖所示,根據類型獲取bean,則不存在類型轉換問題
@Test
public void testHelloWord(){
//獲取IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//獲取IOC容器中的bean
HelloWord helloWord = applicationContext.getBean(HelloWord.class);
helloWord.sayHello();
}
註意:如下圖所示,當根據類型獲取bean時,要求IOC容器中指定類型的bean有且只能有一個;否則會報錯
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'org.rain.spring.component.HelloWord' available:
expected single matching bean but found 2: helloWord,helloWordtwo
+++++++++++++++++++++++++++++++++++分割線+++++++++++++++++++++++++++++++++++
3.3.3、根據id和類型獲取
@Test
public void testHelloWord(){
//獲取IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//獲取IOC容器中的bean
HelloWord helloWord = applicationContext.getBean("helloWord",HelloWord.class);
helloWord.sayHello();
}
3.3.4、重要擴展
如果組件類實現了介面,則根據介面類型可以獲取 bean,前提是IOC容器中實現該介面的組件類型的bean有且只能有一個
+++++++++++++++++++++++++++++++++++分割線+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++分割線+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++分割線+++++++++++++++++++++++++++++++++++
@Test
public void testHelloWord(){
//獲取IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//根據介面類型獲取IOC容器中的bean
HelloWord helloWord = (HelloWord) applicationContext.getBean(Hello.class);
helloWord.sayHello();
}