01Spring基於xml的IOC配置 1、創建一個普通的maven工程 1.1 選擇maven,不用骨架,點擊下一步。 1.2 填寫GroupId、ArtifactId、Version。填完點擊下一步 1.3 確認項目路徑,確認完成點擊finish 2、模擬三層創建包和類 2.1 創建servic ...
01Spring基於xml的IOC配置
1、創建一個普通的maven工程
1.1 選擇maven,不用骨架,點擊下一步。
1.2 填寫GroupId、ArtifactId、Version。填完點擊下一步
1.3 確認項目路徑,確認完成點擊finish
2、模擬三層創建包和類
2.1 創建service介面
package com.yuank.service; public interface IAccountService { /** * 模擬保存賬戶 */ void saveAccount(); }
2.2 創建service介面的實現類
package com.yuank.service.impl; import com.yuank.dao.IAccountDao; import com.yuank.dao.impl.AccountDaoImpl; import com.yuank.service.IAccountService; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:33 * @description TODO **/ public class AccountServiceImpl implements IAccountService { //手動初始化 private IAccountDao accountDao = new AccountDaoImpl(); public void saveAccount() { accountDao.saveAccount(); } }
2.3 創建dao持久層介面
package com.yuank.dao; public interface IAccountDao { void saveAccount(); }
2.4 創建dao持久層實現類
package com.yuank.dao.impl; import com.yuank.dao.IAccountDao; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:38 * @description TODO **/ public class AccountDaoImpl implements IAccountDao { public void saveAccount() { System.out.println("保存賬戶"); } }
2.5 創建web表現層
import com.yuank.service.IAccountService; import com.yuank.service.impl.AccountServiceImpl; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:39 * @description TODO **/ public class Client { public static void main(String[] args) { IAccountService accountService = new AccountServiceImpl(); accountService.saveAccount(); } }
2.6 測試運行
3、創建配置文件
3.1 在resources下創建: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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
3.2 在pom文件中添加依賴,解決配置文件約束報紅
<dependencies> <!--IOC相關依賴--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency> </dependencies>
3.3 通過spring的IOC來創建對象
<!-- 1. spring會通過反射的方式創建對象,並將該對象以key和value的方式存入到IOC容器中。 2. bean標簽的id就是key,value就是類的全路徑 3. 通過bean標簽將對象創建並存入到IOC容器的方式,我們可以稱之為裝配bean 4. 只要可以正常new出來的對象,都可以通過這種方式裝配到IOC容器中 --> <!--將service實現類裝配到IOC容器中--> <bean id="accountService" class="com.yuank.service.impl.AccountServiceImpl"/> <!-- 將dao實現類裝配到IOC容器中 --> <bean id="accountDao" class="com.yuank.dao.impl.AccountDaoImpl"/>
3.4 載入applicationContext.xml配置文件&從spring的IOC容器中獲取對象
package com.yuank.web; import com.yuank.dao.IAccountDao; import com.yuank.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author PC-Black * @version v1.0 * @date 2019/7/21 21:39 * @description TODO **/ public class Client { public static void main(String[] args) { // IAccountService accountService = new AccountServiceImpl(); //1. 載入配置文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2. 從容器中獲取service IAccountService accountService = (IAccountService) ac.getBean("accountService"); //2.1 列印accountService對象 System.out.println(accountService); //3. 從容器中獲取dao IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao"); //3.1 列印accountDao對象 System.out.println(accountDao); accountService.saveAccount(); } }
3.5 運行main方法
查看控制台,我們發現對象創建成功